-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectExperimental.hpp
More file actions
157 lines (128 loc) · 3.75 KB
/
ObjectExperimental.hpp
File metadata and controls
157 lines (128 loc) · 3.75 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
#include <string.h>
#include <typeinfo>
class NoWrapObject{
};
class Object{
public:
void* data = nullptr;
size_t data_size = 0;
std::string type;
Object(){
type = "none";
}
/**
* This constructor will do the following:
* 1. Call standard/overloaded copy constructor something(cont something& init)
* 2. store the data in the variable data
* 3. store type of the data
*
* @note this class can appear a bit buggy when used
* with some std::classes, but userdefine schould work mostly
*
* additionaly some memory leaks might apear since deconstructor call is difficult
* but syntax is great :)
*/
template<class value_type,typename = typename std::enable_if<!std::is_base_of<NoWrapObject,value_type>::value >::type>
Object(const value_type& _data) {
type = type(value_type);
data = malloc(sizeof(value_type));
data_size = sizeof(value_type);
new (data) value_type(_data); //this will increase reference count by 1 from smart pointer in Array
}
/**
* Called when cast to something
* will throw runtime_error if not the same type as initialized
*/
operator Object(){
Object out;
out.data_size = data_size;
out.type = type;
out.data = malloc(data_size);
memcpy(out.data,data,data_size);
return out;
}
template<class value_type>
operator Array<value_type>(){
auto requested_type = type(Array<value_type>);
if(type != requested_type){
throw std::runtime_error("Don't want convert " + type + " to " + requested_type);
}
Array<value_type> tmp = *((Array<value_type>*)data);
((Array<value_type>*)data)->arrayRef.reset(); //decrease reference count by 1
return tmp;
}
template<class value_type>
operator value_type(){
auto requested_type = type(value_type);
if(type != requested_type){
throw std::runtime_error("Don't want convert " + type + " to " + requested_type);
}
Array<value_type> tmp = *((value_type*)data);
delete ((value_type*)data);
return tmp;
}
~Object(){
if(data != nullptr){
free(data);
}
}
};
class TupleSubscript;
class Tuple{
public:
Object* data;
size_t len;
template<class... Args>
Tuple(Args const&... args);
TupleSubscript operator[](int idx);
~Tuple(){
//operator delete[](data);
delete[] data;
}
};
class TupleSubscript : NoWrapObject{
public:
Tuple* tuple;
int idx;
TupleSubscript(Tuple* _tuple,int _idx): tuple(_tuple), idx(_idx) {}
template<class value_type>
Tuple& operator=(value_type value){
tuple->data[idx] = Object(value);
return *tuple;
}
/*operator Object(){
return (tuple->data[idx]);
}*/
/*operator Object(){
return (tuple->data[idx]);
}*/
template<class value_type>
operator value_type(){
return (tuple->data[idx]).operator value_type();
}
template<class value_type>
value_type astype(){
return (tuple->data[idx]).operator value_type();
}
};
/**
* @param anything..
*
*/
template<class... Args>
Tuple::Tuple(Args const&... args){
std::initializer_list<Object> _list = {args...};
Object* list = (Object*)_list.begin();
len = _list.size();
data = new Object[len];
//std::cout << "len "<<len << "\n";
for(int k = 0; k < len ; k++){
data[k].data_size = list[k].data_size;
data[k].type = list[k].type;
data[k].data = malloc(list[k].data_size);
memcpy(data[k].data,list[k].data,list[k].data_size);
}
}
TupleSubscript Tuple::operator[](int idx){
return TupleSubscript(this,idx);
}