-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.cpp
338 lines (265 loc) · 7.92 KB
/
variant.cpp
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
#include <typeindex>
#include <iostream>
#include <type_traits>
#include <tuple>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
//获取最大的整数
template <size_t arg, size_t... rest>
struct IntegerMax;
template <size_t arg>
struct IntegerMax<arg> : std::integral_constant<size_t, arg>
{
//static const size_t value = arg;
//enum{value = arg};
};
//获取最大的align
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 >
{
/*static const size_t value = arg1 >= arg2 ? static_max<arg1, others...>::value :
static_max<arg2, others...>::value;*/
};
template<typename... Args>
struct MaxAlign : std::integral_constant<int, IntegerMax<std::alignment_of<Args>::value...>::value>{};
/*
template<typename T, typename... Args>
struct MaxAlign : std::integral_constant<int,
(std::alignment_of<T>::value >MaxAlign<Args...>::value ? std::alignment_of<T>::value : MaxAlign<Args...>::value) >
{};
template<typename T>
struct MaxAlign<T> : std::integral_constant<int, std::alignment_of<T>::value >{}; */
//是否包含某个类型
template < typename T, typename... List >
struct Contains : std::true_type {};
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{};
//获取第一个T的索引位置
// Forward
template<typename Type, typename... Types>
struct GetLeftSize;
// Declaration
template<typename Type, typename First, typename... Types>
struct GetLeftSize<Type, First, Types...> : GetLeftSize<Type, Types...>
{
};
// Specialized
template<typename Type, typename... Types>
struct GetLeftSize<Type, Type, Types...> : std::integral_constant<int, sizeof...(Types)>
{
//static const int ID = sizeof...(Types);
};
template<typename Type>
struct GetLeftSize<Type> : std::integral_constant<int, -1>
{
//static const int ID = -1;
};
template<typename T, typename... Types>
struct Index : std::integral_constant<int, sizeof...(Types)-GetLeftSize<T, Types...>::value - 1>{};
//根据索引获取索引位置的类型
// Forward declaration
template<int index, typename... Types>
struct IndexType;
// Declaration
template<int index, typename First, typename... Types>
struct IndexType<index, First, Types...> : IndexType<index - 1, Types...>
{
};
// Specialized
template<typename First, typename... Types>
struct IndexType<0, First, Types...>
{
typedef First DataType;
};
template<typename... Args>
struct VariantHelper;
template<typename T, typename... Args>
struct VariantHelper<T, Args...>
{
inline static void Destroy(std::type_index id, void * data)
{
if (id == std::type_index(typeid(T)))
//((T*) (data))->~T();
reinterpret_cast<T*>(data)->~T();
else
VariantHelper<Args...>::Destroy(id, data);
}
inline static void move(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)));
else
VariantHelper<Args...>::move(old_t, old_v, new_v);
}
inline static void copy(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));
else
VariantHelper<Args...>::copy(old_t, old_v, new_v);
}
};
template<>
struct VariantHelper<>
{
inline static void Destroy(std::type_index id, void * data) { }
inline static void move(std::type_index old_t, void * old_v, void * new_v) { }
inline static void copy(std::type_index old_t, const void * old_v, void * new_v) { }
};
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())>
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
enum { arity = sizeof...(Args) };
// arity is the number of arguments.
typedef ReturnType result_type;
template <size_t i>
struct arg
{
//typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
// the i-th argument is equivalent to the i-th tuple element of a tuple
// composed of those arguments.
};
typedef std::function<ReturnType(Args...)> FunType;
typedef std::tuple<Args...> ArgTupleType;
};
template<typename... Types>
class Variant
{
typedef VariantHelper<Types...> Helper_t;
enum
{
data_size = IntegerMax<sizeof(Types)...>::value,
//align_size = IntegerMax<alignof(Types)...>::value
align_size = MaxAlign<Types...>::value //ctp才有alignof, 为了兼容用此版本
};
using data_t = typename std::aligned_storage<data_size, align_size>::type;
public:
template<int index>
using IndexType = typename IndexType<index, Types...>::DataType;
Variant(void) : m_typeIndex(typeid(void))
{
}
~Variant()
{
Helper_t::Destroy(m_typeIndex, &m_data);
}
Variant(Variant<Types...>&& old) : m_typeIndex(old.m_typeIndex)
{
Helper_t::move(old.m_typeIndex, &old.m_data, &m_data);
}
Variant(const Variant<Types...>& old) : m_typeIndex(old.m_typeIndex)
{
Helper_t::copy(old.m_typeIndex, &old.m_data, &m_data);
}
Variant& operator=(const Variant& old)
{
Helper_t::copy(old.m_typeIndex, &old.m_data, &m_data);
m_typeIndex = old.m_typeIndex;
return *this;
}
Variant& operator=(Variant&& old)
{
Helper_t::move(old.m_typeIndex, &old.m_data, &m_data);
m_typeIndex = old.m_typeIndex;
return *this;
}
template <class T,
class = typename std::enable_if<Contains<typename std::remove_reference<T>::type, Types...>::value>::type>
Variant(T&& value) : m_typeIndex(typeid(void))
{
Helper_t::Destroy(m_typeIndex, &m_data);
typedef typename std::remove_reference<T>::type U;
new(&m_data) U(std::forward<T>(value));
m_typeIndex = std::type_index(typeid(T));
}
template<typename T>
bool Is() const
{
return (m_typeIndex == std::type_index(typeid(T)));
}
bool Empty() const
{
return m_typeIndex == std::type_index(typeid(void));
}
std::type_index Type() const
{
return m_typeIndex;
}
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 " <<
m_typeIndex.name() << std::endl;
throw std::bad_cast();
}
return *(U*)(&m_data);
}
template<typename T>
int GetIndexOf()
{
return Index<T, Types...>::value;
}
template<typename F>
void Visit(F&& f)
{
using T = typename function_traits<F>::arg<0>::type;
//typedef typename function_traits<F>::arg<0>::type T;
if (Is<T>())
f(Get<T>());
}
template<typename F, typename... Rest>
void Visit(F&& f, Rest&&... rest)
{
using T = typename function_traits<F>::arg<0>::type;
if (Is<T>())
Visit(std::forward<F>(f));
else
Visit(std::forward<Rest>(rest)...);
}
bool operator==(const Variant& rhs) const
{
return m_typeIndex == rhs.m_typeIndex;
}
bool operator<(const Variant& rhs) const
{
return m_typeIndex < rhs.m_typeIndex;
}
private:
data_t m_data;
std::type_index m_typeIndex; //类型ID
};
void Test()
{
typedef Variant<int, double, std::string, int> cv;
//根据index获取类型
std::cout << typeid(cv::IndexType<1>).name() << std::endl;
//根据类型获取索引
cv v = 10;
int i = v.GetIndexOf<std::string>();
std::cout << "i = " << i << std::endl;
//通过一组lambda访问vairant
v.Visit([&](double i){std::cout << "double: " << i << std::endl; },
[&](short i){std::cout << "short: " << i << std::endl; },
[](int i){std::cout << "int: " << i << std::endl; },
[](std::string i){std::cout << "std::string: " << i << std::endl; } );
bool emp1 = v.Empty();
std::cout << v.Type().name() << std::endl;
}
int main(void)
{
Test();
return 0;
}