-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathluaFunction.h
executable file
·358 lines (306 loc) · 9.39 KB
/
luaFunction.h
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
Copyright (C) <2012> <huangweilook@21cn.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* brief : 注册全局函数
* Author : huangwei
*/
#ifndef _LUAFUNCTION_H
#define _LUAFUNCTION_H
#include "utility.h"
#include "any.h"
#include <iostream>
#include <vector>
namespace luacpp{
template<typename T>
void push_obj(lua_State *L,const T &obj);
//取出栈顶的值,通知将其出栈
template<typename T>
T popvalue(lua_State *L);
template<typename Ret>
class doLuaCall
{
public:
static Ret doCall(lua_State *L,int nArgs,int errFunc,bool callByObject = false)
{
typedef LOKI_TYPELIST_1(void) voidType;
return call(L,nArgs,errFunc,Int2Type<IndexOf<voidType,Ret>::value == 0>(),callByObject);
}
private:
//返回void
static Ret call(lua_State *L,int nArgs,int errFunc,Int2Type<true>,bool callByObject)
{
if(lua_pcall(L, nArgs, 0, errFunc ) != 0)
{
const char *error = lua_tostring(L,-1);
std::string err(error);
lua_pop(L,1);
throw err;
}
if(callByObject)
lua_pop(L,1);
}
//有返回值
static Ret call(lua_State *L,int nArgs,int errFunc,Int2Type<false>,bool callByObject)
{
Ret ret = _call(L,nArgs,errFunc,callByObject);
if(callByObject)
lua_pop(L,1);
return ret;
}
//只有一个返回值
static Ret _call(lua_State *L,int nArgs,int errFunc,bool callByObject)
{
if(lua_pcall(L, nArgs, 1, errFunc ) != 0)
{
const char *error = lua_tostring(L,-1);
printf("%s\n",error);
std::string err(error);
lua_pop(L,1);
throw err;
return Ret();
}
return popvalue<Ret>(L);
}
};
#ifndef PUSH_FUNCTOR
#define PUSH_FUNCTOR do{\
lua_pushlightuserdata(L, (void*)_func);\
lua_pushcclosure(L,lua_cfunction,1);\
lua_setglobal(L, name);\
}while(0)
#endif
template<typename FUNCTOR>
class funbinder{};
//用于注册全局函数
//无参
template<typename Ret>
class funbinder<Ret(*)()>
{
public:
static void pushfuctor(lua_State *L,const char *name,Ret(*_func)())
{
PUSH_FUNCTOR;
}
static int lua_cfunction(lua_State *L)
{
return doCall<Ret>(L,(__func)lua_touserdata(L,lua_upvalueindex(1)),Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(*__func)();
template<typename Result>
static int doCall(lua_State *L,__func func,Int2Type<false>)
{
push_obj<Result>(L,func());
return 1;
}
template<typename Result>
static int doCall(lua_State *L,__func func,Int2Type<true>)
{
func();
return 0;
}
};
//单参
template<typename Ret,typename Arg1>
class funbinder<Ret(*)(Arg1)>
{
private:
typedef typename refTraits<Arg1>::RefType arg1_ref_type;
public:
static void pushfuctor(lua_State *L,const char *name,Ret(*_func)(Arg1))
{
PUSH_FUNCTOR;
}
static int lua_cfunction(lua_State *L)
{
typename GetReplaceType<Arg1>::type tmp_arg1 = popvalue<typename GetReplaceType<Arg1>::type>(L);
Arg1 arg1 = GetRawValue<typename GetReplaceType<Arg1>::type>(tmp_arg1);
return doCall<Ret>(L,arg1,(__func)lua_touserdata(L,lua_upvalueindex(1)),Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(*__func)(Arg1);
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,__func func,Int2Type<false>)
{
push_obj<Result>(L,func(arg1));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,__func func,Int2Type<true>)
{
func(arg1);
return 0;
}
};
//2参
template<typename Ret,typename Arg1,typename Arg2>
class funbinder<Ret(*)(Arg1,Arg2)>
{
public:
static void pushfuctor(lua_State *L,const char *name,Ret(*_func)(Arg1,Arg2))
{
PUSH_FUNCTOR;
}
static int lua_cfunction(lua_State *L)
{
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
typename GetReplaceType<Arg1>::type tmp_arg1 = popvalue<typename GetReplaceType<Arg1>::type>(L);
Arg1 arg1 = GetRawValue<typename GetReplaceType<Arg1>::type>(tmp_arg1);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
return doCall<Ret>(L,arg1,arg2,(__func)lua_touserdata(L,lua_upvalueindex(1)),Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(*__func)(Arg1,Arg2);
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,__func func,Int2Type<false>)
{
push_obj<Result>(L,func(arg1,arg2));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,__func func,Int2Type<true>)
{
func(arg1,arg2);
return 0;
}
};
//3参
template<typename Ret,typename Arg1,typename Arg2,typename Arg3>
class funbinder<Ret(*)(Arg1,Arg2,Arg3)>
{
public:
static void pushfuctor(lua_State *L,const char *name,Ret(*_func)(Arg1,Arg2,Arg3))
{
PUSH_FUNCTOR;
}
static int lua_cfunction(lua_State *L)
{
typename GetReplaceType<Arg3>::type tmp_arg3 = popvalue<typename GetReplaceType<Arg3>::type>(L);
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
typename GetReplaceType<Arg1>::type tmp_arg1 = popvalue<typename GetReplaceType<Arg1>::type>(L);
Arg1 arg1 = GetRawValue<typename GetReplaceType<Arg1>::type>(tmp_arg1);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
Arg3 arg3 = GetRawValue<typename GetReplaceType<Arg3>::type>(tmp_arg3);
return doCall<Ret>(L,arg1,arg2,arg3,(__func)lua_touserdata(L,lua_upvalueindex(1)),Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(*__func)(Arg1,Arg2,Arg3);
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,__func func,Int2Type<false>)
{
push_obj<Result>(L,func(arg1,arg2,arg3));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,__func func,Int2Type<true>)
{
func(arg1,arg2,arg3);
return 0;
}
};
//4参
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
class funbinder<Ret(*)(Arg1,Arg2,Arg3,Arg4)>
{
public:
static void pushfuctor(lua_State *L,const char *name,Ret(*_func)(Arg1,Arg2,Arg3,Arg4))
{
PUSH_FUNCTOR;
}
static int lua_cfunction(lua_State *L)
{
typename GetReplaceType<Arg4>::type tmp_arg4 = popvalue<typename GetReplaceType<Arg4>::type>(L);
typename GetReplaceType<Arg3>::type tmp_arg3 = popvalue<typename GetReplaceType<Arg3>::type>(L);
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
typename GetReplaceType<Arg1>::type tmp_arg1 = popvalue<typename GetReplaceType<Arg1>::type>(L);
Arg1 arg1 = GetRawValue<typename GetReplaceType<Arg1>::type>(tmp_arg1);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
Arg3 arg3 = GetRawValue<typename GetReplaceType<Arg3>::type>(tmp_arg3);
Arg4 arg4 = GetRawValue<typename GetReplaceType<Arg4>::type>(tmp_arg4);
return doCall<Ret>(L,arg1,arg2,arg3,arg4,(__func)lua_touserdata(L,lua_upvalueindex(1)),Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(*__func)(Arg1,Arg2,Arg3,Arg4);
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,const Arg4 &arg4,__func func,Int2Type<false>)
{
push_obj<Result>(L,func(arg1,arg2,arg3,arg4));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,const Arg4 &arg4,__func func,Int2Type<true>)
{
func(arg1,arg2,arg3,arg4);
return 0;
}
};
template<typename FUNTOR>
void reg_cfun(lua_State *L,const char *name, FUNTOR _func)
{
funbinder<FUNTOR>::pushfuctor(L,name,_func);
}
inline void check_call(lua_State *L,const char *funname)
{
lua_getglobal(L,funname);
if(LUA_TFUNCTION != lua_type(L,-1))
{
lua_pop(L,1);
char str[512];
snprintf(str,512,"lua中不存在函数%s",funname);
std::string err(str);
throw err;
}
}
template<typename Ret>
Ret call(lua_State *L,const char *funname)
{
check_call(L,funname);
return doLuaCall<Ret>::doCall(L,0,0);
}
template<typename Ret,typename Arg1>
Ret call(lua_State *L,const char *funname,const Arg1 &arg1)
{
check_call(L,funname);
push_obj<Arg1>(L,arg1);
return doLuaCall<Ret>::doCall(L,1,0);
}
template<typename Ret,typename Arg1,typename Arg2>
Ret call(lua_State *L,const char *funname,const Arg1 &arg1,const Arg2 &arg2)
{
check_call(L,funname);
push_obj<Arg1>(L,arg1);
push_obj<Arg2>(L,arg2);
return doLuaCall<Ret>::doCall(L,2,0);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3>
Ret call(lua_State *L,const char *funname,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3)
{
check_call(L,funname);
push_obj<Arg1>(L,arg1);
push_obj<Arg2>(L,arg2);
push_obj<Arg3>(L,arg3);
return doLuaCall<Ret>::doCall(L,3,0);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
Ret call(lua_State *L,const char *funname,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,const Arg4 &arg4)
{
check_call(L,funname);
push_obj<Arg1>(L,arg1);
push_obj<Arg2>(L,arg2);
push_obj<Arg3>(L,arg3);
push_obj<Arg4>(L,arg4);
return doLuaCall<Ret>::doCall(L,4,0);
}
}
#endif