-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant.hpp
148 lines (122 loc) · 4.73 KB
/
variant.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
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
#pragma once
namespace nall {
template<typename T, typename... P> struct variant_size {
static constexpr u32 size = max(sizeof(T), variant_size<P...>::size);
};
template<typename T> struct variant_size<T> {
static constexpr u32 size = sizeof(T);
};
template<u32 Index, typename F, typename T, typename... P> struct variant_index {
static constexpr u32 index = is_same_v<F, T> ? Index : variant_index<Index + 1, F, P...>::index;
};
template<u32 Index, typename F, typename T> struct variant_index<Index, F, T> {
static constexpr u32 index = is_same_v<F, T> ? Index : 0;
};
template<typename T, typename... P> struct variant_copy {
constexpr variant_copy(u32 index, u32 assigned, void* target, void* source) {
if(index == assigned) new(target) T(*((T*)source));
else variant_copy<P...>(index + 1, assigned, target, source);
}
};
template<typename T> struct variant_copy<T> {
constexpr variant_copy(u32 index, u32 assigned, void* target, void* source) {
if(index == assigned) new(target) T(*((T*)source));
}
};
template<typename T, typename... P> struct variant_move {
constexpr variant_move(u32 index, u32 assigned, void* target, void* source) {
if(index == assigned) new(target) T(move(*((T*)source)));
else variant_move<P...>(index + 1, assigned, target, source);
}
};
template<typename T> struct variant_move<T> {
constexpr variant_move(u32 index, u32 assigned, void* target, void* source) {
if(index == assigned) new(target) T(move(*((T*)source)));
}
};
template<typename T, typename... P> struct variant_destruct {
constexpr variant_destruct(u32 index, u32 assigned, void* data) {
if(index == assigned) ((T*)data)->~T();
else variant_destruct<P...>(index + 1, assigned, data);
}
};
template<typename T> struct variant_destruct<T> {
constexpr variant_destruct(u32 index, u32 assigned, void* data) {
if(index == assigned) ((T*)data)->~T();
}
};
template<typename F, typename T, typename... P> struct variant_equals {
constexpr auto operator()(u32 index, u32 assigned) const -> bool {
if(index == assigned) return is_same_v<F, T>;
return variant_equals<F, P...>()(index + 1, assigned);
}
};
template<typename F, typename T> struct variant_equals<F, T> {
constexpr auto operator()(u32 index, u32 assigned) const -> bool {
if(index == assigned) return is_same_v<F, T>;
return false;
}
};
template<typename... P> struct variant final { //final as destructor is not virtual
variant() : assigned(0) {}
variant(const variant& source) { operator=(source); }
variant(variant&& source) { operator=(move(source)); }
template<typename T> variant(const T& value) { operator=(value); }
template<typename T> variant(T&& value) { operator=(move(value)); }
~variant() { reset(); }
explicit operator bool() const { return assigned; }
template<typename T> explicit constexpr operator T&() { return get<T>(); }
template<typename T> explicit constexpr operator const T&() const { return get<T>(); }
template<typename T> constexpr auto is() const -> bool {
return variant_equals<T, P...>()(1, assigned);
}
template<typename T> constexpr auto get() -> T& {
static_assert(variant_index<1, T, P...>::index, "type not in variant");
struct variant_bad_cast{};
if(!is<T>()) throw variant_bad_cast{};
return *((T*)data);
}
template<typename T> constexpr auto get() const -> const T& {
static_assert(variant_index<1, T, P...>::index, "type not in variant");
struct variant_bad_cast{};
if(!is<T>()) throw variant_bad_cast{};
return *((const T*)data);
}
template<typename T> constexpr auto get(const T& fallback) const -> const T& {
if(!is<T>()) return fallback;
return *((const T*)data);
}
auto reset() -> void {
if(assigned) variant_destruct<P...>(1, assigned, (void*)data);
assigned = 0;
}
auto& operator=(const variant& source) {
reset();
if(assigned = source.assigned) variant_copy<P...>(1, source.assigned, (void*)data, (void*)source.data);
return *this;
}
auto& operator=(variant&& source) {
reset();
if(assigned = source.assigned) variant_move<P...>(1, source.assigned, (void*)data, (void*)source.data);
source.assigned = 0;
return *this;
}
template<typename T> auto& operator=(const T& value) {
static_assert(variant_index<1, T, P...>::index, "type not in variant");
reset();
new((void*)&data) T(value);
assigned = variant_index<1, T, P...>::index;
return *this;
}
template<typename T> auto& operator=(T&& value) {
static_assert(variant_index<1, T, P...>::index, "type not in variant");
reset();
new((void*)&data) T(move(value));
assigned = variant_index<1, T, P...>::index;
return *this;
}
private:
alignas(P...) char data[variant_size<P...>::size];
u32 assigned;
};
}