Ensure that hash_append will never throw

Conflicts:
	src/beast/beast/net/IPAddress.h
This commit is contained in:
Nik Bougalis
2015-01-07 07:49:03 -08:00
committed by Vinnie Falco
parent e8c07717fc
commit f78269b02d
2 changed files with 11 additions and 9 deletions

View File

@@ -24,8 +24,9 @@
#include <beast/net/IPAddressV6.h> #include <beast/net/IPAddressV6.h>
#include <beast/hash/hash_append.h> #include <beast/hash/hash_append.h>
#include <beast/hash/uhash.h> #include <beast/hash/uhash.h>
#include <beast/utility/noexcept.h>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <cassert>
#include <cstdint> #include <cstdint>
#include <ios> #include <ios>
#include <string> #include <string>
@@ -102,14 +103,14 @@ public:
/** Returns `true` if this address represents an IPv4 address. */ /** Returns `true` if this address represents an IPv4 address. */
bool bool
is_v4 () const is_v4 () const noexcept
{ {
return m_type == ipv4; return m_type == ipv4;
} }
/** Returns `true` if this address represents an IPv6 address. */ /** Returns `true` if this address represents an IPv6 address. */
bool bool
is_v6() const is_v6() const noexcept
{ {
return m_type == ipv6; return m_type == ipv6;
} }
@@ -121,12 +122,11 @@ public:
AddressV4 const& AddressV4 const&
to_v4 () const to_v4 () const
{ {
if (m_type != ipv4) if (!is_v4 ())
throw std::bad_cast(); throw std::bad_cast();
return m_v4; return m_v4;
} }
/** Returns the IPv6 address. /** Returns the IPv6 address.
Precondition: Precondition:
is_v6() == `true` is_v6() == `true`
@@ -134,7 +134,7 @@ public:
AddressV6 const& AddressV6 const&
to_v6 () const to_v6 () const
{ {
if (m_type != ipv6) if (!is_v6 ())
throw std::bad_cast(); throw std::bad_cast();
return m_v6; return m_v6;
} }
@@ -142,13 +142,15 @@ public:
template <class Hasher> template <class Hasher>
friend friend
void void
hash_append(Hasher& h, Address const& addr) hash_append(Hasher& h, Address const& addr) noexcept
{ {
using beast::hash_append; using beast::hash_append;
if (addr.is_v4 ()) if (addr.is_v4 ())
hash_append(h, addr.to_v4 ()); hash_append(h, addr.to_v4 ());
else else if (addr.is_v6 ())
hash_append(h, addr.to_v6 ()); hash_append(h, addr.to_v6 ());
else
assert (false);
} }
/** Arithmetic comparison. */ /** Arithmetic comparison. */

View File

@@ -140,7 +140,7 @@ struct AddressV4
Proxy <true> operator[] (std::size_t index) const; Proxy <true> operator[] (std::size_t index) const;
Proxy <false> operator[] (std::size_t index); Proxy <false> operator[] (std::size_t index);
/** @{ */ /** @} */
/** The value as a 32 bit unsigned. */ /** The value as a 32 bit unsigned. */
std::uint32_t value; std::uint32_t value;