-
Notifications
You must be signed in to change notification settings - Fork 456
/
Variant.hpp
220 lines (182 loc) · 4.62 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
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
#pragma once
#include <typeindex>
#include <iostream>
/** 获取最大的整数 */
template <size_t arg, size_t... rest>
struct IntegerMax;
template <size_t arg>
struct IntegerMax<arg> : std::integral_constant<size_t, arg>
{
};
template <size_t arg1, size_t arg2, size_t... rest>
struct IntegerMax<arg1, arg2, rest...> : std::integral_constant<size_t, arg1 >= arg2 ? IntegerMax<arg1, rest...>::value
: IntegerMax<arg2, rest...>::value>
{
};
/** 获取最大的align */
template<typename... Args>
struct MaxAlign : std::integral_constant<int, IntegerMax<std::alignment_of<Args>::value...>::value>
{
};
/** 是否包含某个类型 */
template <typename T, typename... List>
struct Contains;
template <typename T, typename Head, typename... Rest>
struct Contains<T, Head, Rest...>
: std::conditional<std::is_same<T, Head>::value, std::true_type, Contains<T, Rest... >> ::type
{
};
template <typename T>
struct Contains<T> : std::false_type
{
};
template <typename T, typename... List>
struct IndexOf;
template <typename T, typename Head, typename... Rest>
struct IndexOf<T, Head, Rest...>
{
enum { value = IndexOf<T, Rest...>::value + 1 };
};
template <typename T, typename... Rest>
struct IndexOf<T, T, Rest...>
{
enum { value = 0 };
};
template <typename T>
struct IndexOf<T>
{
enum{value = -1};
};
template<int index, typename... Types>
struct At;
template<int index, typename First, typename... Types>
struct At<index, First, Types...>
{
using type = typename At<index - 1, Types...>::type;
};
template<typename T, typename... Types>
struct At<0, T, Types...>
{
using type = T;
};
template<typename... Types>
class Variant
{
enum
{
data_size = IntegerMax<sizeof(Types)...>::value,
align_size = MaxAlign<Types...>::value
};
using data_t = typename std::aligned_storage<data_size, align_size>::type;
public:
template<int index>
using IndexType = typename At<index, Types...>::type;
Variant(void) :type_index_(typeid(void))
{
}
~Variant()
{
destroy(type_index_, &data_);
}
Variant(Variant<Types...>&& old) : type_index_(old.type_index_)
{
move(old.type_index_, &old.data_, &data_);
}
Variant(const Variant<Types...>& old) : type_index_(old.type_index_)
{
copy(old.type_index_, &old.data_, &data_);
}
Variant& operator=(const Variant& old)
{
copy(old.type_index_, &old.data_, &data_);
type_index_ = old.type_index_;
return *this;
}
Variant& operator=(Variant&& old)
{
move(old.type_index_, &old.data_, &data_);
type_index_ = old.type_index_;
return *this;
}
template <class T,
class = typename std::enable_if<Contains<typename std::decay<T>::type, Types...>::value>::type>
Variant(T&& value) : type_index_(typeid(void))
{
destroy(type_index_, &data_);
typedef typename std::decay<T>::type U;
new(&data_) U(std::forward<T>(value));
type_index_ = std::type_index(typeid(U));
}
template<typename T>
bool is() const
{
return (type_index_ == std::type_index(typeid(T)));
}
bool Empty() const
{
return type_index_ == std::type_index(typeid(void));
}
std::type_index type() const
{
return type_index_;
}
template<typename T>
typename std::decay<T>::type& get()
{
using U = typename std::decay<T>::type;
if (!is<U>())
{
std::cout << typeid(U).name() << " is not defined. "
<< "current type is " << type_index_.name() << std::endl;
throw std::bad_cast{};
}
return *(U*)(&data_);
}
template <typename T>
int indexOf()
{
return IndexOf<T, Types...>::value;
}
bool operator==(const Variant& rhs) const
{
return type_index_ == rhs.type_index_;
}
bool operator<(const Variant& rhs) const
{
return type_index_ < rhs.type_index_;
}
private:
void destroy(const std::type_index& index, void *buf)
{
[](Types&&...){}((destroy0<Types>(index, buf), 0)...);
}
template<typename T>
void destroy0(const std::type_index& id, void *data)
{
if (id == std::type_index(typeid(T)))
reinterpret_cast<T*>(data)->~T();
}
void move(const std::type_index& old_t, void *old_v, void *new_v)
{
[](Types&&...){}((move0<Types>(old_t, old_v, new_v), 0)...);
}
template<typename T>
void move0(const std::type_index& old_t, void *old_v, void *new_v)
{
if (old_t == std::type_index(typeid(T)))
new (new_v)T(std::move(*reinterpret_cast<T*>(old_v)));
}
void copy(const std::type_index& old_t, const void *old_v, void *new_v)
{
[](Types&&...){}((copy0<Types>(old_t, old_v, new_v), 0)...);
}
template<typename T>
void copy0(const std::type_index& old_t, const void *old_v, void *new_v)
{
if (old_t == std::type_index(typeid(T)))
new (new_v)T(*reinterpret_cast<const T*>(old_v));
}
private:
data_t data_;
std::type_index type_index_;
};