Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r68145 - in trunk: boost/functional/hash libs/functional/hash/doc libs/functional/hash/test
From: dnljms_at_[hidden]
Date: 2011-01-13 22:13:40


Author: danieljames
Date: 2011-01-13 22:13:39 EST (Thu, 13 Jan 2011)
New Revision: 68145
URL: http://svn.boost.org/trac/boost/changeset/68145

Log:
Support typeindex in hash. Refs #4756.
Added:
   trunk/libs/functional/hash/test/hash_type_index_test.cpp (contents, props changed)
Text files modified:
   trunk/boost/functional/hash/hash.hpp | 20 ++++++++++++++++++++
   trunk/libs/functional/hash/doc/ref.xml | 34 ++++++++++++++++++++++++++++++++++
   trunk/libs/functional/hash/test/Jamfile.v2 | 1 +
   3 files changed, 55 insertions(+), 0 deletions(-)

Modified: trunk/boost/functional/hash/hash.hpp
==============================================================================
--- trunk/boost/functional/hash/hash.hpp (original)
+++ trunk/boost/functional/hash/hash.hpp 2011-01-13 22:13:39 EST (Thu, 13 Jan 2011)
@@ -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 @@
     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 @@
         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 @@
     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
 

Modified: trunk/libs/functional/hash/doc/ref.xml
==============================================================================
--- trunk/libs/functional/hash/doc/ref.xml (original)
+++ trunk/libs/functional/hash/doc/ref.xml 2011-01-13 22:13:39 EST (Thu, 13 Jan 2011)
@@ -406,6 +406,29 @@
         </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 @@
           <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 @@
                   </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>

Modified: trunk/libs/functional/hash/test/Jamfile.v2
==============================================================================
--- trunk/libs/functional/hash/test/Jamfile.v2 (original)
+++ trunk/libs/functional/hash/test/Jamfile.v2 2011-01-13 22:13:39 EST (Thu, 13 Jan 2011)
@@ -40,6 +40,7 @@
         [ 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 ]

Added: trunk/libs/functional/hash/test/hash_type_index_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/functional/hash/test/hash_type_index_test.cpp 2011-01-13 22:13:39 EST (Thu, 13 Jan 2011)
@@ -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();
+}


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