-
Notifications
You must be signed in to change notification settings - Fork 49
/
luaClass.h
executable file
·796 lines (692 loc) · 23.2 KB
/
luaClass.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#ifndef _LUACLASS_H
#define _LUACLASS_H
#include <string.h>
#include <map>
#include <set>
#include "luacommon.h"
#include "luaObject.h"
#include "any.h"
#include "luaFunction.h"
namespace luacpp{
template<typename T>
void push_obj(lua_State *L,const T &obj);
void set_userdata(lua_State *L,void *ptr,int index);
extern std::map<void*,void*> ptrToUserData;
extern std::set<void*> userdataSet;
enum{
MEMBER_FUNCTION = 1,
STATIC_FUNCTION,
MEMBER_FIELD,
CONSTRUCTOR,
};
template <typename T>
struct memberfield
{
typedef void (*property_getter)(T *,lua_State*,void *(T::*));//for property get
typedef void (*property_setter)(T *,lua_State*,void *(T::*));//for property set
char tt;
union{
struct {
property_getter getter;
property_setter setter;
void *(T::*property);
};
struct {
void (T::*mfunction)(void); //member func
lua_CFunction mlua_func;
};
struct {
void (*sfunction)(void); //static func
lua_CFunction slua_func;
};
void (*constructor)(lua_State *L);
};
memberfield() {
memset(this,0,sizeof(*this));
}
template<typename PARENT>
memberfield(const memberfield<PARENT> &p){
memcpy(this,&p,sizeof(p));
}
};
template<typename T>
class objUserData;
template<typename T,typename property_type>
static void GetPropertyData(T *self,lua_State *L,Int2Type<true>,void*(T::*field))
{
//for obj type
push_obj<property_type*>(L,(property_type*)&(self->*field));
}
template<typename T,typename property_type>
static void GetPropertyData(T *self,lua_State *L,Int2Type<false>,void*(T::*property))
{
push_obj<property_type>(L,*(property_type*)&(self->*property));
}
template<typename T,typename property_type>
static void _GetProperty(T *self,lua_State *L,void*(T::*property),Int2Type<false>)
{
//for other type
GetPropertyData<T,property_type>(self,L,Int2Type<!pointerTraits<property_type>::isPointer\
&& IndexOf<SupportType,typename pointerTraits<property_type>::PointeeType>::value == -1>(),property);
}
template<typename T,typename property_type>
static void _GetProperty(T *self,lua_State *L,void*(T::*property),Int2Type<true>)
{
//for luatable
luatable *lt_ptr = (luatable*)(&(self->*property));
push_obj<luatable>(L,*lt_ptr);
}
template<typename T,typename property_type>
static void GetProperty(T *self,lua_State *L,void*(T::*property) )
{
typedef LOKI_TYPELIST_1(luatable) lt;
_GetProperty<T,property_type>(self,L,property,Int2Type<IndexOf<lt,property_type>::value==0>());
}
template<typename T,typename property_type>
static void SetPropertyData(T *self,lua_State *L,Int2Type<true>,void*(T::*property))
{
//empty function for obj type
}
template<typename T,typename property_type>
static void SetPropertyData(T *self,lua_State *L,Int2Type<false>,void*(T::*property))
{
property_type new_value;
new_value = *(property_type*)(&(self->*property)) = popvalue<property_type>(L);
push_obj<property_type>(L,new_value);
}
template<typename CLASS_TYPE,typename property_type>
struct Seter
{
Seter(CLASS_TYPE *self,lua_State *L,void*(CLASS_TYPE::*property))
{
SetPropertyData<CLASS_TYPE,property_type>(self,L,Int2Type<IndexOf<SupportType,\
typename pointerTraits<property_type>::PointeeType>::value == -1>(),property);
}
};
template<typename CLASS_TYPE,typename property_type>
struct Seter<CLASS_TYPE,const property_type*>
{
Seter(CLASS_TYPE *self,lua_State *L,void*(CLASS_TYPE::*property)){}
};
template<typename CLASS_TYPE>
struct Seter<CLASS_TYPE,const char*>
{
Seter(CLASS_TYPE *self,lua_State *L,void*(CLASS_TYPE::*property)){}
};
template<typename CLASS_TYPE>
struct Seter<CLASS_TYPE,char*>
{
Seter(CLASS_TYPE *self,lua_State *L,void*(CLASS_TYPE::*property)){}
};
void get_luatable(luatable &,lua_State *L);
template<typename CLASS_TYPE>
struct Seter<CLASS_TYPE,luatable>
{
Seter(CLASS_TYPE *self,lua_State *L,void*(CLASS_TYPE::*property))
{
luatable *lt_ptr = (luatable*)(&(self->*property));
get_luatable(*lt_ptr,L);
}
};
template<typename T,typename property_type>
static void SetProperty(T *self,lua_State *L,void*(T::*property) )
{
Seter<T,property_type> seter(self,L,property);
}
template<typename T>
class luaClassWrapper
{
friend class objUserData<T>;
public:
static void luaopen_objlib(lua_State *L) {
luaL_Reg meta[] = {
{"__gc",&objUserData<T>::on_gc},
{"__index",&objUserData<T>::Index},
{"__newindex",&objUserData<T>::NewIndex},
//{"__eq",&objUserData<T>::Eq},
{NULL, NULL}
};
luaL_newmetatable(L, luaRegisterClass<T>::GetClassName());
luaL_setfuncs(L, meta, 0);
lua_pop(L,1);
}
static std::map<std::string,memberfield<T> > &GetAllFields()
{
return fields;
}
static void InsertFields(const char *name,memberfield<T> &mf)
{
fields.insert(std::make_pair(std::string(name),mf));
}
static int InsertConstructors(int arg_count,memberfield<T> &mf)
{
if(constructors[arg_count].constructor == NULL)
{
constructors[arg_count] = mf;
++constructor_size;
return constructor_size;
}
return -1;
}
private:
static std::map<std::string,memberfield<T> > fields;
static int constructor_size;
static memberfield<T> constructors[16];
};
template<typename T>
std::map<std::string,memberfield<T> > luaClassWrapper<T>::fields;
template<typename T>
int luaClassWrapper<T>::constructor_size = 0;
template<typename T>
memberfield<T> luaClassWrapper<T>::constructors[16];
extern int pushObj(lua_State *L,const void *ptr,const char *classname);
template<typename T>
class objUserData
{
public:
static objUserData<T> *checkobjuserdata (lua_State *L,int index) {
void *ud = lua_touserdata(L,index);
luaL_argcheck(L, ud != NULL, 1, "userdata expected");
return (objUserData<T> *)ud;
}
static int pushObj(lua_State *L,const T *ptr)
{
return luacpp::pushObj(L,ptr,luaRegisterClass<T>::GetClassName());
}
static int NewIndex(lua_State *L)
{
objUserData<T> *self = checkobjuserdata(L,1);
const char *name = luaL_checkstring(L, 2);
typename std::map<std::string,memberfield<T> >::iterator it = luaClassWrapper<T>::fields.find(std::string(name));
if(it != luaClassWrapper<T>::fields.end())
{
memberfield<T> &mf = it->second;
if(mf.tt == MEMBER_FIELD) mf.setter(self->ptr,L,mf.property);
}
return 0;
}
static int Index(lua_State *L)
{
objUserData<T> *obj = checkobjuserdata(L,1);
const char *name = luaL_checkstring(L, 2);
typename std::map<std::string,memberfield<T> >::iterator it = luaClassWrapper<T>::fields.find(std::string(name));
if(it != luaClassWrapper<T>::fields.end())
{
memberfield<T> &mf = it->second;
if(mf.tt == MEMBER_FUNCTION)
{
lua_pushlightuserdata(L,&mf.mfunction);
lua_pushcclosure(L,mf.mlua_func,1);
return 1;
}
else if(mf.tt == STATIC_FUNCTION)
{
lua_pushlightuserdata(L,(void*)mf.sfunction);
lua_pushcclosure(L,mf.slua_func,1);
return 1;
}
else if(mf.tt == MEMBER_FIELD)
{
mf.getter(obj->ptr,L,mf.property);
return 1;
}
}
return 0;
}
static int Eq(lua_State *L) {
if(lua_type(L,1) != lua_type(L,2)) {
lua_pushboolean(L,0);
return 1;
}
objUserData<void*>* left = (objUserData<void*>*)lua_touserdata(L,1);
objUserData<void*>* right = (objUserData<void*>*)lua_touserdata(L,2);
lua_pushboolean(L,left->ptr == right->ptr ? 1:0);
return 1;
}
static int Construct(lua_State *L)
{
int arg_size = lua_gettop(L);
if(arg_size < 16 && luaClassWrapper<T>::constructors[arg_size].constructor)
{
luaClassWrapper<T>::constructors[arg_size].constructor(L);
luaL_setmetatable(L,luaRegisterClass<T>::GetClassName());
}
else
{
char str[512];
snprintf(str,512,"%s: unsupport %d arguments constructor\n",luaRegisterClass<T>::GetClassName(),arg_size);
luaL_error(L,str);
}
return 1;
}
static int on_gc(lua_State *L)
{
objUserData<T> *self = (objUserData<T> *)lua_touserdata(L,-1);
if(self->ptr)
{
ptrToUserData.erase((void*)self->ptr);
userdataSet.erase((void*)self);
if(self->construct_by_lua)
{
delete self->ptr;
}
self->ptr = NULL;
}
return 0;
}
public:
T *ptr;
bool construct_by_lua;
};
#ifndef _GETFUNC
#define _GETFUNC (*(__func*)lua_touserdata(L,lua_upvalueindex(1)))
#endif
template<typename FUNC>
class memberfunbinder{};
template<typename Ret,typename Cla>
class memberfunbinder<Ret(Cla::*)()>
{
public:
typedef Cla CLASS_TYPE;
static int lua_cfunction(lua_State *L)
{
objUserData<Cla> *obj = objUserData<Cla>::checkobjuserdata(L,1);
Cla *cla = obj->ptr;
return doCall<Ret>(L,cla,Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(Cla::*__func)();
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Int2Type<false>)
{
push_obj<Result>(L,(cla->*_GETFUNC)());
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Int2Type<true>)
{
(cla->*_GETFUNC)();
return 0;
}
};
template<typename Ret,typename Arg1,typename Cla>
class memberfunbinder<Ret(Cla::*)(Arg1)>
{
public:
typedef Cla CLASS_TYPE;
static int lua_cfunction(lua_State *L)
{
objUserData<Cla> *obj = objUserData<Cla>::checkobjuserdata(L,1);
Cla *cla = obj->ptr;
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,cla,arg1,Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(Cla::*__func)(Arg1);
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Int2Type<false>)
{
push_obj<Result>(L,(cla->*_GETFUNC)(arg1));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Int2Type<true>)
{
(cla->*_GETFUNC)(arg1);
return 0;
}
};
template<typename Ret,typename Arg1,typename Arg2,typename Cla>
class memberfunbinder<Ret(Cla::*)(Arg1,Arg2)>
{
public:
typedef Cla CLASS_TYPE;
static int lua_cfunction(lua_State *L)
{
objUserData<Cla> *obj = objUserData<Cla>::checkobjuserdata(L,1);
Cla *cla = obj->ptr;
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,cla,arg1,arg2,Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(Cla::*__func)(Arg1,Arg2);
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Int2Type<false>)
{
push_obj<Result>(L,(cla->*_GETFUNC)(arg1,arg2));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Int2Type<true>)
{
(cla->*_GETFUNC)(arg1,arg2);
return 0;
}
};
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Cla>
class memberfunbinder<Ret(Cla::*)(Arg1,Arg2,Arg3)>
{
public:
typedef Cla CLASS_TYPE;
static int lua_cfunction(lua_State *L)
{
objUserData<Cla> *obj = objUserData<Cla>::checkobjuserdata(L,1);
Cla *cla = obj->ptr;
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,cla,arg1,arg2,arg3,Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(Cla::*__func)(Arg1,Arg2,Arg3);
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Arg3 arg3,Int2Type<false>)
{
push_obj<Result>(L,(cla->*_GETFUNC)(arg1,arg2,arg3));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Arg3 arg3,Int2Type<true>)
{
(cla->*_GETFUNC)(arg1,arg2,arg3);
return 0;
}
};
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Cla>
class memberfunbinder<Ret(Cla::*)(Arg1,Arg2,Arg3,Arg4)>
{
public:
static int lua_cfunction(lua_State *L)
{
objUserData<Cla> *obj = objUserData<Cla>::checkobjuserdata(L,1);
Cla *cla = obj->ptr;
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,cla,arg1,arg2,arg3,arg4,Int2Type<isVoid<Ret>::is_Void>());
}
private:
typedef Ret(Cla::*__func)(Arg1,Arg2,Arg3,Arg4);
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Int2Type<false>)
{
push_obj<Result>(L,(cla->*_GETFUNC)(arg1,arg2,arg3,arg4));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Int2Type<true>)
{
(cla->*_GETFUNC)(arg1,arg2,arg3,arg4);
return 0;
}
};
template<typename Parent,typename T>
void DefParent(Int2Type<false>)
{
std::map<std::string,memberfield<Parent> > &parent_fields = luaClassWrapper<Parent>::GetAllFields();
std::map<std::string,memberfield<T> > &filds = luaClassWrapper<T>::GetAllFields();
typename std::map<std::string,memberfield<Parent> >::iterator it = parent_fields.begin();
typename std::map<std::string,memberfield<Parent> >::iterator end = parent_fields.end();
for( ; it != end; ++it)
filds.insert(std::make_pair(it->first,it->second));
}
template<typename Parent,typename T>
void DefParent(Int2Type<true>){}
template<typename T>
class class_def
{
private:
class construct_function0
{
public:
static void lua_cfunction(lua_State *L)
{
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->construct_by_lua = true;
obj->ptr = new T;
ptrToUserData[(void*)obj->ptr] = obj;
userdataSet.insert((void*)obj);
set_userdata(L,(void*)obj,-1);
return;
}
};
template<typename Arg1>
class construct_function1
{
public:
static void 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);
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->construct_by_lua = true;
obj->ptr = new T(arg1);
ptrToUserData[(void*)obj->ptr] = obj;
userdataSet.insert((void*)obj);
set_userdata(L,(void*)obj,-1);
}
};
template<typename Arg1,typename Arg2>
class construct_function2
{
public:
static void 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);
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2);
ptrToUserData[(void*)obj->ptr] = obj;
userdataSet.insert((void*)obj);
set_userdata(L,(void*)obj,-1);
}
};
template<typename Arg1,typename Arg2,typename Arg3>
class construct_function3
{
public:
static void 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);
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
typename GetReplaceType<Arg3>::type tmp_arg3 = popvalue<typename GetReplaceType<Arg3>::type>(L);
Arg3 arg3 = GetRawValue<typename GetReplaceType<Arg3>::type>(tmp_arg3);
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2,arg3);
ptrToUserData[(void*)obj->ptr] = obj;
userdataSet.insert((void*)obj);
set_userdata(L,(void*)obj,-1);
}
};
template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
class construct_function4
{
public:
static void 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);
typename GetReplaceType<Arg2>::type tmp_arg2 = popvalue<typename GetReplaceType<Arg2>::type>(L);
Arg2 arg2 = GetRawValue<typename GetReplaceType<Arg2>::type>(tmp_arg2);
typename GetReplaceType<Arg3>::type tmp_arg3 = popvalue<typename GetReplaceType<Arg3>::type>(L);
Arg3 arg3 = GetRawValue<typename GetReplaceType<Arg3>::type>(tmp_arg3);
typename GetReplaceType<Arg4>::type tmp_arg4 = popvalue<typename GetReplaceType<Arg4>::type>(L);
Arg4 arg4 = GetRawValue<typename GetReplaceType<Arg4>::type>(tmp_arg4);
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2,arg3,arg4);
ptrToUserData[(void*)obj->ptr] = obj;
userdataSet.insert((void*)obj);
set_userdata(L,(void*)obj,-1);
}
};
public:
class_def(lua_State *L):L(L){}
template<typename property_type>
class_def<T>& property(const char *name,property_type (T::*property))
{
memberfield<T> mf;
mf.tt = MEMBER_FIELD;
mf.getter = GetProperty<T,property_type>;
mf.property = (void*(T::*))property;
mf.setter = SetProperty<T,property_type>;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
//class member method
template<typename FUNTOR>
class_def<T>& method(const char *name,FUNTOR _func)
{
memberfield<T> mf;
mf.tt = MEMBER_FUNCTION;
mf.mfunction = (void(T::*)())_func;
mf.mlua_func = memberfunbinder<FUNTOR>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
//class static method
template<typename Ret>
class_def<T>& method(const char *name,Ret(*_func)())
{
memberfield<T> mf;
mf.tt = STATIC_FUNCTION;
mf.sfunction = (void (*)())_func;
mf.slua_func = funbinder<Ret(*)()>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename Ret,typename Arg1>
class_def<T>& method(const char *name,Ret(*_func)(Arg1))
{
memberfield<T> mf;
mf.tt = STATIC_FUNCTION;
mf.sfunction = (void (*)())_func;
mf.slua_func = funbinder<Ret(*)(Arg1)>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename Ret,typename Arg1,typename Arg2>
class_def<T>& method(const char *name,Ret(*_func)(Arg1,Arg2))
{
memberfield<T> mf;
mf.tt = STATIC_FUNCTION;
mf.sfunction = (void (*)())_func;
mf.slua_func = funbinder<Ret(*)(Arg1,Arg2)>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3>
class_def<T>& method(const char *name,Ret(*_func)(Arg1,Arg2,Arg3))
{
memberfield<T> mf;
mf.tt = STATIC_FUNCTION;
mf.sfunction = (void (*)())_func;
mf.slua_func = funbinder<Ret(*)(Arg1,Arg2,Arg3)>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
class_def<T>& method(const char *name,Ret(*_func)(Arg1,Arg2,Arg3,Arg4))
{
memberfield<T> mf;
mf.tt = STATIC_FUNCTION;
mf.sfunction = (void (*)())_func;
mf.slua_func = funbinder<Ret(*)(Arg1,Arg2,Arg3,Arg4)>::lua_cfunction;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename ARG1>
class_def<T>& constructor()
{
_constructor<ARG1>(Int2Type<isVoid<ARG1>::is_Void == 1>());
return *this;
}
template<typename ARG1,typename ARG2>
class_def<T>& constructor()
{
memberfield<T> mf;
mf.tt = CONSTRUCTOR;
mf.constructor = construct_function2<ARG1,ARG2>::lua_cfunction;
_constructor(2,mf);
return *this;
}
template<typename ARG1,typename ARG2,typename ARG3>
class_def<T>& constructor()
{
memberfield<T> mf;
mf.tt = CONSTRUCTOR;
mf.constructor = construct_function3<ARG1,ARG2,ARG3>::lua_cfunction;
_constructor(3,mf);
return *this;
}
template<typename ARG1,typename ARG2,typename ARG3,typename ARG4>
class_def<T>& constructor()
{
memberfield<T> mf;
mf.tt = CONSTRUCTOR;
mf.constructor = construct_function4<ARG1,ARG2,ARG3,ARG4>::lua_cfunction;
_constructor(4,mf);
return *this;
}
private:
template<typename ARG1>
void _constructor(Int2Type<true>)
{
memberfield<T> mf;
mf.tt = CONSTRUCTOR;
mf.constructor = construct_function0::lua_cfunction;
_constructor(0,mf);
}
template<typename ARG1>
void _constructor(Int2Type<false>)
{
memberfield<T> mf;
mf.tt = CONSTRUCTOR;
mf.constructor = construct_function1<ARG1>::lua_cfunction;
_constructor(1,mf);
}
void _constructor(int arg_size,memberfield<T> &mf)
{
if(1 == luaClassWrapper<T>::InsertConstructors(arg_size,mf))
{
lua_pushcfunction(L,objUserData<T>::Construct);
lua_setglobal(L,luaRegisterClass<T>::GetClassName());
}
}
lua_State *L;
};
template<typename T,typename Parent1 = void,typename Parent2 = void,typename Parent3 = void,typename Parent4 = void>
struct reg_cclass
{
static class_def<T> _reg(lua_State *L,const char *name)
{
luaRegisterClass<T>::SetClassName(name);
luaClassWrapper<T>::luaopen_objlib(L);
luaRegisterClass<T>::SetRegister();
DefParent<Parent1,T>(Int2Type<isVoid<Parent1>::is_Void>());
DefParent<Parent2,T>(Int2Type<isVoid<Parent2>::is_Void>());
DefParent<Parent3,T>(Int2Type<isVoid<Parent3>::is_Void>());
DefParent<Parent4,T>(Int2Type<isVoid<Parent4>::is_Void>());
return class_def<T>(L);
}
};
}
#endif