-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcpps_regfunctionfmt.h
More file actions
144 lines (130 loc) · 4.94 KB
/
cpps_regfunctionfmt.h
File metadata and controls
144 lines (130 loc) · 4.94 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
#ifndef CPPS_REG_FUNCTIONFMT_CPPS_HEAD_
#define CPPS_REG_FUNCTIONFMT_CPPS_HEAD_
//===================================
//@Author : Johnson
//@QQ : 88481106
//@Email : 88481106@qq.com
//@Date : 2015/11/20 (yy/mm/dd)
//@Module : CPPS_CALL_FUNCTION
//@Description : 注册函数
//@website : http://cppscript.org
//==================================
namespace cpps
{
template<typename p1>
class dofunc;
template<class retType, typename context, typename F, typename IsVoid>
retType call_function(cpps_value &ret, cpps_domain *domain, node*o, context &c, F &f, IsVoid&);
template<class T>
class cpps_is_void;
template <class C>
struct cpps_class;
struct cpps_cppsclass;
struct cpps_cpps_value;
struct Buffer;
#define cpps_def_regfunction 1
#define cpps_def_regclass 2
#define cpps_def_regvar 3
std::string transformat(std::string fmt, cpps_value v);
size_t formatcheck(C *c,std::string &fmt, size_t j, size_t i, std::string &take, cpps_value &v);
template<class R>
struct cpps_functionfmt : public cpps_function
{
cpps_functionfmt(R(*f)(C* c, cpps::cpps_value args, ...))
:f(f)
{
}
virtual int8 getparamcount() { return 1; }
template<typename IsVoid>
void call_function(C* c, cpps_value& vct_value, cpps_value* ret, IsVoid) {
R r = f(c, vct_value);
if (!cpps_cpp_to_cpps_converter<R>::match(c, r))
{
throw(cpps_error("0", 0, 0, "%s is not defined to script, conversion failed.", typeid(R).name()));
}
*ret = cpps_cpp_to_cpps_converter<R>::apply(c, r); //c++的返回值 只有可以转换的才可以
}
void call_function(C* c, cpps_value& vct_value, cpps_value* ret, cpps_is_void<void>) {
f(c, vct_value);
}
void callfunction(C *c, cpps_value *ret, cpps_domain *domain, cpps_std_vector *o, cpps_stack *stack = NULL, std::vector< cpps_regvar*>* lambdastacklist = NULL)
{
cpps_vector* vct = NULL; cpps_value vct_value;
newclass<cpps_vector>(c, &vct, &vct_value);
vct->realvector().assign(o->begin(),o->end());
call_function(c, vct_value, ret, cpps_is_void<R>());
}
R(*f)(C* c, cpps::cpps_value args, ...);
};
template<>
struct cpps_functionfmt<cpps_value*> : public cpps_function
{
cpps_functionfmt(cpps_value* (*f)(C* c, cpps::cpps_value args, ...))
:f(f)
{
}
void call_function(C* c, cpps_value& vct_value, cpps_value* ret) {
cpps_value* r = f(c, vct_value);
ret->tt = r->tt;
ret->value = r->value;
ret->incruse();
}
void callfunction(C* c, cpps_value* ret, cpps_domain* domain, cpps_std_vector* o, cpps_stack* stack = NULL, std::vector< cpps_regvar*>* lambdastacklist = NULL)
{
cpps_vector* vct = NULL; cpps_value vct_value;
newclass<cpps_vector>(c, &vct, &vct_value);
vct->realvector().assign(o->begin(), o->end());
call_function(c, vct_value, ret);
}
cpps_value* (*f)(C* c, cpps::cpps_value args, ...);
};
template<class R,typename CLS>
struct cpps_functionfmt_class : public cpps_function
{
cpps_functionfmt_class(R(CLS::*f)(C* c, cpps::cpps_value args, ...))
:f(f)
{
}
virtual int8 getparamcount() { return 1; }
template<typename IsVoid>
void call_function(C* c, cpps_domain *domain,cpps_value& vct_value, cpps_value* ret, IsVoid) {
if (domain && domain->domainType == cpps_domain_type_classvar)
{
cpps_classvar<CLS>* cls = (cpps_classvar<CLS> *)domain;
R r = (cls->__class->*f)(c, vct_value);
if (!cpps_cpp_to_cpps_converter<R>::match(c, r))
{
throw(cpps_error("0", 0, 0, "%s is not defined to script, conversion failed.", typeid(R).name()));
}
*ret = cpps_cpp_to_cpps_converter<R>::apply(c, r); //c++的返回值 只有可以转换的才可以
}
}
void call_function(C* c, cpps_domain* domain, cpps_value& vct_value, cpps_value* ret, cpps_is_void<void>) {
if (domain && domain->domainType == cpps_domain_type_classvar)
{
cpps_classvar<CLS>* cls = (cpps_classvar<CLS> *)domain;
(cls->__class->*f)(c, vct_value);
}
}
virtual void callfunction(C* c, cpps_value* ret, cpps_domain* domain, cpps_std_vector* o, cpps_stack* stack = NULL, std::vector< cpps_regvar*>* lambdastacklist = NULL)
{
cpps_vector* vct = NULL; cpps_value vct_value;
newclass<cpps_vector>(c, &vct, &vct_value);
vct->realvector().assign(o->begin(), o->end());
call_function(c,domain, vct_value, ret, cpps_is_void<R>());
}
R(CLS::*f)(C* c, cpps::cpps_value args, ...);
};
template<class R>
cpps_regfunction* make_regfunction(std::string func, R(*f)(C*c,cpps::cpps_value args , ...), bool isasync ,bool isoperator )
{
return CPPSNEW( cpps_regfunction)(func, CPPSNEW( cpps_functionfmt<R>)(f),isasync, isoperator);
}
template<class R,typename CLS>
cpps_regfunction* make_regfunction2(std::string func, R(CLS::*f)(C*c,cpps::cpps_value args , ...), bool isasync ,bool isoperator )
{
typedef cpps_functionfmt_class<R, CLS> _Type;
return CPPSNEW( cpps_regfunction)(func, CPPSNEW(_Type)(f),isasync, isoperator);
}
}
#endif