hash.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*!
  2. @file
  3. Defines `boost::hana::hash`.
  4. @copyright Jason Rice 2016
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_HASH_HPP
  9. #define BOOST_HANA_HASH_HPP
  10. #include <boost/hana/fwd/hash.hpp>
  11. #include <boost/hana/concept/hashable.hpp>
  12. #include <boost/hana/concept/integral_constant.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/fwd/integral_constant.hpp>
  16. #include <boost/hana/type.hpp>
  17. #include <type_traits>
  18. BOOST_HANA_NAMESPACE_BEGIN
  19. //! @cond
  20. template <typename X>
  21. constexpr auto hash_t::operator()(X const& x) const {
  22. using Tag = typename hana::tag_of<X>::type;
  23. using Hash = BOOST_HANA_DISPATCH_IF(hash_impl<Tag>,
  24. hana::Hashable<Tag>::value
  25. );
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(hana::Hashable<Tag>::value,
  28. "hana::hash(x) requires 'x' to be Hashable");
  29. #endif
  30. return Hash::apply(x);
  31. }
  32. //! @endcond
  33. template <typename Tag, bool condition>
  34. struct hash_impl<Tag, when<condition>> : default_ {
  35. template <typename X>
  36. static constexpr auto apply(X const&) = delete;
  37. };
  38. namespace detail {
  39. template <typename T, typename = void>
  40. struct hash_integral_helper;
  41. template <typename Member, typename T>
  42. struct hash_integral_helper<Member T::*> {
  43. template <typename X>
  44. static constexpr auto apply(X const&) {
  45. return hana::type_c<hana::integral_constant<Member T::*, X::value>>;
  46. }
  47. };
  48. template <typename T>
  49. struct hash_integral_helper<T,
  50. typename std::enable_if<std::is_signed<T>::value>::type
  51. > {
  52. template <typename X>
  53. static constexpr auto apply(X const&) {
  54. constexpr signed long long x = X::value;
  55. return hana::type_c<hana::integral_constant<signed long long, x>>;
  56. }
  57. };
  58. template <typename T>
  59. struct hash_integral_helper<T,
  60. typename std::enable_if<std::is_unsigned<T>::value>::type
  61. > {
  62. template <typename X>
  63. static constexpr auto apply(X const&) {
  64. constexpr unsigned long long x = X::value;
  65. return hana::type_c<hana::integral_constant<unsigned long long, x>>;
  66. }
  67. };
  68. template <>
  69. struct hash_integral_helper<bool> {
  70. template <typename X>
  71. static constexpr auto apply(X const&) {
  72. return hana::type_c<hana::integral_constant<bool, X::value>>;
  73. }
  74. };
  75. template <>
  76. struct hash_integral_helper<char> {
  77. template <typename X>
  78. static constexpr auto apply(X const&) {
  79. using T = std::conditional<std::is_signed<char>::value,
  80. signed long long, unsigned long long
  81. >::type;
  82. constexpr T x = X::value;
  83. return hana::type_c<hana::integral_constant<T, x>>;
  84. }
  85. };
  86. }
  87. template <typename Tag>
  88. struct hash_impl<Tag, when<hana::IntegralConstant<Tag>::value>> {
  89. template <typename X>
  90. static constexpr auto apply(X const& x) {
  91. using T = typename std::remove_cv<decltype(X::value)>::type;
  92. return detail::hash_integral_helper<T>::apply(x);
  93. }
  94. };
  95. BOOST_HANA_NAMESPACE_END
  96. #endif // !BOOST_HANA_HASH_HPP