Commit 6a3a9eb0 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

DRY some methods in IPAddress

Summary:
[Folly] DRY some methods in `IPAddress`.

Specifically, the ones which just delegate to either `asV4()` or to `asV6()`.

Reviewed By: mzlee

Differential Revision: D5529797

fbshipit-source-id: 4dd3dc893ab19281325700b85400c1c1aadfd77f
parent 53b24a32
...@@ -65,6 +65,12 @@ typedef std::pair<IPAddress, uint8_t> CIDRNetwork; ...@@ -65,6 +65,12 @@ typedef std::pair<IPAddress, uint8_t> CIDRNetwork;
* @encode * @encode
*/ */
class IPAddress { class IPAddress {
private:
template <typename F>
auto pick(F f) const {
return isV4() ? f(asV4()) : f(asV6());
}
public: public:
// returns true iff the input string can be parsed as an ip-address // returns true iff the input string can be parsed as an ip-address
static bool validate(StringPiece ip); static bool validate(StringPiece ip);
...@@ -267,14 +273,12 @@ class IPAddress { ...@@ -267,14 +273,12 @@ class IPAddress {
// @return true if this address is all zeros // @return true if this address is all zeros
bool isZero() const { bool isZero() const {
return isV4() ? asV4().isZero() return pick([&](auto& _) { return _.isZero(); });
: asV6().isZero();
} }
// Number of bits in the address representation. // Number of bits in the address representation.
size_t bitCount() const { size_t bitCount() const {
return isV4() ? IPAddressV4::bitCount() return pick([&](auto& _) { return _.bitCount(); });
: IPAddressV6::bitCount();
} }
// Number of bytes in the address representation. // Number of bytes in the address representation.
size_t byteCount() const { size_t byteCount() const {
...@@ -302,32 +306,27 @@ class IPAddress { ...@@ -302,32 +306,27 @@ class IPAddress {
* {family:'AF_INET|AF_INET6', addr:'address', hash:long}. * {family:'AF_INET|AF_INET6', addr:'address', hash:long}.
*/ */
std::string toJson() const { std::string toJson() const {
return isV4() ? asV4().toJson() return pick([&](auto& _) { return _.toJson(); });
: asV6().toJson();
} }
// Hash of address // Hash of address
std::size_t hash() const { std::size_t hash() const {
return isV4() ? asV4().hash() return pick([&](auto& _) { return _.hash(); });
: asV6().hash();
} }
// Return true if the address qualifies as localhost. // Return true if the address qualifies as localhost.
bool isLoopback() const { bool isLoopback() const {
return isV4() ? asV4().isLoopback() return pick([&](auto& _) { return _.isLoopback(); });
: asV6().isLoopback();
} }
// Return true if the address qualifies as link local // Return true if the address qualifies as link local
bool isLinkLocal() const { bool isLinkLocal() const {
return isV4() ? asV4().isLinkLocal() return pick([&](auto& _) { return _.isLinkLocal(); });
: asV6().isLinkLocal();
} }
// Return true if the address qualifies as broadcast. // Return true if the address qualifies as broadcast.
bool isLinkLocalBroadcast() const { bool isLinkLocalBroadcast() const {
return isV4() ? asV4().isLinkLocalBroadcast() return pick([&](auto& _) { return _.isLinkLocalBroadcast(); });
: asV6().isLinkLocalBroadcast();
} }
/** /**
...@@ -337,8 +336,7 @@ class IPAddress { ...@@ -337,8 +336,7 @@ class IPAddress {
* 2000::/3, ffxe::/16. * 2000::/3, ffxe::/16.
*/ */
bool isNonroutable() const { bool isNonroutable() const {
return isV4() ? asV4().isNonroutable() return pick([&](auto& _) { return _.isNonroutable(); });
: asV6().isNonroutable();
} }
/** /**
...@@ -346,14 +344,12 @@ class IPAddress { ...@@ -346,14 +344,12 @@ class IPAddress {
* (for example, 192.168.xxx.xxx or fc00::/7 addresses) * (for example, 192.168.xxx.xxx or fc00::/7 addresses)
*/ */
bool isPrivate() const { bool isPrivate() const {
return isV4() ? asV4().isPrivate() return pick([&](auto& _) { return _.isPrivate(); });
: asV6().isPrivate();
} }
// Return true if the address is a multicast address. // Return true if the address is a multicast address.
bool isMulticast() const { bool isMulticast() const {
return isV4() ? asV4().isMulticast() return pick([&](auto& _) { return _.isMulticast(); });
: asV6().isMulticast();
} }
/** /**
...@@ -363,8 +359,7 @@ class IPAddress { ...@@ -363,8 +359,7 @@ class IPAddress {
* @return IPAddress instance with bits set to 0 * @return IPAddress instance with bits set to 0
*/ */
IPAddress mask(uint8_t numBits) const { IPAddress mask(uint8_t numBits) const {
return isV4() ? IPAddress(asV4().mask(numBits)) return pick([&](auto& _) { return IPAddress(_.mask(numBits)); });
: IPAddress(asV6().mask(numBits));
} }
/** /**
...@@ -373,8 +368,7 @@ class IPAddress { ...@@ -373,8 +368,7 @@ class IPAddress {
* @throws IPAddressFormatException on inet_ntop error * @throws IPAddressFormatException on inet_ntop error
*/ */
std::string str() const { std::string str() const {
return isV4() ? asV4().str() return pick([&](auto& _) { return _.str(); });
: asV6().str();
} }
/** /**
...@@ -383,27 +377,24 @@ class IPAddress { ...@@ -383,27 +377,24 @@ class IPAddress {
* this is the hex representation with : characters inserted every 4 digits. * this is the hex representation with : characters inserted every 4 digits.
*/ */
std::string toFullyQualified() const { std::string toFullyQualified() const {
return isV4() ? asV4().toFullyQualified() return pick([&](auto& _) { return _.toFullyQualified(); });
: asV6().toFullyQualified();
} }
/// Same as toFullyQualified but append to an output string. /// Same as toFullyQualified but append to an output string.
void toFullyQualifiedAppend(std::string& out) const { void toFullyQualifiedAppend(std::string& out) const {
return isV4() ? asV4().toFullyQualifiedAppend(out) return pick([&](auto& _) { return _.toFullyQualifiedAppend(out); });
: asV6().toFullyQualifiedAppend(out);
} }
// Address version (4 or 6) // Address version (4 or 6)
uint8_t version() const { uint8_t version() const {
return isV4() ? asV4().version() return pick([&](auto& _) { return _.version(); });
: asV6().version();
} }
/** /**
* Access to address bytes, in network byte order. * Access to address bytes, in network byte order.
*/ */
const unsigned char* bytes() const { const unsigned char* bytes() const {
return isV4() ? asV4().bytes() : asV6().bytes(); return pick([&](auto& _) { return _.bytes(); });
} }
private: private:
......
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