-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcpps_module.h
More file actions
279 lines (238 loc) · 6.5 KB
/
cpps_module.h
File metadata and controls
279 lines (238 loc) · 6.5 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
#ifndef CPPS_MODULE_CPPS_HEAD_
#define CPPS_MODULE_CPPS_HEAD_
//===================================
//@Author : Johnson
//@QQ : 88481106
//@Email : 88481106@qq.com
//@Date : 2015/11/20 (yy/mm/dd)
//@Module : CPPS_MODULE
//@Description : Cpp注册到Cpps接口
//@website : http://cppscript.org
//==================================
namespace cpps
{
template<class R, class CLS>
cpps_regfunction* make_regfunction2(std::string func, R(CLS::* f)(C* c, cpps::cpps_value args, ...), bool isasync, bool isoperator);
void cpps_debug_trace_domain(C* c, int kg, std::string parent, cpps_domain* root);
struct cpps_reg_class
{
cpps_reg* f;
};
struct regxmodule
{
regxmodule(cpps_reg_class c)
:f(c.f)
{
f->isneedC = false;
}
regxmodule( cpps_regfunction* f,bool b )
:f(f)
{
f->isneedC = b;
}
regxmodule(cpps_reggvar* f, bool b)
:f(f)
{
f->isneedC = b;
}
regxmodule operator ,(regxmodule c)
{
if (!f->next)
{
f->next = c.f;
}
else
{
cpps_reg* next = f->next;
while (next->next)
next = next->next;
next->next = c.f;
}
return *this;
}
cpps_reg* f;
};
struct _enum : public cpps_reg_class
{
_enum(C*cstate,std::string name)
{
c = cstate;
_enum_domain = CPPSNEW(cpps_domain)(NULL, cpps_domain_type_enum, name.c_str());
f = CPPSNEW(cpps_regenum)(name, _enum_domain);
}
_enum& value(std::string varname, cpps_integer v);
cpps_domain* _enum_domain;
C* c;
};
template< class CLS>
struct _class : public cpps_reg_class
{
_class(std::string name)
{
//assert(!cpps_class_singleton<CLS*>::instance()->getcls());
_cls = CPPSNEW( cpps_class<CLS>)(name,NULL, cpps_domain_type_class);
f = CPPSNEW( cpps_regclass_template<CLS>)(name, _cls);
cpps_class_singleton<CLS*>::instance()->setsls(_cls);
}
template<class F>
_class<CLS>& base()
{
assert(cpps_class_singleton<F*>::instance()->getcls());
cpps_reg* r = make_parentclass(cpps_class_singleton<F*>::instance()->getcls());
r->isneedC = false;
_cls->regfunc(r);
return *this;
}
template<class F>
_class<CLS>& def(std::string func, F _f, bool isasync = false)
{
cpps_reg* r = make_regfunction(func, _f,isasync);
r->isneedC = false;
_cls->regfunc( r );
return *this;
}
template<class F>
_class<CLS>& defvar(C *c, std::string name, F v)
{
cpps_reg* r = make_regvar(name, cpps_cpp_to_cpps_converter<F>::apply(c, v));
r->isneedC = false;
_cls->regfunc(r);
return *this;
}
_class<CLS>& def_classvar( std::string name, object CLS::* v)
{
cpps_reg* r = make_regclassvar(name,v);
_cls->regfunc(r);
return *this;
}
template<class F>
_class<CLS>& def_inside(std::string func, F _f, bool isasync = false)
{
cpps_reg* r = make_regfunction(func, _f,isasync);
r->isneedC = true;
_cls->regfunc(r);
return *this;
}
template<class F>
_class<CLS>& def_operator(std::string func, F _f)
{
cpps_reg* r = make_regfunction(func, _f, false,true);
r->isneedC = false;
_cls->regfunc(r);
return *this;
}
template<class F>
_class<CLS>& def_operator_inside(std::string func, F _f)
{
cpps_reg* r = make_regfunction(func, _f, false, true);
r->isneedC = true;
_cls->regfunc(r);
return *this;
}
regxmodule operator ,(regxmodule c)
{
regxmodule(*this).operator,(c);
return *this;
}
cpps_class<CLS> *_cls;
};
struct _class2 : public cpps_reg_class
{
_class2(std::string name, cpps_class_alloc __alloc, cpps_class_free __free)
{
_cls = CPPSNEW(cpps_class2)(name, NULL, cpps_domain_type_class, __alloc, __free);
f = CPPSNEW(cpps_regclass)(name, _cls);
cpps_class_singleton<cpps_class_handler*>::instance()->setsls(_cls);
}
_class2& def_inside(std::string func, cpps_class_func _f2, bool isasync = false)
{
cpps_reg* r = make_regfunction2(func, &cpps_class_handler::def_call, isasync,false);
r->isneedC = true;
_cls->regfunc(r);
_cls->bind_func(func, _f2);
return *this;
}
_class2& def_operator_inside(std::string func, cpps_class_func _f2)
{
cpps_reg* r = make_regfunction(func, &cpps_class_handler::def_operator_call, false, true);
r->isneedC = true;
_cls->regfunc(r);
_cls->bind_func(func, _f2);
return *this;
}
regxmodule operator ,(regxmodule c)
{
regxmodule(*this).operator,(c);
return *this;
}
cpps_class2* _cls;
};
template<class F>
regxmodule def(std::string func, F f, bool isasync = false)
{
return regxmodule(make_regfunction(func, f, isasync,false),false);
}
template<class F>
regxmodule defvar(C *c,std::string name, F v)
{
return regxmodule(make_regvar(name, cpps_cpp_to_cpps_converter<F>::apply(c,v)), false);
}
template<class F>
regxmodule def_inside(std::string func, F f, bool isasync = false)
{
return regxmodule(make_regfunction(func, f,isasync), true);
}
struct cpps_module
{
cpps_module(C* c,std::string _domain,bool isinit) //默认注册给_G
{
cState = c;
domain = cState->_G;//默认是根节点
if (isinit) {
if (!_domain.empty())//如果定义名字了 那就是自己的一个。。。
{
cpps_domain* leftdomain = NULL;
cpps_regvar* v = domain->getvar(_domain, leftdomain);
if (!v)
{
cpps_domain* temp_domain = CPPSNEW(cpps_domain)(c->_G, cpps_domain_type_module, _domain.c_str());//创建根节点域
v = CPPSNEW(cpps_regvar)();//_G 为根节点
v->setvarname(_domain);
auto temp = cpps_value(temp_domain);
v->setval(temp); //域列表会copy进去
domain->regvar(NULL, v); //将自己注册成_G..
v->setsource(true);
}
domain = v->getval().value.domain;
}
}
else {
if (!_domain.empty())//如果定义名字了 那就是自己的一个。。。
{
cpps_domain* leftdomain = NULL;
cpps_regvar* v = domain->getvar(_domain, leftdomain);
if (v)
{
cpps_domain* temp_domain = v->getval().value.domain;
if (v->getval().isdomain()) {
temp_domain->destory(c, true);
}
temp_domain->release();
domain->unregvar(c, v);
v->release();
}
}
}
}
void operator [](regxmodule m)
{
domain->regfunc(m.f,cState);
}
public:
C *cState;
cpps_domain *domain; //根节点
};
cpps_module _module(C* c, std::string _domain = "");
void _unmodule(C* c, std::string _domain);
}
#endif // CPPS_MODULE_CPPS_HEAD_