3 Star 0 Fork 1

Gitee 极速下载/luawrapper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/sniperHW/luawrapper
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
luaObject.h 4.39 KB
一键复制 编辑 原始数据 按行查看 历史
sniperHW 提交于 2018-03-07 12:39 . none
/*
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/>.
*/
#ifndef _LUAOBJECT_H
#define _LUAOBJECT_H
#include "luacommon.h"
#include "luaFunction.h"
#define PREPARE(L,index){\
lua_rawgeti(L,LUA_REGISTRYINDEX,index);\
lua_pushstring(L,name);\
lua_gettable(L,-2);\
lua_rawgeti(L,LUA_REGISTRYINDEX,index);\
}
namespace luacpp{
template<typename T>
T popvalue(lua_State *L);
template<typename T>
void push_obj(lua_State *L,const T &obj);
//代表lua中的一个对象(其实就是一个表)
class luaObject
{
public:
luaObject(lua_State *lState,int index)
:m_iKeyIndex(index),m_plState(lState){
counter = new int(1);
}
luaObject()
:m_iKeyIndex(LUA_NOREF),m_plState(NULL),counter(NULL){}
luaObject &operator = (const luaObject & other)
{
if(this != &other)
{
if(!counter)
{
counter = other.counter;
++(*counter);
this->m_iKeyIndex = other.m_iKeyIndex;
this->m_plState = other.m_plState;
}
else
{
if(--(*counter) <= 0)
{
//原来指向的对象现在已经没有其它对象在引用了,可以从lua中释放掉了
if(!this->isNULL())
{
luaL_unref(this->m_plState,LUA_REGISTRYINDEX,this->m_iKeyIndex);
}
delete counter;
}
counter = other.counter;
++(*counter);
this->m_iKeyIndex = other.m_iKeyIndex;
this->m_plState = other.m_plState;
}
}
return *this;
}
luaObject(const luaObject &other)
:m_iKeyIndex(other.m_iKeyIndex),m_plState(other.m_plState),counter(other.counter)
{
++(*counter);
}
~luaObject()
{
if(counter)
{
if(!(--(*counter)))
{
if(!this->isNULL())
{
luaL_unref(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
}
delete counter;
}
}
}
template<typename RET,typename KEY>
RET Get(KEY key)
{
lua_rawgeti(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
push_obj<KEY>(m_plState,key);
lua_gettable(m_plState,-2);
RET ret = popvalue<RET>(m_plState);
lua_pop(m_plState,1);//pop the table
return ret;
}
template<typename T,typename KEY>
void Set(KEY key,T value)
{
lua_rawgeti(m_plState,LUA_REGISTRYINDEX,m_iKeyIndex);
push_obj<KEY>(m_plState,key);
push_obj<T>(m_plState,value);
lua_settable(m_plState,-3);
lua_pop(m_plState,1);
}
template<typename Ret>
Ret call(const char *name)
{
PREPARE(m_plState,m_iKeyIndex);
return doLuaCall<Ret>::doCall(m_plState,1,0,true);
}
template<typename Ret,typename Arg1>
Ret call(const char *name,const Arg1 &arg1)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
return doLuaCall<Ret>::doCall(m_plState,2,0,true);
}
template<typename Ret,typename Arg1,typename Arg2>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
return doLuaCall<Ret>::doCall(m_plState,3,0,true);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
push_obj<Arg3>(m_plState,arg3);
return doLuaCall<Ret>::doCall(m_plState,4,0,true);
}
template<typename Ret,typename Arg1,typename Arg2,typename Arg3,typename Arg4>
Ret call(const char *name,const Arg1 &arg1,const Arg2 &arg2,const Arg3 &arg3,const Arg4 &arg4)
{
PREPARE(m_plState,m_iKeyIndex);
push_obj<Arg1>(m_plState,arg1);
push_obj<Arg2>(m_plState,arg2);
push_obj<Arg3>(m_plState,arg3);
push_obj<Arg4>(m_plState,arg4);
return doLuaCall<Ret>::doCall(m_plState,5,0,true);
}
lua_State *getState() const {
return m_plState;
}
int getIndex() const {
return m_iKeyIndex;
}
bool isNULL() const{
return (m_iKeyIndex == LUA_REFNIL) || (m_iKeyIndex == LUA_NOREF);
}
private:
lua_State *m_plState;
int m_iKeyIndex;
int *counter;
};
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/mirrors/luawrapper.git
git@gitee.com:mirrors/luawrapper.git
mirrors
luawrapper
luawrapper
master

搜索帮助