summaryrefslogtreecommitdiffstats
path: root/gfx/gl/AutoMappable.h
blob: f93b2ccb5738c661f7bc6e94375ce1fd6b701247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_AUTO_MAPPABLE_H
#define MOZILLA_AUTO_MAPPABLE_H

// Here be dragons.

#include <functional>

namespace mozilla::gfx {

template <class T>
size_t Hash(const T&);

template <class T>
struct StaticStdHasher {
  static auto HashImpl(const T& v) { return std::hash<T>()(v); }
};

template <class T>
struct StaticHasher {
  static auto HashImpl(const T& v) { return v.hash(); }
};
template <class T>
struct StaticHasher<std::optional<T>> {
  static size_t HashImpl(const std::optional<T>& v) {
    if (!v) return 0;
    return Hash(*v);
  }
};
template <>
struct StaticHasher<int> : public StaticStdHasher<int> {};
template <>
struct StaticHasher<bool> : public StaticStdHasher<bool> {};
template <>
struct StaticHasher<float> : public StaticStdHasher<float> {};

template <class T>
size_t Hash(const T& v) {
  return StaticHasher<T>::HashImpl(v);
}

//-
// From Boost:
// https://www.boost.org/doc/libs/1_37_0/doc/html/hash/reference.html#boost.hash_combine

inline size_t HashCombine(size_t seed, const size_t hash) {
  seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  return seed;
}

// -
// See
// https://codereview.stackexchange.com/questions/136770/hashing-a-tuple-in-c17

template <class... Args, size_t... Ids>
size_t HashTupleN(const std::tuple<Args...>& tup,
                  const std::index_sequence<Ids...>&) {
  size_t seed = 0;
  for (const auto& hash : {Hash(std::get<Ids>(tup))...}) {
    seed = HashCombine(seed, hash);
  }
  return seed;
}

template <class... Args>
size_t HashTuple(const std::tuple<Args...>& tup) {
  return HashTupleN(tup, std::make_index_sequence<sizeof...(Args)>());
}

// -

template <class T>
auto MembersEq(const T& a, const T& b) {
  const auto atup = a.Members();
  const auto btup = b.Members();
  return atup == btup;
}

template <class T>
auto MembersLt(const T& a, const T& b) {
  const auto atup = a.Members();
  const auto btup = b.Members();
  return atup == btup;
}

template <class T>
auto MembersHash(const T& a) {
  const auto atup = a.Members();
  return HashTuple(atup);
}

template <class T>
struct MembersHasher final {
  auto operator()(const T& v) const { return v.hash(); }
};

/** E.g.:
struct Foo {
    int i;
    bool b;

   auto Members() const { return std::tie(i, b); }
    INLINE_AUTO_MAPPABLE(Foo)
};
std::unordered_set<T, T::Hasher> easy;
**/
#define INLINE_DERIVE_MEMBERS_EQ(T)                \
  friend bool operator==(const T& a, const T& b) { \
    return mozilla::gfx::MembersEq(a, b);          \
  }                                                \
  friend bool operator!=(const T& a, const T& b) { return !operator==(a, b); }
#define INLINE_AUTO_MAPPABLE(T)                                          \
  friend bool operator<(const T& a, const T& b) {                        \
    return mozilla::gfx::MembersLt(a, b);                                \
  }                                                                      \
  INLINE_DERIVE_MEMBERS_EQ(T)                                            \
  size_t hash() const {                                                  \
    return mozilla::gfx::MembersHash(*reinterpret_cast<const T*>(this)); \
  }                                                                      \
  using Hasher = mozilla::gfx::MembersHasher<T>;

// -

/** E.g.:
```
struct Foo : public AutoMappable<Foo> {
    int i;
    bool b;

    auto Members() const { return std::tie(i, b); }
};
std::unordered_set<T, T::Hasher> easy;
```
`easy.insert({{}, 2, true});`
The initial {} is needed for aggregate initialization of AutoMappable<Foo>.
Use INLINE_AUTO_MAPPABLE if this is too annoying.
**/
template <class T>
struct AutoMappable {
  INLINE_AUTO_MAPPABLE(T)
};

}  // namespace mozilla::gfx

#endif  // MOZILLA_AUTO_MAPPABLE_H