-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCallers.hpp
232 lines (224 loc) · 5.28 KB
/
Callers.hpp
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
/*
* Callers.hpp
*
* Define some native object with lambda members to help simplify the code
* Copyright (C) 2018-2019 khjxiaogu
*
* Author: khjxiaogu
* Web: http://www.khjxiaogu.com
*/
template<typename T>
class PropertyCaller : public tTJSDispatch
{
public:
//getter type and setter type macro
typedef typename std::function<T()> getterT;
typedef typename std::function<void(T)> setterT;
getterT getter;
setterT setter;
//normal construct
PropertyCaller(getterT getter, setterT setter)
: getter(getter), setter(setter) {
}
//Read only property
PropertyCaller(getterT getter, int)
: getter(getter), setter(NULL) {
}
//Write only property
PropertyCaller(int, setterT setter)
: getter(NULL), setter(setter) {
}
tjs_error TJS_INTF_METHOD PropGet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
tTJSVariant* result,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (getter)
*result = tTJSVariant(getter());
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
tjs_error TJS_INTF_METHOD PropSet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tTJSVariant* param,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (setter)
setter(T(*param));
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
tjs_error TJS_INTF_METHOD
IsInstanceOf(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tjs_char* classname,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
else
if (wcscmp(classname, L"Property"))
return TJS_S_TRUE;
return TJS_S_FALSE;
}
};
//For some special type,define a type-convertion function
template <>
tjs_error TJS_INTF_METHOD PropertyCaller<tTJSVariantOctet*> ::PropSet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tTJSVariant* param,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (setter)
setter(param->AsOctetNoAddRef());
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
template <>
tjs_error TJS_INTF_METHOD PropertyCaller<tTJSVariantOctet*> ::PropGet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
tTJSVariant* result,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (getter) {
auto get = getter();
*result = tTJSVariant(get);
get->Release();
}
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
template <>
tjs_error TJS_INTF_METHOD PropertyCaller<iTJSDispatch2*> ::PropSet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tTJSVariant* param,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (setter)
setter(param->AsObjectNoAddRef());
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
template <>
tjs_error TJS_INTF_METHOD PropertyCaller<iTJSDispatch2*> ::PropGet(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
tTJSVariant* result,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
if (getter) {
auto get = getter();
*result = tTJSVariant(get);
get->Release();
}
else
return TJS_E_ACCESSDENYED;
return TJS_S_OK;
}
//Function caller to Iterate Object Members.
class ObjectIterateCaller : public tTJSDispatch
{
public:
std::function<void(tTJSVariant*, tTJSVariant*)>func;
ObjectIterateCaller(std::function<void(tTJSVariant*, tTJSVariant*)> func)
: func(func) {
}
virtual tjs_error TJS_INTF_METHOD FuncCall( // function invocation
tjs_uint32 flag, // calling flag
const tjs_char* membername,// member name ( NULL for a default member )
tjs_uint32* hint, // hint for the member name (in/out)
tTJSVariant* result, // result
tjs_int numparams, // number of parameters
tTJSVariant** param, // parameters
iTJSDispatch2* objthis // object as "this"
) {
if (numparams > 1) {
if ((int)*param[1] != TJS_HIDDENMEMBER) {
func(param[0], param[2]);
}
}
if (result)
*result = true;
return TJS_S_OK;
}
};
//define Native function lambda type
using NativeTJSFunction = std::function<tjs_error(tTJSVariant * r, tjs_int n, tTJSVariant * *p)>;
#define TJSNATIVEARG (tTJSVariant* r, tjs_int n, tTJSVariant** p)->tjs_error
//Function caller to provide a bridge between tjs function and native function
class FunctionCaller : public tTJSDispatch
{
public:
NativeTJSFunction func;
virtual void BeforeDestruction(void) {
TVPAddLog("Function deleted");
}
FunctionCaller(NativeTJSFunction func)
: func(func) {
}
virtual tjs_error TJS_INTF_METHOD FuncCall( // function invocation
tjs_uint32 flag, // calling flag
const tjs_char* membername,// member name ( NULL for a default member )
tjs_uint32* hint, // hint for the member name (in/out)
tTJSVariant* result, // result
tjs_int numparams, // number of parameters
tTJSVariant** param, // parameters
iTJSDispatch2* objthis // object as "this"
) {
if (membername)return TJS_E_MEMBERNOTFOUND;
return func(result, numparams, param);
}
tjs_error TJS_INTF_METHOD
IsInstanceOf(
tjs_uint32 flag,
const tjs_char* membername,
tjs_uint32* hint,
const tjs_char* classname,
iTJSDispatch2* objthis
)
{
if (membername)
return TJS_E_MEMBERNOTFOUND;
else
if (wcscmp(classname, L"Function"))
return TJS_S_TRUE;
return TJS_S_FALSE;
}
};