Repository navigation
Expand file tree
/
Copy pathvariant_test_combined.cpp
More file actions
277 lines (220 loc) · 7.32 KB
/
Copy pathvariant_test_combined.cpp
File metadata and controls
277 lines (220 loc) · 7.32 KB
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
#include <iostream>
#include <type_traits>
#include <utility>
#include <variant> // for std::bad_variant_access
using std::cout;
using std::endl;
// helper functions in details
namespace details {
template <typename T>
struct valid_parameter
{
static_assert(!std::is_array<T>::value, "Parameter cannot be an array");
static_assert(!std::is_reference<T>::value, "Parameter cannot be a reference");
static_assert(!std::is_same<T,void>::value, "Parameter cannot be void");
};
template <typename... Args>
struct max_size_in_var_args {};
template <typename T>
struct max_size_in_var_args<T>
{
valid_parameter<T> obj {};
static const int size = sizeof(T);
};
template <typename T, typename... Args>
struct max_size_in_var_args<T, Args...>
{
valid_parameter<T> obj {};
static const int size = sizeof(T);
static const int size_rest = max_size_in_var_args<Args...>::size;
static const int max_size = size > size_rest ? size : size_rest;
};
template <int N, typename... Args>
struct type_list_contains {};
template <int N, typename InBound>
struct type_list_contains<N,InBound>
{
static const int in_list = -1;
};
template <int N, typename InBound, typename T, typename... Args>
struct type_list_contains<N,InBound,T,Args...>
{
static const int in_list = std::is_same<T,InBound>::value ? N : type_list_contains<N+1,InBound,Args...>::in_list;
};
template <int start_size,typename In, typename... Args>
struct type_index_checker
{
static const int index_of_type = -1;
};
template <int start_size, typename In, typename T, typename... Args>
struct type_index_checker<start_size,In,T,Args...>
{
static const int index_of_type = std::is_same<In,T>::value ? (start_size - sizeof...(Args) -1) : type_index_checker<start_size,In,Args...>::index_of_type;
};
template <int Idx, int StartSize, typename... Args>
struct indexed_type
{
using template_type = void;
};
template <int Idx, int StartSize, typename T, typename... Args>
struct indexed_type<Idx,StartSize,T,Args...>
{
static constexpr bool is_correct_idx = ((Idx + 1) == int(StartSize - int(sizeof...(Args))));
using template_type = typename std::conditional<is_correct_idx,T,typename indexed_type<Idx,StartSize,Args...>::template_type>::type;
};
}
template <typename... Args>
struct Var
{
template <typename T, typename... VArgs, typename std::enable_if<!bool(sizeof...(VArgs))>::type* = nullptr>
void index_to_type(int start_size, int idx)
{
(void)start_size;
(void)idx;
T* ptr = reinterpret_cast<T*>(&data);
ptr->~T();
return;
}
template <typename T, typename... VArgs, typename std::enable_if<bool(sizeof...(VArgs))>::type* = nullptr>
void index_to_type(int start_size, int idx)
{
if((idx + 1) == int(start_size - int(sizeof...(VArgs))))
{
T* ptr = reinterpret_cast<T*>(&data);
ptr->~T();
return;
}
index_to_type<VArgs...>(start_size, idx);
}
template <typename... VArgs>
void index_to_type_wrapper(int idx)
{
index_to_type<VArgs...>(sizeof...(VArgs),idx);
}
bool contains = false;
int curr_idx;
static constexpr int num_tmps = sizeof...(Args);
static constexpr int max_size = details::max_size_in_var_args<Args...>::max_size;
typename std::aligned_storage<max_size, alignof(std::max_align_t)>::type data;
//default ctor
// should probably do something similar to the std::monostate constraint
// but not yet
Var()
{}
// copy constructor and assignment operator for other Variants
// copy constructor of variant with different Args should not compile
template <typename... Vargs>
Var(const Var<Vargs...>& other)
{
static_assert(std::is_same_v<decltype(std::declval<Var<Args...>>),decltype(std::declval<Var<Vargs...>>)>,"Variant type mismatch in copy ctor");
//memcpy the bytes and set the index
curr_idx = other.curr_idx;
memcpy(&(data),&(other.data),max_size);
}
template <typename... Vargs>
void operator=(const Var<Vargs...>& other)
{
static_assert(std::is_same_v<decltype(std::declval<Var<Args...>>),decltype(std::declval<Var<Vargs...>>)>,"Variant type mismatch in copy ctor");
curr_idx = other.curr_idx;
memcpy(&(data),&(other.data),max_size);
}
// Generic copy constructor and assignment operator
template <typename T>
Var(const T& obj) // change to const T&?
{
curr_idx = details::type_list_contains<0,T,Args...>::in_list;
if(curr_idx == -1)
{
throw std::bad_variant_access();
}
new(&data) T(obj);
contains = true;
}
template <typename T>
void operator=(const T& obj)
{
int old_idx = curr_idx;
curr_idx = details::type_list_contains<0,T,Args...>::in_list;
if( curr_idx == -1)
{
throw std::bad_variant_access();
}
if(contains)
{
index_to_type_wrapper<Args...>(old_idx);
}
else
{
contains = true;
}
new(&data) T(obj);
}
// call the appropriate destructor if the variant was ever set
~Var()
{
if(contains)
{
index_to_type_wrapper<Args...>(curr_idx);
}
}
}; // variant
// Test class to make sure construction / destruction works
class test_class
{
public:
test_class()
{
cout << "test class ctor\n";
}
test_class(const test_class& obj)
{
(void) obj; // remove unused warning
cout << "test class copy ctor\n";
}
~test_class()
{
cout << "test class dtor\n";
}
};
// Implementation of std::variant::get<INT>();
template <int N, typename... Args>
auto Var_get(const Var<Args...>& v) -> typename std::add_lvalue_reference<typename details::indexed_type<N,sizeof...(Args),Args...>::template_type>::type
{
if(v.curr_idx != N)
{
throw std::bad_variant_access();
}
//replace with using.. eventually
typedef typename details::indexed_type<N,sizeof...(Args),Args...>::template_type ReturnType; // return type
ReturnType* ptr;
ptr = const_cast<ReturnType*>(reinterpret_cast<const ReturnType*>((&v.data)));
return *ptr;
}
// Implementation of std::variant::get<T>();
// Check if current type is actually T
template <typename T, typename... Args>
T& Var_get(const Var<Args...>& v)
{
int index_of_T = details::type_index_checker<sizeof...(Args),T,Args...>::index_of_type;
if (index_of_T != v.curr_idx)
{
throw std::bad_variant_access();
}
T *ptr = const_cast<T*>(reinterpret_cast<const T*>((&v.data)));
return *ptr;
}
int main()
{
Var<int,double,char> v{'c'};
Var<int,test_class> vv = 1;
test_class a;
vv = a;
Var_get<char>(v) = 'l';
cout << Var_get<char>(v) << endl;
Var<int,double,char> g;
g = -981;
v = g;
cout << Var_get<int>(v) << endl;
Var_get<0>(v) = 6;
cout << Var_get<int>(v) << endl;
}