hash_variant.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //-----------------------------------------------------------------------------
  2. // boost variant/detail/hash_variant.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2011
  7. // Antony Polukhin
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_HASH_VARIANT_FUNCTION_HPP
  13. #define BOOST_HASH_VARIANT_FUNCTION_HPP
  14. #if defined(_MSC_VER)
  15. # pragma once
  16. #endif
  17. #include <boost/variant/variant_fwd.hpp>
  18. #include <boost/variant/static_visitor.hpp>
  19. #include <boost/variant/apply_visitor.hpp>
  20. #include <boost/functional/hash_fwd.hpp>
  21. namespace boost {
  22. namespace detail { namespace variant {
  23. struct variant_hasher: public boost::static_visitor<std::size_t> {
  24. template <class T>
  25. std::size_t operator()(T const& val) const {
  26. boost::hash<T> hasher;
  27. return hasher(val);
  28. }
  29. };
  30. }}
  31. template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
  32. std::size_t hash_value(variant< BOOST_VARIANT_ENUM_PARAMS(T) > const& val) {
  33. std::size_t seed = boost::apply_visitor(detail::variant::variant_hasher(), val);
  34. hash_combine(seed, val.which());
  35. return seed;
  36. }
  37. }
  38. #endif