-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcpps_object.h
More file actions
286 lines (256 loc) · 7.86 KB
/
cpps_object.h
File metadata and controls
286 lines (256 loc) · 7.86 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
#ifndef CPPS_OBJECT_CPPS_HEAD_
#define CPPS_OBJECT_CPPS_HEAD_
//===================================
//@Author : Johnson
//@QQ : 88481106
//@Email : 88481106@qq.com
//@Date : 2015/11/24 (yy/mm/dd)
//@Module : CPPS_OBJECT
//@Description : CppsתC++
//@website : http://cppscript.org
//==================================
#include "cpps_array.h"
#include "cpps_map.h"
namespace cpps
{
struct cpps_domain;
struct cpps_vector;
struct cpps_map;
struct cpps_map;
object function_caller(C* c, object func, cpps_std_vector& paramlist);
object function_caller(C* c, object leftdomain, object func, cpps_std_vector& paramlist);
template<class T>
inline void newclass(C* c, T** ret,cpps_value *ret_value);
struct object
{
object();
object(const object& k);
object(const cpps_value& v);
static object real(object& o);
struct vector
{
public:
static vector create(C* c);
public:
vector(object& obj);
cpps_std_vector::iterator begin();
cpps_std_vector::iterator end();
object begin_obj(C*c);
object end_obj(C* c);
void push_back(object v);
void erase(cpps_integer idx);
size_t size();
void clear();
object toobject();
object operator[](const cpps_integer k);
cpps_std_vector& realvector();
private:
cpps_vector* _vec;
cpps_value _src_value;
};
struct map
{
public:
static map create(C* c);
public:
map(C* cstate, object& obj);
cpps_hash_map::iterator begin();
cpps_hash_map::iterator end();
object begin_obj(C* c);
object end_obj(C* c);
void insert(const object& key, const object& value);
object toobject();
size_t size();
void clear();
template<class T>
bool has(const T k) {
cpps_value key = cpps_cpp_to_cpps_converter<T>::apply(c, k);
return _map->has(key);
}
template<class T>
void erase(const T k) {
cpps_value key = cpps_cpp_to_cpps_converter<T>::apply(c, k);
_map->erase(key);
}
template<class T>
object operator[](const T k) {
cpps_value key = cpps_cpp_to_cpps_converter<T>::apply(c, k);
cpps_value& value = _map->cpps_find(key);
return cpps_value(&value);
}
cpps_hash_map& realmap();
private:
cpps_value _src_value;
cpps_map* _map;
C* c;
};
struct set
{
public:
static set create(C* c);
public:
set(C* cstate, object& obj);
cpps_hash_set::iterator begin();
cpps_hash_set::iterator end();
object begin_obj(C* c);
object end_obj(C* c);
void insert(object& key);
object toobject();
size_t size();
void clear();
template<class T>
bool has(const T k) {
cpps_value key = cpps_cpp_to_cpps_converter<T>::apply(c, k);
return _set->has(key);
}
cpps_hash_set& realset();
private:
cpps_value _src_value;
cpps_set* _set;
C* c;
};
struct pair
{
public:
static pair create(C* c,object &first,object &second);
static pair create(C* c,object &&first,object &&second);
public:
pair(C* cstate, object obj);
object first();
object second();
object toobject();
cpps_pair* _pair;
cpps_value _src_value;
C* c;
};
// define global var.
static void define(C* c, std::string varname, object v = object());
//create object.
//class C is required because the string needs GC.
//
static object create_with_pair(C* c);
static object create_with_map(C* c);
static object create_with_set(C* c);
static object create_with_vector(C* c);
static object create_with_cppsclassvar(C* c,object __classobject);
template<class T>
static object create_with_classvar(C* c, T** ptr) {
object ret;
newclass<T>(c, ptr,&ret.value);
return ret;
}
template<class Type>
static object create(C*c, Type v) {
return object(c, v);
}
template<class Type>
static object string(C*c, Type v) {
return object(c, v);
}
object& operator=(const cpps_value k);
object& operator=(const object& k);
template<typename _T>
object& operator=(_T k)
{
object _obj(k);
*this = _obj;
return *this;
}
//_G root node.
static object globals(C* c);
//check
bool ispair();
bool ismap();
bool isset();
bool isstring();
bool isvector();
bool isrange();
bool istuple();
bool isellipsis();
bool isint();
bool isuint();
bool ischar();
bool isnumber();
bool isnull();
bool isfunction();
bool isclass();
bool isclassvar();
bool isref();
std::string getclassname();
//convert
std::string tostring();
cpps_integer toint();
cpps_uinteger touint();
char& tochar();
cpps_number tonumber();
bool tobool();
object toreal();
cpps_value ref();
cpps_value &realval();
const cpps_value &realval() const;
cpps_function* tofunction();
//class func
template<class... _ArgTypes>
object call(C* c, std::string funcname, _ArgTypes&&... _Args) {
object func = (*this)[funcname];
if (!func.isfunction()) return cpps::nil;
return doclassfunction(c, *this, func, _Args...);
}
object call(C* c, std::string funcname, cpps_std_vector& vec) {
object func = (*this)[funcname];
if (!func.isfunction()) return cpps::nil;
return function_caller(c, *this, func, vec);
}
template<class... _ArgTypes>
object operator () (C* c, _ArgTypes&&... _Args) {
if (!isfunction()) return cpps::nil;
return dofunction(c, *this, _Args...);
}
//vector ,map ,string .
cpps_integer size();
//vector map only.
void clear();
bool empty();
object operator[](const std::string k); // and domain
//map only.
void insert(object key,object val);
//vector only.
void push_back(object& val);
object operator[](const cpps_integer k);
cpps_value& getval();
template<class Type>
object(C* c, Type v)
{
if (!cpps_cpp_to_cpps_converter<Type>::match(c, v))
{
throw(cpps_error(__FILE__, __LINE__, 0, "%s is not defined to script, conversion failed.", typeid(Type).name()));
}
value = cpps_cpp_to_cpps_converter<Type>::apply(c, v);
}
object(bool v) { value = cpps_value((bool)v); }
object(int64 v) { value = cpps_value((cpps_integer)v); }
object(int32 v) { value = cpps_value((cpps_integer)v); }
object(int16 v) { value = cpps_value((cpps_integer)v); }
object(int8 v) { value = cpps_value((cpps_integer)v); }
object(usint8 v) { value = cpps_value((cpps_uinteger)v); }
object(usint16 v) { value = cpps_value((cpps_uinteger)v); }
object(usint32 v) { value = cpps_value((cpps_uinteger)v); }
object(usint64 v) { value = cpps_value((cpps_uinteger)v); }
object(long double v) { value = cpps_value((cpps_number)v); }
object(double v) { value = cpps_value((cpps_number)v); }
object(float v) { value = cpps_value((cpps_number)v); }
object(void *v) { value = cpps_value(v); }
template<typename T>
const bool is_kindof() const {
return value.is_kindof<T>();
}
cpps_value value;
};
int32 type(const object& o);
std::string type_s(const object& o);
int32 type(object&& o);
std::string type_s(object&& o);
void print(C*c,const object& o);
void println(C* c, const object& o);
}
#endif // CPPS_OBJECT_CPPS_HEAD_