Commit 4927f087 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Define IPAddressV6 comparison ops in terms of its fields

Summary: [Folly] Define `IPAddressV6` comparison ops in terms of its fields using `std::tie`.

Reviewed By: andrewjcg

Differential Revision: D5524248

fbshipit-source-id: 5f5f2acd6e9cfd6dfd148cc7d95bda720bf81ee9
parent 21099baa
...@@ -330,6 +330,7 @@ class IPAddressV6 { ...@@ -330,6 +330,7 @@ class IPAddressV6 {
} }
const unsigned char* bytes() const { return addr_.in6Addr_.s6_addr; } const unsigned char* bytes() const { return addr_.in6Addr_.s6_addr; }
protected: protected:
/** /**
* Helper that returns true if the address is in the binary subnet specified * Helper that returns true if the address is in the binary subnet specified
...@@ -338,6 +339,31 @@ class IPAddressV6 { ...@@ -338,6 +339,31 @@ class IPAddressV6 {
bool inBinarySubnet(const std::array<uint8_t, 2> addr, bool inBinarySubnet(const std::array<uint8_t, 2> addr,
size_t numBits) const; size_t numBits) const;
private:
auto tie() const {
return std::tie(addr_.bytes_, scope_);
}
public:
friend inline bool operator==(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() == b.tie();
}
friend inline bool operator!=(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() != b.tie();
}
friend inline bool operator<(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() < b.tie();
}
friend inline bool operator>(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() > b.tie();
}
friend inline bool operator<=(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() <= b.tie();
}
friend inline bool operator>=(const IPAddressV6& a, const IPAddressV6& b) {
return a.tie() >= b.tie();
}
private: private:
union AddressStorage { union AddressStorage {
in6_addr in6Addr_; in6_addr in6Addr_;
...@@ -369,37 +395,6 @@ std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr); ...@@ -369,37 +395,6 @@ std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr);
void toAppend(IPAddressV6 addr, std::string* result); void toAppend(IPAddressV6 addr, std::string* result);
void toAppend(IPAddressV6 addr, fbstring* result); void toAppend(IPAddressV6 addr, fbstring* result);
/**
* Return true if two addresses are equal.
*/
inline bool operator==(const IPAddressV6& addr1, const IPAddressV6& addr2) {
return (std::memcmp(addr1.toAddr().s6_addr, addr2.toAddr().s6_addr, 16) == 0)
&& addr1.getScopeId() == addr2.getScopeId();
}
// Return true if addr1 < addr2
inline bool operator<(const IPAddressV6& addr1, const IPAddressV6& addr2) {
auto cmp = std::memcmp(addr1.toAddr().s6_addr,
addr2.toAddr().s6_addr, 16) < 0;
if (!cmp) {
return addr1.getScopeId() < addr2.getScopeId();
} else {
return cmp;
}
}
// Derived operators
inline bool operator!=(const IPAddressV6& a, const IPAddressV6& b) {
return !(a == b);
}
inline bool operator>(const IPAddressV6& a, const IPAddressV6& b) {
return b < a;
}
inline bool operator<=(const IPAddressV6& a, const IPAddressV6& b) {
return !(a > b);
}
inline bool operator>=(const IPAddressV6& a, const IPAddressV6& b) {
return !(a < b);
}
} // folly } // folly
namespace std { namespace std {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment