mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-17 15:25:35 +00:00
36792d4337
Lots and lots of places in the code had broken < operators because they are returning something like: foo < other.foo or bar < other.bar; but this breaks both the strict weak ordering requirements that are required for the "Compare" requirement for things like std::map/set/priority_queue. For example: a = {.foo=1, .bar=3} b = {.foo=3, .bar=1} does not have an ordering over a and b (both `a < b` and `b < a` are satisfied at the same time). This needs to be instead something like: foo < other.foo or (foo == other.foo and bar < other.bar) but that's a bit clunkier, and it is easier to use std::tie for tuple's built-in < comparison which does the right thing: std::tie(foo, bar) < std::tie(other.foo, other.bar) (Initially I noticed this in SockAddr/sockaddr_in6, but upon further investigation this extends to the major of multi-field `operator<`'s.) This fixes it by using std::tie (or something similar) everywhere we are doing multi-field inequalities.
71 lines
1.5 KiB
C++
71 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include "util/bencode.hpp"
|
|
#include "constants/version.hpp"
|
|
#include "constants/proto.hpp"
|
|
#include "util/formattable.hpp"
|
|
|
|
namespace llarp
|
|
{
|
|
struct RouterVersion
|
|
{
|
|
using Version_t = std::array<uint16_t, 3>;
|
|
|
|
RouterVersion() = default;
|
|
|
|
explicit RouterVersion(const Version_t& routerVersion, uint64_t protoVersion);
|
|
|
|
bool
|
|
BEncode(llarp_buffer_t* buf) const;
|
|
|
|
bool
|
|
BDecode(llarp_buffer_t* buf);
|
|
|
|
/// return true if this router version is all zeros
|
|
bool
|
|
IsEmpty() const;
|
|
|
|
/// set to be empty
|
|
void
|
|
Clear();
|
|
|
|
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 std::tie(m_ProtoVersion, m_Version) < std::tie(other.m_ProtoVersion, other.m_Version);
|
|
}
|
|
|
|
bool
|
|
operator!=(const RouterVersion& other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
|
|
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}};
|
|
int64_t m_ProtoVersion = llarp::constants::proto_version;
|
|
};
|
|
|
|
template <>
|
|
constexpr inline bool IsToStringFormattable<RouterVersion> = true;
|
|
|
|
static constexpr int64_t INVALID_VERSION = -1;
|
|
static const RouterVersion emptyRouterVersion({0, 0, 0}, INVALID_VERSION);
|
|
|
|
} // namespace llarp
|