forked from boostorg/container_hash
Hash: Quick attempt at supporting enums.
Thanks to Filip Konvička. [SVN r80139]
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
#include <boost/functional/hash/detail/hash_float.hpp>
|
||||
#include <string>
|
||||
#include <boost/limits.hpp>
|
||||
#include <boost/type_traits/is_enum.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
|
||||
#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||
#include <boost/type_traits/is_pointer.hpp>
|
||||
@@ -90,6 +92,10 @@ namespace boost
|
||||
template <typename T>
|
||||
typename boost::hash_detail::ulong_numbers<T>::type hash_value(T);
|
||||
|
||||
template <typename T>
|
||||
typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
|
||||
hash_value(T);
|
||||
|
||||
#if !BOOST_WORKAROUND(__DMC__, <= 0x848)
|
||||
template <class T> std::size_t hash_value(T* const&);
|
||||
#else
|
||||
@@ -179,6 +185,13 @@ namespace boost
|
||||
return hash_detail::hash_value_unsigned(v);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename boost::enable_if<boost::is_enum<T>, std::size_t>::type
|
||||
hash_value(T v)
|
||||
{
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
|
||||
// Implementation by Alberto Barbati and Dave Harris.
|
||||
#if !BOOST_WORKAROUND(__DMC__, <= 0x848)
|
||||
template <class T> std::size_t hash_value(T* const& v)
|
||||
|
@@ -22,6 +22,7 @@ test-suite functional/hash
|
||||
[ run hash_fwd_test_1.cpp ]
|
||||
[ run hash_fwd_test_2.cpp ]
|
||||
[ run hash_number_test.cpp ]
|
||||
[ run hash_enum_test.cpp ]
|
||||
[ run hash_pointer_test.cpp ]
|
||||
[ run hash_function_pointer_test.cpp ]
|
||||
[ run hash_float_test.cpp ]
|
||||
|
59
test/hash_enum_test.cpp
Normal file
59
test/hash_enum_test.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
// Copyright 2012 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"
|
||||
|
||||
#ifdef TEST_STD_INCLUDES
|
||||
# include <functional>
|
||||
#else
|
||||
# include <boost/functional/hash.hpp>
|
||||
#endif
|
||||
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/type_traits/is_base_and_derived.hpp>
|
||||
|
||||
namespace test {
|
||||
enum enum_override { enum_override1, enum_override2 };
|
||||
std::size_t hash_value(enum_override x) { return -896532; }
|
||||
|
||||
enum enum1 { enum1a };
|
||||
enum enum2 { enum2a, enum2b };
|
||||
enum enum3 { enum3a = 574, enum3b };
|
||||
enum enum4 { enum4a = -12574, enum4b };
|
||||
}
|
||||
|
||||
int main() {
|
||||
boost::hash<test::enum1> hash1;
|
||||
boost::hash<test::enum2> hash2;
|
||||
boost::hash<test::enum3> hash3;
|
||||
boost::hash<test::enum4> hash4;
|
||||
|
||||
BOOST_TEST(hash1(test::enum1a) == hash1(test::enum1a));
|
||||
|
||||
BOOST_TEST(hash2(test::enum2a) == hash2(test::enum2a));
|
||||
BOOST_TEST(hash2(test::enum2a) != hash2(test::enum2b));
|
||||
BOOST_TEST(hash2(test::enum2b) == hash2(test::enum2b));
|
||||
|
||||
BOOST_TEST(hash3(test::enum3a) == hash3(test::enum3a));
|
||||
BOOST_TEST(hash3(test::enum3a) != hash3(test::enum3b));
|
||||
BOOST_TEST(hash3(test::enum3b) == hash3(test::enum3b));
|
||||
|
||||
BOOST_TEST(hash4(test::enum4a) == hash4(test::enum4a));
|
||||
BOOST_TEST(hash4(test::enum4a) != hash4(test::enum4b));
|
||||
BOOST_TEST(hash4(test::enum4b) == hash4(test::enum4b));
|
||||
|
||||
boost::hash<test::enum_override> hash_override;
|
||||
|
||||
BOOST_TEST(hash_override(test::enum_override1) ==
|
||||
hash_override(test::enum_override1));
|
||||
BOOST_TEST(hash_override(test::enum_override1) ==
|
||||
hash_override(test::enum_override2));
|
||||
BOOST_TEST(hash_override(test::enum_override1) ==
|
||||
hash_override(test::enum_override1));
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user