Telnet++  3.1.0.2
A C++ library for interacting with Telnet streams
All Classes Namespaces Functions Variables Enumerations Pages
hash.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <type_traits>
5 #include <utility>
6 
7 namespace telnetpp::detail {
8 
9 constexpr void hash_combine(std::size_t & /*seed*/)
10 {
11 }
12 
13 template <typename Field, typename... Fields>
14 constexpr void hash_combine(
15  std::size_t &seed, Field &&field, Fields &&...fields)
16 {
17  using field_hash_type = std::remove_const_t<std::remove_reference_t<Field>>;
18 
19  std::hash<field_hash_type> hasher;
20  seed ^= hasher(field) + 0x9E3779B9 + (seed << 6) + (seed >> 2);
21  hash_combine(seed, std::forward<Fields>(fields)...);
22 }
23 
24 } // namespace telnetpp::detail
25 
26 #define TELNETPP_MAKE_HASHABLE(type, ...) \
27  namespace std { \
28  template <> \
29  struct hash<type> \
30  { \
31  std::size_t operator()(const type &t) const \
32  { \
33  std::size_t ret = 0; \
34  telnetpp::detail::hash_combine(ret, __VA_ARGS__); \
35  return ret; \
36  } \
37  }; \
38  }
39 
40 #define TELNETPP_MAKE_INTRUSIVE_HASH(type) \
41  namespace std { \
42  template <> \
43  struct hash<type> \
44  { \
45  std::size_t operator()(const type &t) const \
46  { \
47  std::size_t ret = 0; \
48  t.hash_combine(ret); \
49  return ret; \
50  } \
51  }; \
52  }