3 Defines `boost::hana::hash`.
5 @copyright Jason Rice 2016
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_HASH_HPP
11 #define BOOST_HANA_HASH_HPP
13 #include <boost/hana/fwd/hash.hpp>
15 #include <boost/hana/concept/hashable.hpp>
16 #include <boost/hana/concept/integral_constant.hpp>
17 #include <boost/hana/config.hpp>
18 #include <boost/hana/core/dispatch.hpp>
19 #include <boost/hana/fwd/integral_constant.hpp>
20 #include <boost/hana/type.hpp>
22 #include <type_traits>
25 namespace boost { namespace hana {
28 constexpr auto hash_t::operator()(X const& x) const {
29 using Tag = typename hana::tag_of<X>::type;
30 using Hash = BOOST_HANA_DISPATCH_IF(hash_impl<Tag>,
31 hana::Hashable<Tag>::value
34 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
35 static_assert(hana::Hashable<Tag>::value,
36 "hana::hash(x) requires 'x' to be Hashable");
39 return Hash::apply(x);
43 template <typename Tag, bool condition>
44 struct hash_impl<Tag, when<condition>> : default_ {
46 static constexpr auto apply(X const&) = delete;
50 template <typename T, typename = void>
51 struct hash_integral_helper;
53 template <typename Member, typename T>
54 struct hash_integral_helper<Member T::*> {
56 static constexpr auto apply(X const&) {
57 return hana::type_c<hana::integral_constant<Member T::*, X::value>>;
62 struct hash_integral_helper<T,
63 typename std::enable_if<std::is_signed<T>::value>::type
66 static constexpr auto apply(X const&) {
67 constexpr signed long long x = X::value;
68 return hana::type_c<hana::integral_constant<signed long long, x>>;
73 struct hash_integral_helper<T,
74 typename std::enable_if<std::is_unsigned<T>::value>::type
77 static constexpr auto apply(X const&) {
78 constexpr unsigned long long x = X::value;
79 return hana::type_c<hana::integral_constant<unsigned long long, x>>;
84 struct hash_integral_helper<bool> {
86 static constexpr auto apply(X const&) {
87 return hana::type_c<hana::integral_constant<bool, X::value>>;
92 struct hash_integral_helper<char> {
94 static constexpr auto apply(X const&) {
95 using T = std::conditional<std::is_signed<char>::value,
96 signed long long, unsigned long long
98 constexpr T x = X::value;
99 return hana::type_c<hana::integral_constant<T, x>>;
104 template <typename Tag>
105 struct hash_impl<Tag, when<hana::IntegralConstant<Tag>::value>> {
106 template <typename X>
107 static constexpr auto apply(X const& x) {
108 using T = typename std::remove_cv<decltype(X::value)>::type;
109 return detail::hash_integral_helper<T>::apply(x);
112 }} // end namespace boost::hana
114 #endif // !BOOST_HANA_HASH_HPP