-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_poly_union_macro_helper.hpp
109 lines (95 loc) · 2.48 KB
/
basic_poly_union_macro_helper.hpp
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
template
< typename Base
, int N
, typename StorageType
#if IS_DEFINING_DEFAULT_CONSTRUCTOR(PARAM)
, typename DefaultConstructType
#endif
>
#if IS_DEFINING_DEFAULT_CONSTRUCTOR(PARAM)
#endif
struct basic_poly_union
< Base
, N
, StorageType
#if IS_DEFINING_DEFAULT_CONSTRUCTOR(PARAM)
, DefaultConstructType
#else
, void
#endif
, IS_DEFINING_MOVE(PARAM)
, IS_DEFINING_COPY(PARAM)
>
{
#if IS_DEFINING_MOVE(PARAM)
basic_poly_union(basic_poly_union &&) = default;
basic_poly_union & operator=(basic_poly_union &&) = default;
#endif
#if IS_DEFINING_COPY(PARAM)
basic_poly_union(basic_poly_union const &) = default;
basic_poly_union & operator=(basic_poly_union const &) = default;
#endif
#if IS_DEFINING_DEFAULT_CONSTRUCTOR(PARAM)
basic_poly_union()
: storage_(std::type_identity<DefaultConstructType>{})
{
}
#endif
template <typename Derived, typename... Args>
requires std::derived_from<Derived, Base>
basic_poly_union(std::type_identity<Derived> w, Args &&... args)
: storage_(w, std::forward<Args>(args)...)
{
}
template <typename Derived>
requires std::derived_from<Derived, Base> && std::copy_constructible<Derived>
basic_poly_union(Derived const & v)
: storage_(std::type_identity<Derived>{}, v)
{
}
template <typename Derived>
requires std::derived_from<Derived, Base> && std::move_constructible<Derived>
basic_poly_union(Derived && v)
: storage_(std::type_identity<Derived>{}, std::forward<Derived>(v))
{
}
template <typename Derived, typename... Args>
requires std::derived_from<Derived, Base>
Derived & emplace(Args &&... args)
{
return *storage_.template emplace<Derived>(std::forward<Args>(args)...);
}
// call emplace with copy constructor
template <typename Derived>
requires std::derived_from<Derived, Base> && std::copy_constructible<Derived>
Derived & insert_copy(Derived const & derived)
{
return emplace<Derived>(derived);
}
Base * pointer()
{
return storage_.pointer();
}
Base const * pointer() const
{
return storage_.pointer();
}
Base * operator->()
{
return pointer();
}
Base const * operator->() const
{
return pointer();
}
Base & get()
{
return *pointer();
}
Base const & get() const
{
return *pointer();
}
private:
StorageType storage_;
};