dont inherit std::array

pull/1072/head
jeff 4 years ago
parent fa30a6f9b2
commit 816070be62

@ -12,7 +12,6 @@
namespace llarp
{
const std::array<uint16_t, 3> VERSION{{LLARP_VERSION_MAJ, LLARP_VERSION_MIN, LLARP_VERSION_PATCH}};
const std::array<uint64_t, 4> ROUTER_VERSION{{LLARP_PROTO_VERSION, LLARP_VERSION_MAJ, LLARP_VERSION_MIN, LLARP_VERSION_PATCH}};
const char* const VERSION_STR = LLARP_VERSION_STR;
const char* const VERSION_TAG = "@VERSIONTAG@";
const char* const VERSION_FULL = LLARP_NAME "-" LLARP_VERSION_STR "-@VERSIONTAG@";

@ -6,11 +6,10 @@
namespace llarp
{
// Given a full lokinet version of: lokinet-1.2.3-abc these are:
extern const std::array< uint16_t, 3 > VERSION; // [1, 2, 3]
extern const std::array< uint64_t, 4 > ROUTER_VERSION; // [proto, 1, 2, 3]
extern const char* const VERSION_STR; // "1.2.3"
extern const char* const VERSION_TAG; // "abc"
extern const char* const VERSION_FULL; // "lokinet-1.2.3-abc"
extern const std::array< uint16_t, 3 > VERSION; // [1, 2, 3]
extern const char* const VERSION_STR; // "1.2.3"
extern const char* const VERSION_TAG; // "abc"
extern const char* const VERSION_FULL; // "lokinet-1.2.3-abc"
extern const char* const RELEASE_MOTTO;
extern const char* const DEFAULT_NETID;

@ -922,7 +922,7 @@ namespace llarp
// set router version if service node
if(IsServiceNode())
{
_rc.routerVersion = RouterVersion(llarp::ROUTER_VERSION);
_rc.routerVersion = RouterVersion(llarp::VERSION, LLARP_PROTO_VERSION);
}
AddressInfo ai;

@ -7,9 +7,15 @@
namespace llarp
{
RouterVersion::RouterVersion(const std::array< uint64_t, 4 >& other)
RouterVersion::RouterVersion(const Version_t& router, uint64_t proto)
: m_Version(router), m_ProtoVersion(proto)
{
std::copy_n(other.begin(), other.size(), begin());
}
bool
RouterVersion::IsCompatableWith(const RouterVersion& other) const
{
return m_ProtoVersion == other.m_ProtoVersion;
}
bool
@ -19,9 +25,13 @@ namespace llarp
return false;
if(not IsEmpty())
{
for(size_t idx = 0; idx < size(); ++idx)
if(not bencode_write_uint64(buf, at(idx)))
if(not bencode_write_uint64(buf, m_ProtoVersion))
return false;
for(const auto& i : m_Version)
{
if(not bencode_write_uint64(buf, i))
return false;
}
}
return bencode_end(buf);
}
@ -29,31 +39,36 @@ namespace llarp
void
RouterVersion::Clear()
{
fill(0);
at(0) = LLARP_PROTO_VERSION;
m_Version.fill(0);
m_ProtoVersion = LLARP_PROTO_VERSION;
assert(IsEmpty());
}
static const RouterVersion emptyRouterVersion({0, 0, 0}, LLARP_PROTO_VERSION);
bool
RouterVersion::IsEmpty() const
{
static const RouterVersion empty{{LLARP_PROTO_VERSION, 0, 0, 0}};
return std::equal(begin(), end(), empty.begin(), empty.end());
return std::equal(begin(), end(), emptyRouterVersion.begin(),
emptyRouterVersion.end());
}
bool
RouterVersion::BDecode(llarp_buffer_t* buf)
{
// clear before hand
fill(0);
Clear();
size_t idx = 0;
if(not bencode_read_list(
[self = this, &idx](llarp_buffer_t* buffer, bool has) {
if(has)
{
if(idx >= self->size())
return false;
if(not bencode_read_integer(buffer, &self->at(idx)))
if(idx == 0)
{
if(not bencode_read_integer(buffer, &self->m_ProtoVersion))
return false;
}
else if(not bencode_read_integer(buffer, &self->at(idx - 1)))
return false;
++idx;
}
@ -62,14 +77,16 @@ namespace llarp
buf))
return false;
// either full list or empty list is valid
return idx == size() || idx == 0;
return idx == 4 || idx == 0;
}
std::string
RouterVersion::ToString() const
{
return std::to_string(at(1)) + "." + std::to_string(at(2)) + "."
+ std::to_string(at(3)) + " protocol version " + std::to_string(at(0));
return std::to_string(m_Version.at(0)) + "."
+ std::to_string(m_Version.at(1)) + "."
+ std::to_string(m_Version.at(2)) + " protocol version "
+ std::to_string(m_ProtoVersion);
}
} // namespace llarp

@ -3,14 +3,19 @@
#include <array>
#include <util/bencode.hpp>
#include <constants/version.hpp>
#include <constants/proto.hpp>
namespace llarp
{
struct RouterVersion : public std::array< uint64_t, 4 >
struct RouterVersion
{
using Version_t = std::array< uint16_t, 3 >;
RouterVersion() = default;
explicit RouterVersion(const std::array< uint64_t, 4 >&);
explicit RouterVersion(const Version_t& routerVersion,
uint64_t protoVersion);
bool
BEncode(llarp_buffer_t* buf) const;
@ -28,6 +33,22 @@ namespace llarp
std::string
ToString() const;
/// return true if the other router version is compatible with ours
bool
IsCompatableWith(const RouterVersion& other) const;
/// compare router versions
bool
operator<(const RouterVersion& other) const
{
return m_ProtoVersion < other.m_ProtoVersion
|| m_Version < other.m_Version;
}
private:
Version_t m_Version = {0, 0, 0};
uint64_t m_ProtoVersion = LLARP_PROTO_VERSION;
};
inline std::ostream&

Loading…
Cancel
Save