-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar.hpp
More file actions
469 lines (380 loc) · 17.2 KB
/
var.hpp
File metadata and controls
469 lines (380 loc) · 17.2 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#ifndef PYTHON_VAR_HPP
#define PYTHON_VAR_HPP
#include <memory>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
#include <initializer_list>
#include <variant>
#include <cstring>
#define REG_TYPE(type) \
if (typeid(T) == type()) \
{ \
return std::any_cast<T>(*this); \
}
namespace python
{
std::string type(const std::type_info &t) {
return abi::__cxa_demangle(t.name(), 0, 0, 0);
}
template <typename T> std::string type(T) {
return abi::__cxa_demangle(typeid(T).name(), 0, 0, 0);
}
class bad_cast : public std::bad_cast {
std::string error;
public:
bad_cast(std::string t) : error(t) {}
bad_cast(const std::type_info &t1, const std::type_info &t2) : bad_cast("\033[1;31mTypeError\033[0m: implicit conversion from " + type(t1) + " to " + type(t2)) {}
const char *what() const noexcept override { return error.c_str(); }
};
class var {
using Storage = void*;
using Type = const std::type_info;
using Size = unsigned long;
struct handle_base {
bool is_ref;
handle_base(bool is_ref) : is_ref(is_ref) {}
virtual Storage copy(Storage) = 0;
virtual Type* type() = 0;
virtual Size size() = 0;
virtual void destroy(Storage) = 0;
virtual bool __bool__(Storage) = 0;
virtual char __char__(Storage) = 0;
virtual int __int__(Storage) = 0;
virtual double __double__(Storage) = 0;
virtual std::string __str__(Storage) = 0;
virtual std::string __repr__(Storage) = 0;
virtual var __neg__(Storage) = 0;
virtual var __pos__(Storage) = 0;
virtual var __dref__(Storage) = 0;
virtual var __rinc__(Storage) = 0;
virtual var __rdec__(Storage) = 0;
virtual var __linc__(Storage) = 0;
virtual var __ldec__(Storage) = 0;
virtual var __getitem__(Storage, var) = 0;
virtual var __eq__(Storage, Storage) = 0;
virtual var __ne__(Storage, Storage) = 0;
virtual var __lt__(Storage, Storage) = 0;
virtual var __gt__(Storage, Storage) = 0;
virtual var __le__(Storage, Storage) = 0;
virtual var __ge__(Storage, Storage) = 0;
virtual var __add__(Storage, Storage) = 0;
virtual var __sub__(Storage, Storage) = 0;
virtual var __mul__(Storage, Storage) = 0;
virtual var __div__(Storage, Storage) = 0;
virtual var __mod__(Storage, Storage) = 0;
virtual var __and__(Storage, Storage) = 0;
virtual var __or__(Storage, Storage) = 0;
virtual var __xor__(Storage, Storage) = 0;
virtual var __lshift__(Storage, Storage) = 0;
virtual var __rshift__(Storage, Storage) = 0;
virtual var __iadd__(Storage, Storage) = 0;
virtual var __isub__(Storage, Storage) = 0;
virtual var __imul__(Storage, Storage) = 0;
virtual var __idiv__(Storage, Storage) = 0;
virtual var __imod__(Storage, Storage) = 0;
virtual var __iand__(Storage, Storage) = 0;
virtual var __ior__(Storage, Storage) = 0;
virtual var __ixor__(Storage, Storage) = 0;
virtual var __ilshift__(Storage, Storage) = 0;
virtual var __irshift__(Storage, Storage) = 0;
virtual var __begin__(Storage) = 0;
virtual var __end__(Storage) = 0;
};
#define DEF_TRY(TYPE,NAME,OP,TRY,RET,...) \
template<typename S,typename=decltype(TRY)> \
static TYPE __##NAME##__(__VA_ARGS__) { \
return RET; \
} \
template<typename S> \
static TYPE __##NAME##__(...) { \
throw std::runtime_error("\033[1;31mTypeError\033[0m: type " + std::string(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) + " has no operator " #OP); \
}
struct handle_helper {
#define DEF_rUNARY(NAME,OP) DEF_TRY(var,NAME,OP,std::declval<S&>() OP,(*(S*)o) OP,Storage o)
DEF_rUNARY(rinc,++) DEF_rUNARY(rdec,--)
#undef DEF_rUNARY
#define DEF_lUNARY(NAME,OP) DEF_TRY(var,NAME,OP,OP std::declval<S&>(),OP (*(S*)o),Storage o)
DEF_lUNARY(dref,*) DEF_lUNARY(linc,++) DEF_lUNARY(ldec,--) DEF_lUNARY(neg,-) DEF_lUNARY(pos,+)
#undef DEF_lUNARY
#define DEF_BINARY(NAME,OP) DEF_TRY(var,NAME,OP,std::declval<S&>() OP std::declval<S&>(),(*(S*)o1 OP *(S*)o2),Storage o1,Storage o2)
DEF_BINARY(add,+) DEF_BINARY(sub,-) DEF_BINARY(mul,*) DEF_BINARY(div,/) DEF_BINARY(mod,%)
DEF_BINARY(and,&) DEF_BINARY(or,|) DEF_BINARY(xor,^) DEF_BINARY(lshift,<<) DEF_BINARY(rshift,>>)
DEF_BINARY(eq,==) DEF_BINARY(ne,!=) DEF_BINARY(lt,<) DEF_BINARY(le,<=) DEF_BINARY(gt,>) DEF_BINARY(ge,>=)
DEF_BINARY(iadd,+=) DEF_BINARY(isub,-=) DEF_BINARY(imul,*=) DEF_BINARY(idiv,/=) DEF_BINARY(imod,%=)
DEF_BINARY(iand,&=) DEF_BINARY(ior,|=) DEF_BINARY(ixor,^=) DEF_BINARY(ilshift,<<=) DEF_BINARY(irshift,>>=)
#undef DEF_BINARY
// #define DEF_CAST(TYPE) DEF_TRY(TYPE,TYPE,TYPE,(TYPE)(std::declval<S&>()),(TYPE)(*(S*)o),Storage o)
#define DEF_CAST(TYPE) DEF_TRY(TYPE,TYPE,TYPE,static_cast<TYPE>(std::declval<S>()),static_cast<TYPE>(*static_cast<S*>(o)),Storage o)
DEF_CAST(bool) DEF_CAST(int) DEF_CAST(double) DEF_CAST(char)
#undef DEF_CAST
template <typename S> static var __getitem__(...) {
throw std::runtime_error("\033[1;31mTypeError\033[0m: type " + std::string(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) + " has no operator []");
}
// template <typename S, typename=decltype(std::declval<S&>()[std::declval<var>()]),std::enable_if_t<!std::is_const<S>::value,int> = 0>
// template <typename T,typename S=std::remove_const_t<T>,typename=decltype(std::declval<S&>()[std::declval<var>()])>
// static var __getitem__(Storage o, var k) {
// return var( &((*(S*)o)[k]) ,new handle<S>(true));
// }
template<typename U> class can_getitem_ref {
template<typename S,typename=decltype(std::addressof(std::declval<S>()[std::declval<var>()]))>
static std::true_type check(int);
template<typename>
static std::false_type check(...);
public: static constexpr bool value = decltype(check<U>(0))::value && !std::is_pointer<U>::value;
};
template<typename U> static constexpr bool can_getitem_ref_v = can_getitem_ref<U>::value;
// get a reference
template <typename S, typename V=decltype(std::declval<S>()[std::declval<var>()]),
std::enable_if_t<can_getitem_ref_v<S>,int> = 0>
static var __getitem__(Storage o, var k) {
return var(std::addressof((*static_cast<S*>(o))[k]) ,new handle<V>(true));
}
// get a copy
template <typename S, typename V=decltype(std::declval<S>()[std::declval<var>()]),
std::enable_if_t<!can_getitem_ref_v<S>,int> = 0>
static var __getitem__(Storage o, var k) {
return var((*static_cast<S*>(o))[k]);
}
template<typename S,typename=decltype(std::cout<<std::declval<S>())>
static std::string __str__(Storage o) {
std::stringstream ss;
if (o) switch (std::is_pointer<S>::value) {
case true:
if (typeid(S) == typeid(char*) || typeid(S) == typeid(const char*))
ss << *(char**)o;
else
ss << o;
break;
case false:
if (typeid(S) == typeid(bool))
ss << (*static_cast<bool*>(o) ? "True" : "False");
else
ss << *static_cast<S*>(o);
}
else
ss << "None";
return ss.str();
}
template<typename S>
static std::string __str__(...) {
throw std::runtime_error("\033[1;31mTypeError\033[0m: type " + std::string(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) + " can not be converted to string");
}
#define DEF_MEMBER_FUNC(NAME) \
template<typename S,typename=decltype(std::declval<S>().NAME())> \
static var __##NAME##__(Storage o) { \
return static_cast<S*>(o)->NAME(); \
} \
template<typename S> \
static var __##NAME##__(...) { \
throw std::runtime_error("\033[1;31mTypeError\033[0m: type " + std::string(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) + " has no member function " #NAME); \
}
DEF_MEMBER_FUNC(begin) DEF_MEMBER_FUNC(end)
#undef DEF_MEMBER_FUNC
};
template<typename U, typename T=std::decay_t<U>>
struct handle : public handle_base {
handle(bool is_ref=false) : handle_base(is_ref) {}
Storage copy(Storage o) { return new T(*static_cast<T*>(o));}
Type* type() override { return &typeid(T); }
Size size() override { return sizeof(T); }
void destroy(Storage o) override { if (!is_ref) delete static_cast<T*>(o); }
#define DEF_UNARY(NAME) var __##NAME##__(Storage o) override { return handle_helper::__##NAME##__<T>(o); }
DEF_UNARY(dref) DEF_UNARY(rinc) DEF_UNARY(rdec) DEF_UNARY(linc) DEF_UNARY(ldec) DEF_UNARY(neg) DEF_UNARY(pos)
#undef DEF_UNARY
#define DEF_BINARY(NAME) var __##NAME##__(Storage o1, Storage o2) override { return handle_helper::__##NAME##__<T>(o1,o2); }
DEF_BINARY(add) DEF_BINARY(sub) DEF_BINARY(mul) DEF_BINARY(div) DEF_BINARY(mod)
DEF_BINARY(and) DEF_BINARY(or) DEF_BINARY(xor) DEF_BINARY(lshift) DEF_BINARY(rshift)
DEF_BINARY(eq) DEF_BINARY(ne) DEF_BINARY(lt) DEF_BINARY(le) DEF_BINARY(gt) DEF_BINARY(ge)
DEF_BINARY(iadd) DEF_BINARY(isub) DEF_BINARY(imul) DEF_BINARY(idiv) DEF_BINARY(imod)
DEF_BINARY(iand) DEF_BINARY(ior) DEF_BINARY(ixor) DEF_BINARY(ilshift) DEF_BINARY(irshift)
#undef DEF_BINARY
var __getitem__(Storage o, var k) override { return handle_helper::__getitem__<T>(o,k); }
#define DEF_CAST(TYPE) TYPE __##TYPE##__(Storage o) override { return handle_helper::__##TYPE##__<T>(o); }
DEF_CAST(bool) DEF_CAST(int) DEF_CAST(double) DEF_CAST(char)
#undef DEF_CAST
std::string __str__(Storage o) override { return handle_helper::__str__<T>(o); }
std::string __repr__(Storage o) override {
if (typeid(T) == typeid(char))
return '\''+__str__(o)+'\'';
else if (typeid(T) == typeid(char*) || typeid(T) == typeid(const char*) || typeid(T) == typeid(std::string))
return '\"'+ __str__(o)+'\"';
else
return __str__(o);
}
var __begin__(Storage o) override { return handle_helper::__begin__<T>(o); }
var __end__(Storage o) override { return handle_helper::__end__<T>(o); }
};
template<typename U, typename T=std::decay_t<U>, typename A> Storage new_(A && v) { return new T(std::forward<U>(v)); }
template<typename U, typename T=std::decay_t<U>, typename...A> Storage new_(A &&...a) { return new T(std::forward<A>(a)...); }
Storage m_data;
handle_base* m_handle;
var(Storage o, handle_base* h) : m_data(o), m_handle(h) {}
public:
// APIs
bool has_value() const { return m_handle != nullptr; }
bool is_empty() const { return m_handle == nullptr; }
bool is_ref() const { return m_handle ? m_handle->is_ref : false; }
// bool is_ptr() const { return
Type& type() const { return m_handle ? *m_handle->type(): typeid(void) ; }
Size size() const { return m_handle ? m_handle->size():0; }
std::string __repr__() const {
return m_handle ? m_handle->__repr__(m_data) : "None";
}
std::string __str__() const {
return m_handle ? m_handle->__str__(m_data) : "None";
}
template<typename T> operator T() const {
return get<T>();
}
template<typename T>
T& get() {
if (type() != typeid(T))
throw bad_cast(type(), typeid(T));
return *static_cast<T*>(m_data);
}
template<typename T>
const T& get() const {
if (type() != typeid(T))
throw bad_cast(type(), typeid(T));
return *static_cast<const T*>(m_data);
}
void swap(var &o) {
std::swap(m_data, o.m_data);
std::swap(m_handle, o.m_handle);
}
var begin() const {
if (m_handle) return m_handle->__begin__(m_data);
else throw std::runtime_error("TypeError: cannot call begin() on None");
}
var end() const {
if (m_handle) return m_handle->__end__(m_data);
else throw std::runtime_error("TypeError: cannot call end() on None");
}
// constructors
var() : var(nullptr,nullptr) {}
var(var &o) : m_data(o.m_handle ? o.m_handle->copy(o.m_data) : nullptr), m_handle(o.m_handle) {}
var(const var &o) : m_data(o.m_handle ? o.m_handle->copy(o.m_data) : nullptr), m_handle(o.m_handle) {}
var(var &&o) : m_data(o.m_data), m_handle(o.m_handle) { o.m_data = nullptr; o.m_handle = nullptr; }
/// @note: the most important constructor that construct from var type
template <typename T> var(T &&v) : m_data(new_<T>(std::forward<T>(v))), m_handle(new handle<T>()) {}
// template <typename T, typename...A> var(A &&...a) : m_data(new_<T>(std::forward<A>(a)...)), m_handle(new handle<T>()) {}
// /// @note: call var(something) explicitly to construct by reference
// template <typename T, std::enable_if_t<!std::is_const<T>::value>>
// explicit var(T &v) : m_data(&v), m_handle(new handle<T>(true)) {
// std::cout << "var(T&) called" << std::endl;
// }
// destructor
~var() { if (m_handle) { m_handle->destroy(m_data); } }
// assignment
var& operator=(const var &o) { var(o).swap(*this); return *this; }
var& operator=(var &o) { var(o).swap(*this); return *this; }
var& operator=(var &&o) {
if (o.is_ref()) {
m_handle = o.m_handle;
o.m_handle = nullptr;
m_data = m_handle->copy(o.m_data);
} else {
var(std::move(o)).swap(*this);
}
return *this;
}
// template <typename T,typename=decltype(*(T*)nullptr = std::declval<T>())>
template <typename T>
var& operator=(T &&v) {
if (type() != typeid(T)) {
var(std::forward<T>(v)).swap(*this);
} else {
*static_cast<std::decay_t<T>*>(m_data) = std::forward<T>(v);
}
return *this;
}
// template<typename T>
// var& operator=(const T &v) {
// if (type() != typeid(T) && !is_ref()) {
// var(v).swap(*this);
// } else {
// *((T*)m_data) = v;
// }
// return *this;
// }
// template <typename T, typename...A> var& operator=(A &&...a) { var(std::forward<A>(a)...).swap(*this); return *this; }
bool operator==(const var& o) const {
if (type() == o.type())
return m_handle->__eq__(m_data, o.m_data);
return false;
}
#define DEF_CALC(OP,NAME) \
var operator OP(const var& o) const { \
if (type() == o.type()) \
return m_handle->__##NAME##__(m_data, o.m_data); \
throw std::runtime_error("\033[1;31mType mismatch\033[0m: got "+python::type(type())+ " " #OP " " +python::type(o.type()) + ", but only operation of the same type is allowed"); \
}
// comparison
DEF_CALC(!=,ne) DEF_CALC(<,lt) DEF_CALC(<=,le) DEF_CALC(>,gt) DEF_CALC(>=,ge)
// arithmetic operators
DEF_CALC(+,add) DEF_CALC(-,sub) DEF_CALC(*,mul) DEF_CALC(/,div) DEF_CALC(%,mod)
DEF_CALC(&,and) DEF_CALC(|,or) DEF_CALC(^,xor) DEF_CALC(<<,lshift) DEF_CALC(>>,rshift)
#undef DEF_CALC
// self-arithmetic operators
#define DEF_SELF_CALC(OP,NAME)\
var& operator OP(const var& o) { \
if (type() == o.type()) { \
m_handle->__##NAME##__(m_data, o.m_data); \
return *this; \
} \
throw std::runtime_error("\033[1;31mType mismatch\033[0m: got "+python::type(type())+ " " #OP " " +python::type(o.type()) + " only operation of the same type is allowed"); \
}
DEF_SELF_CALC(+=,iadd) DEF_SELF_CALC(-=,isub) DEF_SELF_CALC(*=,imul) DEF_SELF_CALC(/=,idiv) DEF_SELF_CALC(%=,imod)
DEF_SELF_CALC(&=,iand) DEF_SELF_CALC(|=,ior) DEF_SELF_CALC(^=,ixor) DEF_SELF_CALC(<<=,ilshift) DEF_SELF_CALC(>>=,irshift)
#undef DEF_SELF_CALC
#define DEF_UNARY(OP,NAME) \
var operator OP() const { \
return m_handle->__##NAME##__(m_data); \
}
DEF_UNARY(+,pos) DEF_UNARY(-,neg) DEF_UNARY(*,dref)
#undef DEF_UNARY
var operator++(int) {
return m_handle->__rinc__(m_data);
}
var operator--(int) {
return m_handle->__rdec__(m_data);
}
var& operator++() {
m_handle->__linc__(m_data);
return *this;
}
var& operator--() {
m_handle->__ldec__(m_data);
return *this;
}
// cast
operator bool() const {
return m_handle ? m_handle->__bool__(m_data) : false;
}
#define DEF_CAST(TYPE) operator TYPE() const { return m_handle ? m_handle->__##TYPE##__(m_data) : TYPE(); }
DEF_CAST(int) DEF_CAST(double) DEF_CAST(char)
#undef DEF_CAST
operator unsigned long long() const { return int(*this); }
operator std::string() const { return m_handle ? m_handle->__str__(m_data) : ""; }
var operator[](var key) const {
return m_handle->__getitem__(m_data, key);
}
Storage operator&() { return m_data; }
// stream
friend std::ostream& operator<<(std::ostream &os, const var &o) {
return os << o.__str__();
}
};
std::string type(const var &v) {
return abi::__cxa_demangle(v.type().name(), NULL, NULL, NULL);
}
template <typename T>
size_t len(const T &t) {
return t.__len__();
}
}
#endif