lua笔记 --- 一个C++调用lua函数的类的实现
2013-05-12 15:45
731 查看
这个类基本上是从LuaPlus那里弄来的,为什么不支持用LuaPlus,因为那东西我实在不知道怎么编译,能编译通过的版本的旧版本的了,而且之前的版本有BUG的存在,使用起来有阴影,不想用了,还是自己写个比较靠谱。
因为感觉LuaPlus那个LuaFunction有点不好用,所以进行了一些改造。自己认为目前自己封装的这个还是比较好用的。
使用方法:
看起来就像是个函数,使用起来也方便
因为感觉LuaPlus那个LuaFunction有点不好用,所以进行了一些改造。自己认为目前自己封装的这个还是比较好用的。
namespace Lua_Wrapper{ struct LuaNil{}; inline void Push(lua_State* L, bool value) { lua_pushboolean(L, value); } inline void Push(lua_State* L, char value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, unsigned char value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, short value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, unsigned short value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, int value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, unsigned int value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, long value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, unsigned long value) { lua_pushnumber(L, value); } inline void Push(lua_State* L, double value) { lua_pushnumber(L, (lua_Number)value); } inline void Push(lua_State* L, float value) { lua_pushnumber(L, (lua_Number)value); } inline void Push(lua_State* L, const char* value) { lua_pushstring(L, value); } inline void Push(lua_State* L, const LuaNil&) { lua_pushnil(L); } inline void Push(lua_State* L, lua_CFunction value) { lua_pushcclosure(L, value, 0); } inline void Push(lua_State* L, const void* value) { lua_pushlightuserdata(L, (void*)value); } struct Get { lua_State* L; int idx; Get(lua_State* L,int index=-1):L(L),idx(index){} operator bool(){assert(lua_isboolean(L,idx));return lua_toboolean(L, idx) != 0;} operator char(){assert(lua_isnumber(L,idx));return static_cast<char>(lua_tonumber(L, idx));} operator unsigned char(){assert(lua_isnumber(L,idx));return static_cast<unsigned char>(lua_tonumber(L, idx));} operator short(){assert(lua_isnumber(L,idx));return static_cast<short>(lua_tonumber(L, idx));} operator unsigned short(){assert(lua_isnumber(L,idx));return static_cast<unsigned short>(lua_tonumber(L, idx));} operator int(){assert(lua_isnumber(L,idx));return static_cast<int>(lua_tonumber(L, idx));} operator unsigned int(){assert(lua_isnumber(L,idx));return static_cast<unsigned int>(lua_tonumber(L, idx));} operator long(){assert(lua_isnumber(L,idx));return static_cast<long>(lua_tonumber(L, idx));} operator unsigned long(){assert(lua_isnumber(L,idx));return static_cast<unsigned long>(lua_tonumber(L, idx));} operator double(){assert(lua_isnumber(L,idx));return static_cast<double>(lua_tonumber(L, idx));} operator float(){assert(lua_isnumber(L,idx));return static_cast<float>(lua_tonumber(L, idx));} operator const char*(){assert(lua_isstring(L,idx));return static_cast<const char*>(lua_tostring(L, idx));} operator lua_CFunction(){assert(lua_iscfunction(L,idx));return static_cast<lua_CFunction>(lua_tocfunction(L, idx));} operator void*(){assert(lua_islightuserdata(L,idx));return static_cast<void*>(lua_touserdata(L, idx));} }; } class luaautoblock { public: luaautoblock(lua_State* L) : L(L), m_stackTop(lua_gettop(L)){} ~luaautoblock(){lua_settop(L, m_stackTop);} private: luaautoblock(const luaautoblock& src); // Not implemented const luaautoblock& operator=(const luaautoblock& src); // Not implemented lua_State* L; int m_stackTop; }; struct CallLuaFunction { lua_State* L; luaautoblock autoblock; CallLuaFunction(lua_State* L, const char* fnName):L(L),autoblock(L){ lua_getglobal(L,fnName); if(lua_pcall(L,0,1,0)){logluaerror(L);} } template<class T1> CallLuaFunction(lua_State* L, const char* fnName, T1 t1):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); if(lua_pcall(L,1,1,0)){logluaerror(L);} } template<class T1,class T2> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); if(lua_pcall(L,2,1,0)){logluaerror(L);} } template<class T1,class T2,class T3> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); if(lua_pcall(L,3,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); if(lua_pcall(L,4,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4,class T5> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); Lua_Wrapper::Push(L,t5); if(lua_pcall(L,5,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4,class T5,class T6> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5,T6 t6):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); Lua_Wrapper::Push(L,t5); Lua_Wrapper::Push(L,t6); if(lua_pcall(L,6,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); Lua_Wrapper::Push(L,t5); Lua_Wrapper::Push(L,t6); Lua_Wrapper::Push(L,t7); if(lua_pcall(L,7,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4,class T5,class T6,class T7,class T8> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); Lua_Wrapper::Push(L,t5); Lua_Wrapper::Push(L,t6); Lua_Wrapper::Push(L,t7); Lua_Wrapper::Push(L,t8); if(lua_pcall(L,8,1,0)){logluaerror(L);} } template<class T1,class T2,class T3,class T4,class T5,class T6,class T7,class T8,class T9> CallLuaFunction(lua_State* L, const char* fnName, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9):L(L),autoblock(L){ lua_getglobal(L,fnName); Lua_Wrapper::Push(L,t1); Lua_Wrapper::Push(L,t2); Lua_Wrapper::Push(L,t3); Lua_Wrapper::Push(L,t4); Lua_Wrapper::Push(L,t5); Lua_Wrapper::Push(L,t6); Lua_Wrapper::Push(L,t7); Lua_Wrapper::Push(L,t8); Lua_Wrapper::Push(L,t9); if(lua_pcall(L,9,1,0)){logluaerror(L);} } template<class RT> operator RT(){return Lua_Wrapper::Get(L);} };
使用方法:
luaL_dostring(L,"function test(...) print(...) return 'ok' end"); const char* ret = CallLuaFunction(L,"test","a","b",3,&a,5,"666",7,"888",998);//调用lua函数 printf("ret:%s\n",ret);
看起来就像是个函数,使用起来也方便
相关文章推荐
- 函数编译lua笔记 --- 一个C++调用lua函数的类的实现
- lua笔记 --- 一个C++调用lua函数的类的实现
- Lua学习笔记(6) : 在Lua中调用C++之函数
- LUA调用C++函数的一个简单Demo
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(3) 最大子序列和问题
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- 069 - 问,lua程序设计(第四部分)笔记三,c++调用lua函数 - everettjf - C++博客
- 【Programming In Lua (2E) 笔记】4:用lua扩展C++——C++调用lua函数
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(4)二分搜索算法
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(6)高效率的幂运算
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(2) IntCell类
- lua脚本语言的学习-----------------如何实现c++无参数的函数在lua中调用
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(5)欧几里得算法欧几里得算法求最大公约数
- C++ 17转发一个函数调用的完美实现
- C++调用LUA函数,可变参模板实现
- [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(1) f(x) = 2f(x-1) + x^2
- c++ 构造函数中调用虚函数的实现方法
- 使用tolua++实现C++与LUA相互调用
- lua调用C++函数崩溃时,查看lua的调用栈信息 (特别适用于tolua++)