-
Notifications
You must be signed in to change notification settings - Fork 0
/
arx_luabinder.h
596 lines (499 loc) · 15.2 KB
/
arx_luabinder.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#ifndef SCRIPTER_H_
#define SCRIPTER_H_
#include <cstring>
#include <vector>
#include <stdint.h>
extern "C" {
#include "./depends/lua-5.2.2/lua.h"
#include "./depends/lua-5.2.2/lauxlib.h"
#include "./depends/lua-5.2.2/lualib.h"
}
typedef int (*LuaFunction)(lua_State*);
class ScripterFunctionBase
{
LuaFunction retfunc_;
public:
LuaFunction get_function()
{
return retfunc_;
}
protected:
void set_function(LuaFunction func)
{
retfunc_ = func;
}
};
/******************************************* ScripterLuaHelper *************************************/
/** some template meta-programming stuff **/
template <typename T, bool sz>
struct _IA
{
typedef T& ThisType;
static T& RevCaster(void* t)
{
return *((T*)t);
}
static void* Caster(T t)
{
return (void*)&t;
}
};
template <typename T>
struct _IA<T, true>
{
typedef T ThisType;
static T RevCaster(void* t)
{
return T(t);
}
static void* Caster(T t)
{
return (void*)t;
}
};
/** ^ **/
template <typename T>
class ScripterLuaHelper
{
public:
typedef T& ThisType;
inline static T& pop_data(lua_State* L, long i)
{
return *((T*)lua_touserdata(L, i));
}
inline static int push_data(lua_State* L, T data)
{
lua_pushlightuserdata(L, (void*)(&data));
return 1;
}
};
template <typename T>
class ScripterLuaHelper<T*>
{
public:
typedef T* ThisType;
inline static T* pop_data(lua_State* L, long i)
{
return (T*)lua_touserdata(L, i);
}
inline static int push_data(lua_State* L, T* data)
{
lua_pushlightuserdata(L, (void*)data);
return 1;
}
};
template <>
class ScripterLuaHelper<int>
{
public:
typedef int ThisType;
inline static int pop_data(lua_State* L, long i)
{
return luaL_checkint(L, i);
}
inline static int push_data(lua_State* L, int data)
{
lua_pushinteger(L, data);
return 1;
}
};
template <>
class ScripterLuaHelper<long>
{
public:
typedef long ThisType;
inline static long pop_data(lua_State* L, long i)
{
return luaL_checklong(L, i);
}
inline static int push_data(lua_State* L, long data)
{
lua_pushinteger(L, data);
return 1;
}
};
template <>
class ScripterLuaHelper<double>
{
public:
typedef double ThisType;
inline static double pop_data(lua_State* L, long i)
{
return luaL_checknumber(L, i);
}
inline static int push_data(lua_State* L, double data)
{
lua_pushnumber(L, data);
return 1;
}
};
template <>
class ScripterLuaHelper<float>
{
public:
typedef float ThisType;
inline static float pop_data(lua_State* L, long i)
{
return float(luaL_checknumber(L, i));
}
inline static int push_data(lua_State* L, float data)
{
lua_pushnumber(L, (double)data);
return 1;
}
};
template <>
class ScripterLuaHelper<const char*>
{
public:
typedef const char* ThisType;
inline static const char* pop_data(lua_State* L, long i)
{
return luaL_checkstring(L, i);
}
inline static int push_data(lua_State* L, const char* data)
{
lua_pushstring(L, data);
return 1;
}
};
/******************************************** ScripterFunction *************************************/
template <int ID, typename Ctype, typename RetType = int, typename A = int, typename B = int, typename C = int, typename D = int>
class ScripterFunction : public ScripterFunctionBase
{
typedef ScripterFunction<ID, Ctype, RetType, A, B, C, D> ThisType;
typedef RetType (Ctype::*_func0) ();
typedef RetType (Ctype::*_func1) (A);
typedef RetType (Ctype::*_func2) (A, B);
typedef RetType (Ctype::*_func3) (A, B, C);
typedef RetType (Ctype::*_func4) (A, B, C, D);
/// no arg
static int lua_func0_0(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
RetType ret = (t->*((_func0)t->get_function(ID)))();
ScripterLuaHelper<RetType>::push_data(L, ret);
return 1;
}
static int lua_func0_1(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
RetType ret = (t->*((_func1)t->get_function(ID)))(at);
ScripterLuaHelper<RetType>::push_data(L, ret);
return 1;
}
static int lua_func0_2(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
RetType ret = (t->*((_func2)t->get_function(ID)))(at, bt);
ScripterLuaHelper<RetType>::push_data(L, ret);
return 1;
}
static int lua_func0_3(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
typename ScripterLuaHelper<C>::ThisType ct = ScripterLuaHelper<C>::pop_data(L, 4);
RetType ret = (t->*((_func3)t->get_function(ID)))(at, bt, ct);
ScripterLuaHelper<RetType>::push_data(L, ret);
return 1;
}
static int lua_func0_4(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
typename ScripterLuaHelper<C>::ThisType ct = ScripterLuaHelper<C>::pop_data(L, 4);
typename ScripterLuaHelper<D>::ThisType dt = ScripterLuaHelper<D>::pop_data(L, 5);
RetType ret = (t->*((_func4)t->get_function(ID)))(at, bt, ct, dt);
ScripterLuaHelper<RetType>::push_data(L, ret);
return 1;
}
public:
ScripterFunction(RetType (Ctype::*func)())
{
this->set_function(lua_func0_0);
}
ScripterFunction(RetType (Ctype::*func)(A))
{
this->set_function(lua_func0_1);
}
ScripterFunction(RetType (Ctype::*func)(A, B))
{
this->set_function(lua_func0_2);
}
ScripterFunction(RetType (Ctype::*func)(A, B, C))
{
this->set_function(lua_func0_3);
}
ScripterFunction(RetType (Ctype::*func)(A, B, C, D))
{
this->set_function(lua_func0_4);
}
};
/*************************** for functions that is 'void' **************************************/
template <int ID, typename Ctype, typename A, typename B, typename C, typename D>
class ScripterFunction<ID, Ctype, void, A, B, C, D> : public ScripterFunctionBase
{
typedef void (Ctype::*_func0) ();
typedef void (Ctype::*_func1) (A);
typedef void (Ctype::*_func2) (A, B);
typedef void (Ctype::*_func3) (A, B, C);
typedef void (Ctype::*_func4) (A, B, C, D);
/// no arg
static int lua_func0_0(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
(t->*((_func0)t->get_function(ID)))();
return 0;
}
static int lua_func0_1(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
(t->*((_func1)t->get_function(ID)))(at);
return 0;
}
static int lua_func0_2(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
(t->*((_func2)t->get_function(ID)))(at, bt);
return 0;
}
static int lua_func0_3(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
typename ScripterLuaHelper<C>::ThisType ct = ScripterLuaHelper<C>::pop_data(L, 4);
(t->*((_func3)t->get_function(ID)))(at, bt, ct);
return 0;
}
static int lua_func0_4(lua_State* L)
{
Ctype* t = (Ctype*) lua_touserdata(L, 1);
typename ScripterLuaHelper<A>::ThisType at = ScripterLuaHelper<A>::pop_data(L, 2);
typename ScripterLuaHelper<B>::ThisType bt = ScripterLuaHelper<B>::pop_data(L, 3);
typename ScripterLuaHelper<C>::ThisType ct = ScripterLuaHelper<C>::pop_data(L, 4);
typename ScripterLuaHelper<D>::ThisType dt = ScripterLuaHelper<D>::pop_data(L, 5);
(t->*((_func4)t->get_function(ID)))(at, bt, ct, dt);
return 0;
}
public:
ScripterFunction(void (Ctype::*func)())
{
this->set_function(lua_func0_0);
}
ScripterFunction(void (Ctype::*func)(A))
{
this->set_function(lua_func0_1);
}
ScripterFunction(void (Ctype::*func)(A, B))
{
this->set_function(lua_func0_2);
}
ScripterFunction(void (Ctype::*func)(A, B, C))
{
this->set_function(lua_func0_3);
}
ScripterFunction(void (Ctype::*func)(A, B, C, D))
{
this->set_function(lua_func0_4);
}
};
template<typename T> class ScripterReg;
template <int ID, uint32_t function_count, int strsz = 32 >
class ScripterData
{
typedef int (ScripterData<ID, function_count, strsz>::*_df)();
static char name_[ strsz ];
static _df fptr_[function_count];
protected:
template<typename T> friend class ScripterReg;
public:
typedef _df FType;
ScripterData<ID, function_count, strsz>::_df get_function(uint32_t index)
{
return fptr_[index];
}
uint32_t add_func(uint32_t id, int (ScripterData<ID, function_count, strsz>::*func)())
{
fptr_[id] = (_df)func;
return id;
}
template <typename T>
void N() { }
void callfunc(uint32_t i)
{
(this->*(this->get_function(i)))();
}
uint32_t fsize() const
{
return function_count;
}
void copy_to(void* mem)
{
memcpy(mem, this, sizeof(ScripterData<ID, function_count, strsz>));
}
int test()
{
std::cout << "called." << std::endl;
return 0;
}
};
template < int ID, uint32_t function_count, int strsz >
char ScripterData< ID, function_count, strsz>::name_[ strsz ];
template < int ID, uint32_t function_count, int strsz >
typename ScripterData<ID, function_count, strsz>::_df ScripterData<ID, function_count, strsz>::fptr_[function_count];
template <typename T>
class ScripterReg
{
struct lua_reg : public luaL_Reg
{
public:
lua_reg()
{
name = NULL;
func = NULL;
}
void set(const char* s, lua_CFunction f)
{
name = s;
func = f;
}
};
T *ptr_;
uint32_t index_;
std::string clsname_;
std::vector<lua_reg> array_;
std::vector<lua_reg> arrayt_;
static int pf_new(lua_State* s)
{
T* t = (T*) lua_newuserdata(s, sizeof(T));
new ((void*)t) T();
luaL_getmetatable(s, t->name_);
lua_setmetatable(s, -2);
return 1;
}
public:
ScripterReg(T& t, const char* name) : index_(0)
{
std::string name_ = "LuaBook.";
name_ += name;
clsname_ = name;
ptr_ = &t;
strcpy(ptr_->name_, name_.c_str());
array_.resize( t.fsize()+1, lua_reg());
arrayt_.resize( 2, lua_reg());
arrayt_[0].set("new", pf_new);
}
template <uint32_t ID, typename RetType>
uint32_t add_func(RetType (T::*func)(), const char* name)
{
ScripterFunction<ID, T, RetType> temp(func);
array_[index_++].set(name, temp.get_function());
ptr_->add_func(ID, (typename T::FType)func);
return 0;
}
template <uint32_t ID, typename RetType, typename A>
uint32_t add_func(RetType (T::*func)(A), const char* name)
{
ScripterFunction<ID, T, RetType, A> temp(func);
array_[index_++].set(name, temp.get_function());
ptr_->add_func(ID, (typename T::FType)func);
return 0;
}
template <uint32_t ID, typename RetType, typename A, typename B>
uint32_t add_func(RetType (T::*func)(A, B), const char* name)
{
ScripterFunction<ID, T, RetType, A, B> temp(func);
array_[index_++].set(name, temp.get_function());
ptr_->add_func(ID, (typename T::FType)func);
return 0;
}
template <uint32_t ID, typename RetType, typename A, typename B, typename C>
uint32_t add_func(RetType (T::*func)(A, B, C), const char* name)
{
ScripterFunction<ID, T, RetType, A, B, C> temp(func);
array_[index_++].set(name, temp.get_function());
ptr_->add_func(ID, (typename T::FType)func);
return 0;
}
template <uint32_t ID, typename RetType, typename A, typename B, typename C, typename D>
uint32_t add_func(RetType (T::*func)(A, B, C, D), const char* name)
{
ScripterFunction<ID, T, RetType, A, B, C, D> temp(func);
array_[index_++].set(name, temp.get_function());
ptr_->add_func(ID, (typename T::FType)func);
return 0;
}
void register_type(lua_State* handle)
{
luaL_newmetatable(handle, ptr_->name_);
lua_pushvalue(handle, -1);
lua_setfield(handle, -2, "__index");
//luaL_register(handle, NULL, array_.data());
//luaL_register(handle, clsname_.c_str(), arrayt_.data());
luaL_setfuncs( handle, array_.data(), 0);
lua_newtable( handle);
luaL_setfuncs( handle, arrayt_.data(), 0);
lua_pushvalue( handle, -1);
lua_setglobal( handle, clsname_.data() );
}
};
class ScripterModule
{
typedef void (*ft_error)(const char*);
ft_error f_error;
lua_State *L_;
public:
ScripterModule() : f_error(NULL), L_(NULL)
{
L_ = luaL_newstate();
}
void open_libs()
{
luaL_openlibs(L_);
}
virtual ~ScripterModule()
{
lua_close(L_);
}
operator lua_State*() const
{
return L_;
}
lua_State *get_handle() const
{
return L_;
}
void reg_function(int (*func)(lua_State*), const char* name)
{
lua_register(L_, name, func);
}
void run_file(const char* filename)
{
if(luaL_loadfile(L_, filename) == 0)
{
if(lua_pcall(L_, 0, LUA_MULTRET, 0) != 0)
{
if(f_error != NULL)
{
f_error( lua_tostring(L_, -1) );
lua_pop(L_, 1);
}
}
}
}
void error_handler(ft_error t)
{
f_error = t;
}
};
#endif