1 Star 0 Fork 1

sniperHW/luawrapper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
luaClass.h 23.09 KB
一键复制 编辑 原始数据 按行查看 历史
sniperHW 提交于 2014-08-09 23:49 . none
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
#ifndef _LUACLASS_H
#define _LUACLASS_H
#include "luacommon.h"
#include "luaObject.h"
#include "any.h"
#include <string.h>
namespace luacpp{
template<typename T>
void push_obj(lua_State *L,const T &obj);
//ȡ��ջ����ֵ��֪ͨ�����ջ
template<typename T>
T popvalue(lua_State *L);
class Integer64
{
public:
Integer64(int64_t val):m_val(val)
{
m_flag = 0XFEDC1234;
}
int64_t GetValue() const
{
return m_val;
}
int GetFlag() const{return m_flag;}
static void Register2Lua(lua_State *L);
static void setmetatable(lua_State *L)
{
luaL_getmetatable(L, "kenny.lualib");
lua_pushstring(L,"int64");
lua_gettable(L,-2);
lua_setmetatable(L, -3);
lua_pop(L,1);
}
#ifndef I64_RELA
#define I64_RELA(OP)\
Integer64 *i64self = (Integer64 *)lua_touserdata(L,1);\
Integer64 *i64other = (Integer64 *)lua_touserdata(L,2);\
lua_pushboolean(L,i64self->m_val OP i64other->m_val);\
return 1;
#endif
static int i64Le(lua_State *L)
{
I64_RELA(<=);
}
static int i64Lt(lua_State *L)
{
I64_RELA(<);
}
static int i64Eq(lua_State *L)
{
I64_RELA(==);
}
#ifndef I64_MATH
#define I64_MATH(OP)\
Integer64 *i64self = (Integer64 *)lua_touserdata(L,1);\
Integer64 *i64other = (Integer64 *)lua_touserdata(L,2);\
Integer64 tmp(0);\
if(!i64other)\
{\
tmp.m_val = (int64_t)lua_tonumber(L,2);\
i64other = &tmp;\
}\
if(!i64self)\
{\
long num = (long)lua_tonumber(L,1);\
size_t nbytes = sizeof(Integer64);\
i64self = (Integer64*)lua_newuserdata(L, nbytes);\
new(i64self)Integer64(num);\
i64self->m_val = i64self->m_val OP i64other->m_val;\
}else\
{\
i64self->m_val = i64self->m_val OP i64other->m_val;\
lua_pushlightuserdata(L,i64self);\
}\
setmetatable(L);\
return 1;
#endif
static int i64Mod(lua_State *L)
{
I64_MATH(%);
}
static int i64Div(lua_State *L)
{
I64_MATH(/);
}
static int i64Mul(lua_State *L)
{
I64_MATH(*);
}
static int i64Add(lua_State *L)
{
I64_MATH(+);
}
static int i64Sub(lua_State *L)
{
I64_MATH(-);
}
static int i64toString(lua_State *L)
{
Integer64 *i64self = (Integer64 *)lua_touserdata(L,1);
luaL_argcheck(L, i64self != NULL, 1, "userdata expected");
char temp[64];
sprintf(temp, "%ld", i64self->m_val);
lua_pushstring(L, temp);
return 1;
}
static int i64Destroy(lua_State *L)
{
Integer64 *i64self = (Integer64 *)lua_touserdata(L,1);
luaL_argcheck(L, i64self != NULL, 1, "userdata expected");
printf("i64Destroy\n");
return 0;
}
private:
int64_t m_val;
int m_flag;
};
static void pushI64(lua_State *L,int64_t value)
{
size_t nbytes = sizeof(Integer64);
void *buf = lua_newuserdata(L, nbytes);
new(buf)Integer64(value);
Integer64::setmetatable(L);
}
template <typename T>
struct memberfield
{
memberfield():gmv(NULL),smv(NULL),mc(NULL),function(NULL){}
template<typename PARENT>
memberfield(const memberfield<PARENT> &p):gmv((GMV)p.gmv),smv((SMV)p.smv),mc((MC)p.mc),
function(p.function),property(p.property) {}
typedef void (*GMV)(T *,lua_State*,void *(T::*));
typedef void (*SMV)(T *,lua_State*,void *(T::*));
typedef int (*MC)(lua_State*);
GMV gmv;
SMV smv;
MC mc;
void (T::*function)(void);
void *(T::*property);
};
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 int luaopen_objlib(lua_State *L) {
luaL_getmetatable(L, "kenny.lualib");
lua_pushstring(L,luaRegisterClass<T>::GetClassName());
luaL_newmetatable(L, luaRegisterClass<T>::GetClassName());
lua_pushstring(L, "__index");
lua_pushcfunction(L, &objUserData<T>::Index);
lua_settable(L, -3);
lua_pushstring(L,"__gc");
lua_pushcfunction(L, &objUserData<T>::on_gc);
lua_rawset(L, -3);
lua_pushstring(L, "__newindex");
lua_pushcfunction(L, &objUserData<T>::NewIndex);
lua_rawset(L, -3);
lua_rawset(L,1);
lua_pop(L,1);
return 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].mc == 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 NewObj(lua_State *L,const void *ptr,const char *classname);
template<typename T>
class objUserData
{
public:
static objUserData<T> *checkobjuserdata (lua_State *L,int index) {
if(!luaRegisterClass<T>::isRegister())
return NULL;
void *ud = lua_touserdata(L,index);
luaL_argcheck(L, ud != NULL, 1, "userdata expected");
return (objUserData<T> *)ud;
}
static int NewObj(lua_State *L,const T *ptr)
{
return luacpp::NewObj(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())
it->second.smv(self->ptr,L,it->second.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())
{
if(it->second.function)
{
lua_pushlightuserdata(L,&it->second.function);
lua_pushcclosure(L,it->second.mc,1);
}
else
it->second.gmv(obj->ptr,L,it->second.property);
}
else
lua_pushnil(L);
return 1;
}
static int Construct(lua_State *L)
{
int arg_size = lua_gettop(L);
if(arg_size < 16 && luaClassWrapper<T>::constructors[arg_size].mc)
{
luaClassWrapper<T>::constructors[arg_size].mc(L);
luaL_getmetatable(L, "kenny.lualib");
lua_pushstring(L,luaRegisterClass<T>::GetClassName());
lua_gettable(L,-2);
lua_setmetatable(L, -3);
lua_pop(L,1);//pop mt kenny.lualib
}
else
{
char str[512];
#ifdef _VC
_snprintf(str,512,"%s: unsupport %d arguments constructor\n",luaRegisterClass<T>::GetClassName(),arg_size);
#else
snprintf(str,512,"%s: unsupport %d arguments constructor\n",luaRegisterClass<T>::GetClassName(),arg_size);
#endif
luaL_error(L,str);
}
return 1;
}
static int on_gc(lua_State *L)
{
printf("on_gc\n");
objUserData<T> *self = (objUserData<T> *)lua_touserdata(L,1);
if(self->construct_by_lua)
{
printf("create_by_lua delete\n");
delete self->ptr;
}
return 0;
}
public:
T *ptr;
int m_flag;
bool construct_by_lua;
};
typedef int (*lua_fun)(lua_State*);
#ifndef _GETFUNC
#define _GETFUNC\
__func func;\
void *t = lua_touserdata(L,lua_upvalueindex(1));\
memcpy(&func,t,sizeof(func));
#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>)
{
_GETFUNC;
push_obj<Result>(L,(cla->*func)());
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Int2Type<true>)
{
_GETFUNC;
(cla->*func)();
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>)
{
_GETFUNC;
push_obj<Result>(L,(cla->*func)(arg1));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Int2Type<true>)
{
_GETFUNC;
(cla->*func)(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>)
{
_GETFUNC;
push_obj<Result>(L,(cla->*func)(arg1,arg2));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Int2Type<true>)
{
_GETFUNC;
(cla->*func)(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);
Arg2 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>)
{
_GETFUNC;
push_obj<Result>(L,(cla->*func)(arg1,arg2,arg3));
return 1;
}
template<typename Result>
static int doCall(lua_State *L,Cla *cla,Arg1 arg1,Arg2 arg2,Arg3 arg3,Int2Type<true>)
{
_GETFUNC;
(cla->*func)(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);
Arg2 arg3 = GetRawValue<typename GetReplaceType<Arg3>::type>(tmp_arg3);
Arg2 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>)
{
_GETFUNC;
push_obj<Result>(L,(cla->*func)(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>)
{
_GETFUNC;
(cla->*func)(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 int lua_cfunction(lua_State *L)
{
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->m_flag = 0x1234AFEC;
obj->construct_by_lua = true;
obj->ptr = new T;
return 1;
}
};
template<typename Arg1>
class construct_function1
{
public:
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);
objUserData<T> *obj = (objUserData<T> *)lua_newuserdata(L, sizeof(objUserData<T>));
obj->m_flag = 0x1234AFEC;
obj->construct_by_lua = true;
obj->ptr = new T(arg1);
return 1;
}
};
template<typename Arg1,typename Arg2>
class construct_function2
{
public:
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);
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->m_flag = 0x1234AFEC;
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2);
return 1;
}
};
template<typename Arg1,typename Arg2,typename Arg3>
class construct_function3
{
public:
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);
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->m_flag = 0x1234AFEC;
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2,arg3);
return 1;
}
};
template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
class construct_function4
{
public:
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);
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->m_flag = 0x1234AFEC;
obj->construct_by_lua = true;
obj->ptr = new T(arg1,arg2,arg3,arg4);
return 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.gmv = GetProperty<T,property_type>;
mf.property = (void*(T::*))property;
mf.smv = SetProperty<T,property_type>;
luaClassWrapper<T>::InsertFields(name,mf);
return *this;
}
template<typename FUNTOR>
class_def<T> function(const char *fun_name,FUNTOR _func)
{
memberfield<T> mf;
mf.function = (void(T::*)())_func;
lua_fun fun = memberfunbinder<FUNTOR>::lua_cfunction;
mf.mc = fun;
luaClassWrapper<T>::InsertFields(fun_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;
lua_fun fun = construct_function2<ARG1,ARG2>::lua_cfunction;
mf.mc = fun;
_constructor(2,mf);
return *this;
}
template<typename ARG1,typename ARG2,typename ARG3>
class_def<T> constructor()
{
memberfield<T> mf;
lua_fun fun = construct_function3<ARG1,ARG2,ARG3>::lua_cfunction;
mf.mc = fun;
_constructor(3,mf);
return *this;
}
template<typename ARG1,typename ARG2,typename ARG3,typename ARG4>
class_def<T> constructor()
{
memberfield<T> mf;
lua_fun fun = construct_function4<ARG1,ARG2,ARG3,ARG4>::lua_cfunction;
mf.mc = fun;
_constructor(4,mf);
return *this;
}
private:
template<typename ARG1>
void _constructor(Int2Type<true>)
{
memberfield<T> mf;
lua_fun fun = construct_function0::lua_cfunction;
mf.mc = fun;
_constructor(0,mf);
}
template<typename ARG1>
void _constructor(Int2Type<false>)
{
memberfield<T> mf;
lua_fun fun = construct_function1<ARG1>::lua_cfunction;
mf.mc = fun;
_constructor(1,mf);
}
void _constructor(int arg_size,memberfield<T> &mf)
{
if(1 == luaClassWrapper<T>::InsertConstructors(arg_size,mf))
{
lua_getglobal(L,"_G");
if(!lua_istable(L, -1))
{
lua_pop(L,1);
lua_newtable(L);
lua_pushvalue(L,-1);
lua_setglobal(L,"_G");
}
lua_pushstring(L, luaRegisterClass<T>::GetClassName());
lua_pushcfunction(L,objUserData<T>::Construct);
lua_settable(L, -3);
lua_pop(L,1);
}
}
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sniperHW/luawrapper.git
git@gitee.com:sniperHW/luawrapper.git
sniperHW
luawrapper
luawrapper
master

搜索帮助