-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant.hpp
More file actions
305 lines (279 loc) · 10.9 KB
/
variant.hpp
File metadata and controls
305 lines (279 loc) · 10.9 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Boost Software License - Version 1.0 - August 17th, 2003
© 2021 Alecto Irene Perez
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by this
license (the "Software") to use, reproduce, display, distribute, execute, and
transmit the Software, and to prepare derivative works of the Software, and to
permit third-parties to whom the Software is furnished to do so, all subject to
the following:
The copyright notices in the Software and this entire statement, including the
above license grant, this restriction and the following disclaimer, must be
included in all copies of the Software, in whole or in part, and all derivative
works of the Software, especially those created in whole or in part by Deep
Neural Networks, Language Models, or other such programs advertised as "AI" or
as "Artificial Intelligence" or as "Machine Learning", either with or without
human input or intervention, unless such copies or derivative works are solely
in the form of machine-executable object code generated by a source language
processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES
OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef RECURSIVE_VARIANT_AUTHORITY_VARIANT_HPP
#define RECURSIVE_VARIANT_AUTHORITY_VARIANT_HPP
#include <cstdint>
#include <variant>
namespace rva {
/**
* @brief replace is a template type used to implement replace_t. It provides
* member, a using declaration named `type`.
*
* @tparam T the type to transform.
* @tparam Find the type to find
* @tparam Replace the type to replace it with.
*/
template <class T, class Find, class Replace>
struct replace;
/**
* @brief replace is a template that takes a type T (which itself might be a
* template), and replaces all instances of *Find* with *Replace*. For example:
*
* - `replace_t<char, char, int>` -> `int`
* - `replace_t<std::vector<char>, char, int>` -> `std::vector<int>`
*
* @tparam T the type to transform.
* @tparam Find the type to find
* @tparam Replace the type to replace it with.
*/
template <class T, class Find, class Replace>
using replace_t = typename replace<T, Find, Replace>::type;
struct self_t {};
// See: https://en.cppreference.com/w/cpp/utility/variant
template <class... T>
class variant : public std::variant<replace_t<T, self_t, variant<T...>>...> {
public:
using base_type = std::variant<replace_t<T, self_t, variant<T...>>...>;
using base_type::base_type;
// Observers
using base_type::index;
using base_type::valueless_by_exception;
// Modifiers
using base_type::operator=;
using base_type::emplace;
using base_type::swap;
variant() = default;
variant(variant const&) = default;
variant(variant&&) = default;
variant& operator=(variant const&) = default;
variant& operator=(variant&&) = default;
constexpr void swap(variant& other) noexcept(std::is_nothrow_swappable_v<base_type>) {
base_type::swap(other);
}
constexpr base_type& get_base() & noexcept { return *this; }
constexpr base_type const& get_base() const& noexcept { return *this; }
constexpr base_type&& get_base() && noexcept { return *this; }
constexpr base_type const&& get_base() const&& noexcept { return *this; }
constexpr base_type* get_pointer_to_base() noexcept { return this; }
constexpr base_type const* get_pointer_to_base() const noexcept {
return this;
}
auto operator<=>(variant const&) const = default;
bool operator==(variant const&) const = default;
};
// See: https://en.cppreference.com/w/cpp/utility/variant/visit
template <class Visitor, class... Variants>
constexpr decltype(auto) visit(Visitor&& visitor, Variants&&... variants) {
return std::visit(
std::forward<Visitor>(visitor),
std::forward<Variants>(variants).get_base()...);
}
template <class R, class Visitor, class... Variants>
constexpr R visit(Visitor&& visitor, Variants&&... variants) {
return std::visit<R>(
std::forward<Visitor>(visitor),
std::forward<Variants>(variants).get_base()...);
}
// See: https://en.cppreference.com/w/cpp/utility/variant/get
template <std::size_t I, class... Types>
constexpr decltype(auto) get(rva::variant<Types...>& v) {
return std::get<I>(std::forward<decltype(v)>(v).get_base());
}
template <std::size_t I, class... Types>
constexpr decltype(auto) get(rva::variant<Types...>&& v) {
return std::get<I>(std::forward<decltype(v)>(v).get_base());
}
template <std::size_t I, class... Types>
constexpr decltype(auto) get(const rva::variant<Types...>& v) {
return std::get<I>(std::forward<decltype(v)>(v).get_base());
}
template <std::size_t I, class... Types>
constexpr decltype(auto) get(const rva::variant<Types...>&& v) {
return std::get<I>(std::forward<decltype(v)>(v).get_base());
}
template <class T, class... Types>
constexpr T& get(rva::variant<Types...>& v) {
return std::get<T>(std::forward<decltype(v)>(v).get_base());
}
template <class T, class... Types>
constexpr T&& get(rva::variant<Types...>&& v) {
return std::get<T>(std::forward<decltype(v)>(v).get_base());
}
template <class T, class... Types>
constexpr const T& get(const rva::variant<Types...>& v) {
return std::get<T>(std::forward<decltype(v)>(v).get_base());
}
template <class T, class... Types>
constexpr const T&& get(const rva::variant<Types...>&& v) {
return std::get<T>(std::forward<decltype(v)>(v).get_base());
}
// See: https://en.cppreference.com/w/cpp/utility/variant/get_if
template <std::size_t I, class... Types>
constexpr auto* get_if(rva::variant<Types...>* pv) noexcept {
return std::get_if<I>(pv->get_pointer_to_base());
}
template <std::size_t I, class... Types>
constexpr auto const* get_if(const rva::variant<Types...>* pv) noexcept {
return std::get_if<I>(pv->get_pointer_to_base());
}
template <class T, class... Types>
constexpr auto* get_if(rva::variant<Types...>* pv) noexcept {
return std::get_if<T>(pv->get_pointer_to_base());
}
template <class T, class... Types>
constexpr auto const* get_if(const rva::variant<Types...>* pv) noexcept {
return std::get_if<T>(pv->get_pointer_to_base());
}
template <class T, class... Types>
constexpr bool holds_alternative(const rva::variant<Types...>& v) noexcept {
return std::holds_alternative(v.get_base());
}
} // namespace rva
template <class... T>
struct std::hash<rva::variant<T...>> : std::hash<std::variant<T...>> {
using base_type = std::hash<std::variant<T...>>;
using base_type::base_type;
hash() = default;
hash(hash const&) = default;
hash(hash&&) = default;
size_t operator()(rva::variant<T...> const& v) const {
return base_type::operator()(v.get_base());
}
};
template <class... Types>
struct std::variant_size<rva::variant<Types...>>
: std::integral_constant<std::size_t, sizeof...(Types)> {};
template <class... Types>
struct std::variant_size<const rva::variant<Types...>>
: std::integral_constant<std::size_t, sizeof...(Types)> {};
template <std::size_t I, class... Types>
struct std::variant_alternative<I, rva::variant<Types...>>
: std::variant_alternative<I, typename rva::variant<Types...>::base_type> {};
template <std::size_t I, class... Types>
struct std::variant_alternative<I, const rva::variant<Types...>>
: std::variant_alternative<I, typename rva::variant<Types...>::base_type> {};
// Implementation for replace
namespace rva {
template <class T, class Find, class Replace>
struct replace {
using type = T;
};
template <class Find, class Replace>
struct replace<Find, Find, Replace> {
using type = Replace;
};
template <class Find, class Replace>
struct replace<Find*, Find, Replace> {
using type = Replace*;
};
template <class Find, class Replace>
struct replace<Find&, Find, Replace> {
using type = Replace&;
};
template <class Find, class Replace>
struct replace<Find&&, Find, Replace> {
using type = Replace&&;
};
template <class Find, class Replace>
struct replace<Find[], Find, Replace> {
using type = Replace[];
};
template <class Find, class Replace, std::size_t N>
struct replace<Find[N], Find, Replace> {
using type = Replace[N];
};
template <class Find, class Replace>
struct replace<const Find, Find, Replace> {
using type = const Replace;
};
template <class Find, class Replace>
struct replace<const Find*, Find, Replace> {
using type = const Replace*;
};
template <class Find, class Replace>
struct replace<const Find&, Find, Replace> {
using type = const Replace&;
};
template <class Find, class Replace>
struct replace<const Find[], Find, Replace> {
using type = const Replace[];
};
template <class Find, class Replace, std::size_t N>
struct replace<const Find[N], Find, Replace> {
using type = const Replace[N];
};
template <class T, class Find, class Replace>
struct replace<T*, Find, Replace> {
using type = replace_t<T, Find, Replace>*;
};
template <class T, class Find, class Replace>
struct replace<T&, Find, Replace> {
using type = replace_t<T, Find, Replace>&;
};
template <class T, class Find, class Replace>
struct replace<T&&, Find, Replace> {
using type = replace_t<T, Find, Replace>&&;
};
template <class T, class Find, class Replace>
struct replace<T[], Find, Replace> {
using type = replace_t<T, Find, Replace>[];
};
template <class T, class Find, class Replace, std::size_t N>
struct replace<T[N], Find, Replace> {
using type = replace_t<T, Find, Replace>[N];
};
template <class T, class Find, class Replace>
struct replace<const T, Find, Replace> {
using type = replace_t<T, Find, Replace> const;
};
template <class T, class Find, class Replace>
struct replace<const T*, Find, Replace> {
using type = replace_t<T, Find, Replace> const*;
};
template <class T, class Find, class Replace>
struct replace<const T&, Find, Replace> {
using type = replace_t<T, Find, Replace> const&;
};
template <class T, class Find, class Replace>
struct replace<const T[], Find, Replace> {
using type = replace_t<T, Find, Replace> const[];
};
template <class T, class Find, class Replace, std::size_t N>
struct replace<const T[N], Find, Replace> {
using type = replace_t<T, Find, Replace> const[N];
};
template <template <class...> class T, class... Ts, class Find, class Replace>
struct replace<T<Ts...>, Find, Replace> {
using type = T<replace_t<Ts, Find, Replace>...>;
};
// Add shortcut for rva::variant to avoid replacing into instances of an
// rva::variant that's given as a template parameter to another rva::variant
template <class... Ts, class Find, class Replace>
struct replace<rva::variant<Ts...>, Find, Replace> {
using type = rva::variant<Ts...>;
};
} // namespace rva
#endif