Define IPAddressV6 comparison ops in terms of its fields
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 29 Jul 2017 19:52:49 +0000 (12:52 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 29 Jul 2017 20:00:25 +0000 (13:00 -0700)
Summary: [Folly] Define `IPAddressV6` comparison ops in terms of its fields using `std::tie`.

Reviewed By: andrewjcg

Differential Revision: D5524248

fbshipit-source-id: 5f5f2acd6e9cfd6dfd148cc7d95bda720bf81ee9

folly/IPAddressV6.h

index 7de7dfbd5f8bdbd91505e48902a1c62739e91f79..d0c5cd609dc2b4fadc02054bfdcda693afc8e3f4 100644 (file)
@@ -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<uint8_t, 2> 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 {