-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_variant
328 lines (291 loc) · 13.7 KB
/
static_variant
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_CONTAINER_STATIC_VARIANT_HPP
#define GTL_CONTAINER_STATIC_VARIANT_HPP
// Summary: A static_variant class that can contain any one of its listed template types. [wip]
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the static_variant is misused.
# define GTL_STATIC_VARIANT_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_STATIC_VARIANT_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
namespace gtl {
// Forward declare class to allow a unique definition of operator new using it as a parameter.
template <typename first_type, typename... remaining_types>
class static_variant;
}
namespace {
// Operator new requires the size_t type for its size argument.
using size_t = decltype(sizeof(0));
}
/// @brief Custom placement operator new to avoid including the (massive) <new> header.
/// @tparam types The types contained within the variant.
/// @param size The size of the data to placement new on.
/// @param pointer The pointer of the data to placement new on.
/// @param unused_type_tag An unused type tag used to make this placement new operator function unique.
template <typename... types>
inline void* operator new(size_t size, void* pointer, gtl::static_variant<types...>* unused_type_tag) {
static_cast<void>(size);
static_cast<void>(unused_type_tag);
return pointer;
}
/// @brief Custom placement operator delete to avoid compilers complaing about potential memory leaks.
/// @tparam types The types contained within the variant.
/// @param data The pointer of the data to placement delete on.
/// @param pointer The pointer of the data to placement delete on.
/// @param unused_type_tag An unused type tag used to make this placement delete operator function unique.
template <typename... types>
inline void operator delete(void* data, void* pointer, gtl::static_variant<types...>* unused_type_tag) {
static_cast<void>(data);
static_cast<void>(pointer);
static_cast<void>(unused_type_tag);
}
namespace gtl {
template <typename first_type, typename... remaining_types>
class static_variant final {
private:
/// @brief By making the templated static_variant class friends with all other static_variant classes private members can be accessed in all member functions.
template <typename type, typename... types>
friend class static_variant;
private:
template <typename type>
struct type_identifier_generator final {
/// @brief The actual bit of unique type information.
constexpr static const auto identifier = [](){};
};
private:
/// @brief Recursive function to iterate over the types to find the largest one.
constexpr static unsigned long long int get_largest_type_size() {
if constexpr (sizeof...(remaining_types) == 0) {
return sizeof(first_type);
}
else {
if constexpr (sizeof(first_type) > static_variant<remaining_types...>::get_largest_type_size()) {
return sizeof(first_type);
}
else {
return static_variant<remaining_types...>::get_largest_type_size();
}
}
}
template <typename lhs_type, class rhs_type>
struct is_same_type {
constexpr static const bool value = false;
};
template <typename type>
struct is_same_type<type, type> {
constexpr static const bool value = true;
};
private:
unsigned long long int active_type_id = 0;
#if defined(_MSC_VER)
// Work around an MSVC bug.
alignas(static_variant::get_largest_type_size()) unsigned char data[get_largest_type_size()] = {};
#else
alignas(static_variant::get_largest_type_size()) unsigned char data[static_variant::get_largest_type_size()] = {};
#endif
public:
~static_variant() {
if (!this->empty()) {
this->destruct<first_type, remaining_types...>();
}
}
constexpr static_variant() = default;
template <typename type>
constexpr static_variant(const type& value) {
static_assert(is_same_type<type, first_type>::value || (is_same_type<type, remaining_types>::value || ...), "Value type is not in the list of valid variant types.");
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
new (&this->data[0], static_cast<static_variant*>(nullptr)) type(value);
}
constexpr static_variant(const static_variant& other) {
this->construct_copy<first_type, remaining_types...>(other);
}
constexpr static_variant(static_variant&& other) {
this->construct_move<first_type, remaining_types...>(other);
}
constexpr static_variant& operator=(const static_variant& other) {
if ((!this->empty()) && (!other.empty())) {
// Both valid.
if (this->active_type_id != other.active_type_id) {
this->destruct<first_type, remaining_types...>();
this->assign_copy<first_type, remaining_types...>(other);
}
else {
this->destruct<first_type, remaining_types...>();
this->construct_copy<first_type, remaining_types...>(other);
}
}
else if (!other.empty()) {
// Only other valid.
this->construct_copy<first_type, remaining_types...>(other);
}
else {
// Only this valid.
this->destruct<first_type, remaining_types...>();
}
return *this;
}
constexpr static_variant& operator=(static_variant&& other) {
if ((!this->empty()) && (!other.empty())) {
// Both valid.
if (this->active_type_id != other.active_type_id) {
this->destruct<first_type, remaining_types...>();
this->assign_move<first_type, remaining_types...>(static_cast<static_variant&&>(other));
}
else {
this->destruct<first_type, remaining_types...>();
this->construct_move<first_type, remaining_types...>(static_cast<static_variant&&>(other));
}
}
else if (!other.empty()) {
// Only other valid.
this->construct_move<first_type, remaining_types...>(static_cast<static_variant&&>(other));
}
else {
// Only this valid.
this->destruct<first_type, remaining_types...>();
}
return *this;
}
private:
template <typename type, typename... types>
constexpr void construct_copy(const static_variant& other) {
if (other.is<type>()) {
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
type& other_data = *reinterpret_cast<type*>(&other.data[0]);
new (&this->data[0], static_cast<static_variant*>(nullptr)) type(other_data);
}
else {
if constexpr (sizeof...(types) != 0) {
this->construct_copy<types...>(other);
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to copy construct variants with no matching type.");
}
}
}
template <typename type, typename... types>
constexpr void construct_move(static_variant&& other) {
if (other.is<type>()) {
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
type& other_data = reinterpret_cast<type&>(*reinterpret_cast<type*>(&other.data[0]));
new (&this->data[0], static_cast<static_variant*>(nullptr)) type(static_cast<type&&>(other_data));
}
else {
if constexpr (sizeof...(types) != 0) {
this->construct_move<types...>(static_cast<static_variant&&>(other));
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to move construct variants with no matching type.");
}
}
}
template <typename type, typename... types>
constexpr void assign_copy(const static_variant& other) {
if (other.is<type>()) {
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
type& thus_data = *reinterpret_cast<type*>(&this->data[0]);
type& other_data = *reinterpret_cast<type*>(&other.data[0]);
thus_data = other_data;
}
else {
if constexpr (sizeof...(types) != 0) {
this->assign_copy<types...>(other);
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to copy between variants with no matching type.");
}
}
}
template <typename type, typename... types>
constexpr void assign_move(static_variant&& other) {
if (other.is<type>()) {
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
type& thus_data = reinterpret_cast<type&>(*reinterpret_cast<type*>(&this->data[0]));
type& other_data = reinterpret_cast<type&>(*reinterpret_cast<type*>(&other.data[0]));
thus_data = static_cast<type&&>(other_data);
}
else {
if constexpr (sizeof...(types) != 0) {
this->assign_move<types...>(static_cast<static_variant&&>(other));
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to move between variants with no matching type.");
}
}
}
template <typename type, typename... types>
constexpr void destruct() {
if (this->is<type>()) {
this->active_type_id = 0;
reinterpret_cast<type*>(&this->data[0])->~type(); // lgtm [cpp/incorrect-string-type-conversion]
}
else {
if constexpr (sizeof...(types) != 0) {
this->destruct<types...>();
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to destruct variant with no matching type.");
}
}
}
template <typename return_type, typename type, typename... types>
constexpr return_type cast() const {
if (this->is<type>()) {
return static_cast<return_type>(*reinterpret_cast<const type*>(&this->data[0]));
}
else {
if constexpr (sizeof...(types) != 0) {
return this->cast<return_type, types...>();
}
else {
GTL_STATIC_VARIANT_ASSERT(false, "Failed to cast variant with no matching type.");
return {};
}
}
}
public:
constexpr bool empty() const {
return (this->active_type_id == 0);
}
template <typename type>
constexpr bool is() const {
return (this->active_type_id == reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier));
}
template <typename type>
constexpr type as() const {
GTL_STATIC_VARIANT_ASSERT(!this->empty(), "Attempting to get the value of an empty variant.");
return this->cast<type, first_type, remaining_types...>();
}
template <typename type>
constexpr const type& get() const {
GTL_STATIC_VARIANT_ASSERT(!this->empty(), "Attempting to get the value of an empty variant.");
return reinterpret_cast<const type&>(*reinterpret_cast<const type*>(&this->data[0]));
}
template <typename type>
constexpr void set(const type& value) {
if (!this->empty()) {
this->destruct<first_type, remaining_types...>();
}
this->active_type_id = reinterpret_cast<const unsigned long long int>(&static_variant<void*>::type_identifier_generator<type>::identifier);
new (&this->data[0], static_cast<static_variant*>(nullptr)) type(value);
}
};
}
#undef GTL_STATIC_VARIANT_ASSERT
#endif // GTL_CONTAINER_STATIC_VARIANT_HPP