From: Yedidya Feldblum Date: Sat, 29 Jul 2017 19:52:49 +0000 (-0700) Subject: Define IPAddressV6 comparison ops in terms of its fields X-Git-Tag: v2017.07.31.00~8 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=4927f0878e61bdadd58472469b08fa8b716865ae;p=folly.git 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 --- diff --git a/folly/IPAddressV6.h b/folly/IPAddressV6.h index 7de7dfbd..d0c5cd60 100644 --- a/folly/IPAddressV6.h +++ b/folly/IPAddressV6.h @@ -330,7 +330,8 @@ class IPAddressV6 { } 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 * by addr. @@ -338,6 +339,31 @@ class IPAddressV6 { bool inBinarySubnet(const std::array addr, 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: union AddressStorage { in6_addr in6Addr_; @@ -369,37 +395,6 @@ std::ostream& operator<<(std::ostream& os, const IPAddressV6& addr); void toAppend(IPAddressV6 addr, std::string* 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 namespace std {