Support typeindex in hash. Refs #4756.

[SVN r68145]
This commit is contained in:
Daniel James
2011-01-14 03:13:39 +00:00
parent ad614b3d5f
commit 7dc95d044d
4 changed files with 103 additions and 0 deletions

View File

@@ -406,6 +406,29 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
</method>
</struct-specialization>
<struct-specialization name="hash">
<template></template>
<specialization>
<template-arg>std::type_index</template-arg>
</specialization>
<method name="operator()" cv="const">
<type>std::size_t</type>
<parameter name="val">
<paramtype>std::type_index</paramtype>
</parameter>
<returns>
<para><code>val.hash_code()</code></para>
</returns>
<throws><para>Doesn't throw</para></throws>
</method>
<notes>
<para>
Only available if it's in your standard library and Boost.Config
is aware of it.
</para>
</notes>
</struct-specialization>
<free-function-group name="Support functions (Boost extension).">
<!--
@@ -716,6 +739,11 @@ for(; first != last; ++first)
<parameter name="val"><paramtype>std::complex&lt;T&gt; const&amp;</paramtype></parameter>
</signature>
<signature>
<type>std::size_t</type>
<parameter name="val"><paramtype>std::type_index</paramtype></parameter>
</signature>
<description><para>
Generally shouldn't be called directly by users, instead they should use
<classname>boost::hash</classname>, <functionname>boost::hash_range</functionname>
@@ -798,6 +826,12 @@ return seed;</programlisting></entry>
</entry>
<entry>When <code>T</code> is a built in type and <code>val.imag() == 0</code>, the result is equal to <code>hash_value(val.real())</code>. Otherwise an unspecified value, except that equal arguments shall yield the same result.</entry>
</row>
<row>
<entry>
<code>std::type_index</code>
</entry>
<entry><code>val.hash_code()</code></entry>
</row>
</tbody>
</tgroup>
</informaltable>

View File

@@ -24,6 +24,10 @@
#include <boost/type_traits/is_pointer.hpp>
#endif
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
#include <typeindex>
#endif
#if BOOST_WORKAROUND(__GNUC__, < 3) \
&& !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
#define BOOST_HASH_CHAR_TRAITS string_char_traits
@@ -87,6 +91,11 @@ namespace boost
std::size_t hash_value(
std::basic_string<Ch, std::BOOST_HASH_CHAR_TRAITS<Ch>, A> const&);
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
template <class Ch, class A>
std::size_t hash_value(std::type_index);
#endif
// Implementation
namespace hash_detail
@@ -331,6 +340,13 @@ namespace boost
return boost::hash_detail::float_hash_value(v);
}
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
std::size_t hash_value(std::type_index v)
{
return v.hash_code();
}
#endif
//
// boost::hash
//
@@ -435,6 +451,10 @@ namespace boost
BOOST_HASH_SPECIALIZE(boost::ulong_long_type)
#endif
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
BOOST_HASH_SPECIALIZE(std::type_index)
#endif
#undef BOOST_HASH_SPECIALIZE
#undef BOOST_HASH_SPECIALIZE_REF

View File

@@ -40,6 +40,7 @@ test-suite functional/hash
[ run hash_set_test.cpp ]
[ run hash_map_test.cpp ]
[ run hash_complex_test.cpp ]
[ run hash_type_index_test.cpp ]
[ run link_test.cpp link_test_2.cpp ]
[ run link_ext_test.cpp link_no_ext_test.cpp ]
[ run extensions_hpp_test.cpp ]

View File

@@ -0,0 +1,48 @@
// Copyright 2011 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/config.hpp>
#include <boost/detail/lightweight_test.hpp>
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
#include <typeindex>
void test_type_index() {
std::type_index int_index = typeid(int);
std::type_index int2_index = typeid(int);
std::type_index char_index = typeid(char);
HASH_NAMESPACE::hash<std::type_index> hasher;
BOOST_TEST(hasher(int_index) == int_index.hash_code());
BOOST_TEST(hasher(int_index) == int2_index.hash_code());
BOOST_TEST(hasher(char_index) == char_index.hash_code());
BOOST_TEST(HASH_NAMESPACE::hash_value(int_index) == int_index.hash_code());
BOOST_TEST(HASH_NAMESPACE::hash_value(int_index) == int2_index.hash_code());
BOOST_TEST(HASH_NAMESPACE::hash_value(char_index) == char_index.hash_code());
BOOST_TEST(hasher(int_index) == hasher(int2_index));
BOOST_TEST(hasher(int_index) != hasher(char_index));
}
#endif
int main()
{
#if !defined(BOOST_NO_0X_HDR_TYPEINDEX)
test_type_index();
#else
std::cout<<"<type_index> not available."<<std::endl;
#endif
return boost::report_errors();
}