forked from boostorg/container_hash
Support std::error_code and std::error_condition
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
// Based on Peter Dimov's proposal
|
||||
// http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf
|
||||
// issue 6.18.
|
||||
// issue 6.18.
|
||||
//
|
||||
// This also contains public domain code from MurmurHash. From the
|
||||
// MurmurHash header:
|
||||
@@ -34,6 +34,10 @@
|
||||
#include <typeindex>
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
#include <system_error>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
#pragma warning(push)
|
||||
|
||||
@@ -211,6 +215,11 @@ namespace boost
|
||||
std::size_t hash_value(std::type_index);
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
std::size_t hash_value(std::error_code const&);
|
||||
std::size_t hash_value(std::error_condition const&);
|
||||
#endif
|
||||
|
||||
// Implementation
|
||||
|
||||
namespace hash_detail
|
||||
@@ -460,14 +469,30 @@ namespace boost
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
inline std::size_t hash_value(std::error_code const& v) {
|
||||
std::size_t seed = 0;
|
||||
hash_combine(seed, v.value());
|
||||
hash_combine(seed, &v.category());
|
||||
return seed;
|
||||
}
|
||||
|
||||
inline std::size_t hash_value(std::error_condition const& v) {
|
||||
std::size_t seed = 0;
|
||||
hash_combine(seed, v.value());
|
||||
hash_combine(seed, &v.category());
|
||||
return seed;
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
// boost::hash
|
||||
//
|
||||
|
||||
|
||||
// Define the specializations required by the standard. The general purpose
|
||||
// boost::hash is defined later in extensions.hpp if
|
||||
// BOOST_HASH_NO_EXTENSIONS is not defined.
|
||||
|
||||
|
||||
// BOOST_HASH_SPECIALIZE - define a specialization for a type which is
|
||||
// passed by copy.
|
||||
//
|
||||
|
@@ -44,6 +44,7 @@ test-suite container_hash/hash
|
||||
[ run hash_map_test.cpp ]
|
||||
[ run hash_complex_test.cpp ]
|
||||
[ run hash_type_index_test.cpp ]
|
||||
[ run hash_system_error_test.cpp ]
|
||||
[ run hash_std_array_test.cpp ]
|
||||
[ run hash_std_tuple_test.cpp ]
|
||||
[ run hash_std_smart_ptr_test.cpp ]
|
||||
|
@@ -78,4 +78,17 @@ int main() {
|
||||
std::cout << "BOOST_HASH_HAS_STRING_VIEW: "
|
||||
<< BOOST_HASH_HAS_STRING_VIEW
|
||||
<< std::endl;
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_TYPEINDEX)
|
||||
std::cout << "No <typeindex>" << std::endl;
|
||||
#else
|
||||
std::cout << "<typeindex>" << std::endl;
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
std::cout << "No <system_error>" << std::endl;
|
||||
#else
|
||||
std::cout << "<system_error>" << std::endl;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
55
test/hash_system_error_test.cpp
Normal file
55
test/hash_system_error_test.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
// Copyright 2018 Daniel James
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "./config.hpp"
|
||||
|
||||
#ifndef BOOST_HASH_TEST_STD_INCLUDES
|
||||
# include <boost/container_hash/hash.hpp>
|
||||
#endif
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
|
||||
#include <system_error>
|
||||
|
||||
void test_error_code()
|
||||
{
|
||||
std::error_code err1a = std::make_error_code(std::errc::address_family_not_supported);
|
||||
std::error_code err1b = std::make_error_code(std::errc::address_family_not_supported);
|
||||
std::error_code err2 = std::make_error_code(std::errc::address_in_use);
|
||||
|
||||
boost::hash<std::error_code> hasher;
|
||||
|
||||
BOOST_TEST(hasher(err1a) == hasher(err1a));
|
||||
BOOST_TEST(hasher(err1a) == hasher(err1b));
|
||||
BOOST_TEST(hasher(err1a) != hasher(err2));
|
||||
}
|
||||
|
||||
void test_error_condition()
|
||||
{
|
||||
std::error_condition err1a = std::make_error_condition(std::errc::address_family_not_supported);
|
||||
std::error_condition err1b = std::make_error_condition(std::errc::address_family_not_supported);
|
||||
std::error_condition err2 = std::make_error_condition(std::errc::address_in_use);
|
||||
|
||||
boost::hash<std::error_condition> hasher;
|
||||
|
||||
BOOST_TEST(hasher(err1a) == hasher(err1a));
|
||||
BOOST_TEST(hasher(err1a) == hasher(err1b));
|
||||
BOOST_TEST(hasher(err1a) != hasher(err2));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR)
|
||||
test_error_code();
|
||||
test_error_condition();
|
||||
#else
|
||||
BOOST_LIGHTWEIGHT_TEST_OSTREAM << "<system_error> not available." << std::endl;
|
||||
#endif
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user