X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FIPAddressV4.h;h=061f53fc8a2523b5a0a1821e4f362e8969b69255;hp=93651577687d88d66164ca59ef44d6b90a6f83c6;hb=afde52ff7655ba79759eafdbee14b3fe47428fce;hpb=0b856bd5556ab370e029ff8f2a490db7dd4f3940 diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 93651577..061f53fc 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2014-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,12 @@ #include #include -#include +#include +#include +#include #include #include +#include namespace folly { @@ -59,7 +62,7 @@ class IPAddressV4 { 4 /*words*/ * 3 /*max chars per word*/ + 3 /*separators*/; // returns true iff the input string can be parsed as an ipv4-address - static bool validate(StringPiece ip); + static bool validate(StringPiece ip) noexcept; // create an IPAddressV4 instance from a uint32_t (network byte order) static IPAddressV4 fromLong(uint32_t src); @@ -70,11 +73,21 @@ class IPAddressV4 { * Create a new IPAddress instance from the provided binary data. * @throws IPAddressFormatException if the input length is not 4 bytes. */ - static IPAddressV4 fromBinary(ByteRange bytes) { - IPAddressV4 addr; - addr.setFromBinary(bytes); - return addr; - } + static IPAddressV4 fromBinary(ByteRange bytes); + + /** + * Non-throwing version of fromBinary(). + * On failure returns IPAddressFormatError. + */ + static Expected tryFromBinary( + ByteRange bytes) noexcept; + + /** + * Tries to create a new IPAddressV4 instance from provided string and + * returns it on success. Returns IPAddressFormatError on failure. + */ + static Expected tryFromString( + StringPiece str) noexcept; /** * Returns the address as a Range. @@ -112,10 +125,10 @@ class IPAddressV4 { explicit IPAddressV4(StringPiece ip); // ByteArray4 constructor - explicit IPAddressV4(const ByteArray4& src); + explicit IPAddressV4(const ByteArray4& src) noexcept; // in_addr constructor - explicit IPAddressV4(const in_addr src); + explicit IPAddressV4(const in_addr src) noexcept; // Return the V6 mapped representation of the address. IPAddressV6 createIPv6() const; @@ -140,7 +153,7 @@ class IPAddressV4 { * @see IPAddress#bitCount * @returns 32 */ - static size_t bitCount() { + static constexpr size_t bitCount() { return 32; } @@ -284,9 +297,10 @@ class IPAddressV4 { /** * Set the current IPAddressV4 object to have the address specified by bytes. - * @throws IPAddressFormatException if bytes.size() is not 4. + * Returns IPAddressFormatError if bytes.size() is not 4. */ - void setFromBinary(ByteRange bytes); + Expected trySetFromBinary( + ByteRange bytes) noexcept; }; // boost::hash uses hash_value() so this allows boost::hash to work