-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_union.h
615 lines (521 loc) · 13.8 KB
/
safe_union.h
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// -*-coding: mule-utf-8-unix; fill-column: 58; -*-
/**
* @file
*
* This file (originally) was a part of public
* https://github.com/lodyagin/types repository.
*
* @author Sergei Lodyagin
* @copyright Copyright (c) 2014, Sergei Lodyagin
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TYPES_SAFE_UNION_H
#define TYPES_SAFE_UNION_H
#include <functional>
#include <typeindex>
#include <stdexcept>
#include <utility>
#include "types/templated_switch.h"
#include "types/traits.h"
#include "types/exception.h"
#include "types/typeinfo.h"
#include "types/strings/mixed.h"
namespace types
{
//! A type-safe union for T...
//! Can also hold nothing.
//! @author Sergei Lodyagin
template<class... T>
class safe_union;
using type_code_t = decltype(type_of<void>::code());
// We don't use std::logic_error because
// it has no const char* constructor in GNU library.
struct logic_error : virtual std::exception {};
struct type_error : types::logic_error {};
namespace union_
{
template<class... T>
struct holder;
template<class T1, class T2>
struct holder<T1, T2>
{
union { T1 t1; T2 t2; };
holder() noexcept {}
template<class... Args>
holder(type_of<T1>, Args&&... args)
: t1(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T2>, Args&&... args)
: t2(std::forward<Args>(args)...)
{}
~holder() {}
operator T1&() { return t1; }
operator T2&() { return t2; }
};
template<class T1, class T2, class T3>
struct holder<T1, T2, T3>
{
union { T1 t1; T2 t2; T3 t3; };
holder() noexcept {}
template<class... Args>
holder(type_of<T1>, Args&&... args)
: t1(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T2>, Args&&... args)
: t2(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T3>, Args&&... args)
: t3(std::forward<Args>(args)...)
{}
~holder() {}
operator T1&() { return t1; }
operator T2&() { return t2; }
operator T3&() { return t3; }
};
template<class T1, class T2, class T3, class T4>
struct holder<T1, T2, T3, T4>
{
union { T1 t1; T2 t2; T3 t3; T4 t4; };
holder() noexcept {}
template<class... Args>
holder(type_of<T1>, Args&&... args)
: t1(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T2>, Args&&... args)
: t2(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T3>, Args&&... args)
: t3(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T4>, Args&&... args)
: t4(std::forward<Args>(args)...)
{}
~holder() {}
operator T1&() { return t1; }
operator T2&() { return t2; }
operator T3&() { return t3; }
operator T4&() { return t4; }
};
template<class T1, class T2, class T3, class T4, class T5>
struct holder<T1, T2, T3, T4, T5>
{
union { T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; };
holder() noexcept {}
template<class... Args>
holder(type_of<T1>, Args&&... args)
: t1(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T2>, Args&&... args)
: t2(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T3>, Args&&... args)
: t3(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T4>, Args&&... args)
: t4(std::forward<Args>(args)...)
{}
template<class... Args>
holder(type_of<T5>, Args&&... args)
: t5(std::forward<Args>(args)...)
{}
~holder() {}
operator T1&() { return t1; }
operator T2&() { return t2; }
operator T3&() { return t3; }
operator T4&() { return t4; }
operator T5&() { return t5; }
};
// ...
// helper functions for switch cases
namespace cases
{
template<class T, class Union, class... Args>
bool constructor(type_code_t type, Union& u, Args&&... args)
{
return switch_case(
type,
type_of<T>::code(),
[&u, &args...]()
{
new(&(T&)u) T(std::forward<Args>(args)...);
}
);
}
template<class T, class Union>
bool copy_constructor(type_code_t type, const Union& src, Union& dst)
{
return switch_case(
type,
type_of<T>::code(),
[&src, &dst]()
{
new(&(T&)dst) T((const T&)src);
}
);
}
template<class T, class Union>
bool move_constructor(type_code_t type, Union& src, Union& dst)
{
return switch_case(
type,
type_of<T>::code(),
[&src, &dst]()
{
new(&(T&)dst) T(std::move((T&)src));
}
);
}
// swap different types
template<class T1, class T2, class U/*, class = void*/>
struct swapper
{
static void swap(U& a, U& b)
{
auto t = std::move((T2&)b);
new(&(T1&)b) T1(std::move((T1&)a));
new(&(T2&)a) T2(std::move(t));
}
};
// the same non-void type - just swap
template<class T, class U>
struct swapper<T, T, U>
{
static void swap(U& a, U& b)
{
using std::swap;
swap((T&)a, (T&)b);
}
};
// swap(void, void)
template<class U>
struct swapper<void, void, U>
{
static void swap(U&, U&) {}
};
// swap(void, T)
template<class T, class U>
struct swapper<void, T, U>
{
static void swap(U& a, U& b)
{
new(&(T&)a) T(std::move((T&)b));
}
};
// swap(T, void)
template<class T, class U>
struct swapper<T, void, U>
{
static void swap(U& a, U& b)
{
new(&(T&)b) T(std::move((T&)a));
}
};
template<class T1, class T2, class Union>
bool swap(type_code_t tc2, Union& a, Union& b)
{
return switch_case(
tc2,
type_of<T2>::code(),
[&a, &b]()
{
swapper<T1, T2, Union>::swap(a, b);
}
);
}
template<class T1, class Union, class... Ts>
bool swap1(type_code_t tc1, type_code_t tc2, Union& a, Union& b)
{
return switch_case(
tc1,
type_of<T1>::code(),
[tc2, &a, &b]()
{
if (!do_switch(
tc2,
std::forward_as_tuple(a, b),
cases::swap<T1, Ts, Union>...
))
{
throw exception<type_error>(
"broken union in swap (2)"
);
}
}
);
}
template<class T, class Union>
bool destructor(type_code_t type, Union& u)
{
return switch_case(
type,
type_of<T>::code(),
[&u]()
{
((T&)u).~T();
}
);
}
template<class T, class TR, class Union>
bool cast(
type_code_t type,
Union& u,
std::reference_wrapper<TR>& result
)
{
static_assert(std::is_base_of<TR, T>::value, "it can never happen");
return switch_case(
type,
type_of<T>::code(),
[&u, &result]()
{
result = std::reference_wrapper<TR>((T&)u);
}
);
}
template<class Pack>
struct cast1;
template<class... Ts>
struct cast1<pack::type<Ts...>>
{
template<class T, class Union>
static bool do_switch(
type_code_t type,
Union& u,
std::reference_wrapper<T>& result
)
{
return types::do_switch(
type,
std::forward_as_tuple(u, result),
cases::cast<Ts, T, Union>...
);
}
};
} // cases
} // union_
template<class... Ts>
class safe_union
{
typedef union_::holder<Ts...> union_type;
public:
// void object - both values are uninitialized
safe_union() noexcept : the_type(type_of<void>::code()) {}
template<class T, class... Args>
safe_union(type_of<T> t, Args&&... args)
: the_type(t.code()),
u(t, std::forward<Args>(args)...)
{}
// Dynamic constructor. Supports the Ts... and void types also.
template<class... Args>
safe_union(type_code_t type_code, Args&&... args) : the_type(type_code)
{
using namespace std;
if (type_code == code<void>())
{
return;
}
if (!do_switch(
type_code,
forward_as_tuple(u, forward<Args>(args)...),
union_::cases::constructor<Ts, union_type, Args&&...>...
))
{
throw types::exception<type_error>(
"the type is not supported by the union"
);
}
}
safe_union(const safe_union& o) : the_type(o.type())
{
using namespace std;
if (the_type == code<void>())
{
return;
}
if (!do_switch(
the_type,
forward_as_tuple(o.u, u),
union_::cases::copy_constructor<Ts, union_type>...
))
{
throw types::exception<type_error>(
"broken union in copy constructor"
);
}
}
safe_union(safe_union&& o) : the_type(o.type()) // TODO noexcept
{
using namespace std;
if (the_type == code<void>())
{
return;
}
if (!do_switch(
the_type,
forward_as_tuple(o.u, u),
union_::cases::move_constructor<Ts, union_type>...
))
{
throw types::exception<type_error>(
"broken union in move constructor"
);
}
}
~safe_union()
{
using namespace std;
if (the_type == code<void>())
{
return;
}
do_switch(
the_type,
forward_as_tuple(u),
union_::cases::destructor<Ts, union_type>...
);
}
safe_union& operator=(safe_union o)
{
swap(o);
return *this;
}
safe_union& operator=(safe_union&& o)
{
swap(o);
return *this;
}
void swap(safe_union& o)
{
if (!do_switch(
the_type,
std::forward_as_tuple(o.the_type, u, o.u),
union_::cases::swap1<void, union_type, void, Ts...>,
union_::cases::swap1<Ts, union_type, void, Ts...>...
))
{
throw exception<type_error>(
"broken union is swap (1)"
);
}
std::swap(the_type, o.the_type);
}
// Changes the stored type. It calls a destructor (if not
// void) and then a constructor for a new type with args...
// If new_type is the same as type() do nothing.
template<class... Args>
void reconstruct(type_code_t new_type, Args&&... args)
{
if (new_type != type())
{
this->~safe_union();
// be careful now, actually the object is funny
// destroyed ...
new(this) safe_union(
new_type,
std::forward<Args>(args)...
);
}
}
template<class T, class... Args>
void static_reconstruct(Args&&... args)
{
reconstruct(type_of<T>::code(), std::forward<Args>(args)...);
}
// Doesn't consider base types
template<class T>
bool contains() const
{
// NB std::decay
return the_type ==
type_of<typename std::decay<T>::type>::code();
}
// a type of current union
type_code_t type() const { return the_type; }
// type dictionary
template<class T>
static type_code_t code() { return type_of<T>::code(); }
template<class T>
operator T&()
{
typedef typename std::remove_const<T>::type T1;
T1& nullptr_ref = *(T1*)nullptr;
std::reference_wrapper<T1> result(nullptr_ref);
// try to cast to some descendant of T.
// (T is also a descendant of T here)
if (union_::cases::cast1<
typename types::select_descendants<T, Ts...>
::descendants
> :: do_switch(the_type, u, result))
{
return result;
}
else
{
throw exception<type_error>(
"unable to cast the union of the type ",
limit<64, limit_policy::get_tail>(
mangled_name<mixed_string>(the_type)
),
" to the type ",
limit<64, limit_policy::get_tail>(
type_of<T>::template mangled_name<mixed_string>()
)
);
}
}
template<class T>
operator const T&() const
{
return const_cast<safe_union*>(this)->operator T&();
}
protected:
safe_union(int, type_code_t type_code) : the_type(type_code) {}
type_code_t the_type;
union_type u;
};
template<class... Ts>
void swap(safe_union<Ts...>& a, safe_union<Ts...>& b)
{
a.swap(b);
}
} // types
#endif