|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r80139 - in trunk: boost/functional/hash libs/functional/hash/test
From: dnljms_at_[hidden]
Date: 2012-08-22 07:40:29
Author: danieljames
Date: 2012-08-22 07:40:27 EDT (Wed, 22 Aug 2012)
New Revision: 80139
URL: http://svn.boost.org/trac/boost/changeset/80139
Log:
Hash: Quick attempt at supporting enums.
Thanks to Filip KonviÄka.
Added:
trunk/libs/functional/hash/test/hash_enum_test.cpp (contents, props changed)
Text files modified:
trunk/boost/functional/hash/hash.hpp | 13 +++++++++++++
trunk/libs/functional/hash/test/Jamfile.v2 | 1 +
2 files changed, 14 insertions(+), 0 deletions(-)
Modified: trunk/boost/functional/hash/hash.hpp
==============================================================================
--- trunk/boost/functional/hash/hash.hpp (original)
+++ trunk/boost/functional/hash/hash.hpp 2012-08-22 07:40:27 EDT (Wed, 22 Aug 2012)
@@ -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 @@
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 @@
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)
Modified: trunk/libs/functional/hash/test/Jamfile.v2
==============================================================================
--- trunk/libs/functional/hash/test/Jamfile.v2 (original)
+++ trunk/libs/functional/hash/test/Jamfile.v2 2012-08-22 07:40:27 EDT (Wed, 22 Aug 2012)
@@ -22,6 +22,7 @@
[ 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 ]
Added: trunk/libs/functional/hash/test/hash_enum_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/functional/hash/test/hash_enum_test.cpp 2012-08-22 07:40:27 EDT (Wed, 22 Aug 2012)
@@ -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();
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk