闲来无聊,把luatinker封装了一层,分享之。。。
// lua_tinker_test.cpp : 定义控制台应用程序的入口点。 // /************************************************************************/ /* *author:郑金玮 *time:2014/07/21 *desc:impl luatinker package */ /************************************************************************/ #include "stdafx.h" #include <stdlib.h> extern "C" { #include "..\\lua_tinker\inc\lua.h" #include "..\\lua_tinker\inc\lualib.h" #include "..\\lua_tinker\inc\lauxlib.h" }; #include "..\\lua_tinker\inc\lua_tinker.h" #include <iostream> using namespace std; #pragma comment(lib,"luatinker.lib") ////////////////////全局函数////////////////////////////////////////////////////// int normal_func_add(int arg1, int arg2) { return arg1 + arg2; } int normal_func_sub(int arg1, int arg2) { return arg1 - arg2; } int normal_func_mul(int arg1, int arg2) { return arg1 * arg2; } ////////////////////类////////////////////////////////////////////////////// class CMathLib { public: CMathLib(){} ~CMathLib(){} public: int add(int arg1,int arg2){ return arg1+arg2 ;} int sub(int arg1,int arg2){ return arg1 - arg2 ;} int mul(int arg1,int arg2){ return arg1*arg2 ;} }; struct A { A(int v) : value(v) {} int value; }; struct base { base() {} const char* is_base(){ return "this is base"; } }; class test : public base { public: test(int val) : _test(val) {} ~test() {} const char* is_test(){ return "this is test"; } void ret_void() {} int ret_int() { return _test; } int ret_mul(int m) const { return _test * m; } A get() { return A(_test); } void set(A a) { _test = a.value; } int _test; }; //全局变量 test g_test(11); static int g_count=1; //////////////////////////封装luatinker//////////////////////////////////////////////// class CLua { CLua(); ~CLua(); public: static CLua* instance(); public: int initLua(); int openLuaLib(); int closeLua(); public: void add(); void runLuaFile(); template<typename T> T call(const char* luaFuncName,T arg); template<typename T> T call(const char* luaFuncName,T arg1,T arg2); template<typename T> T call(const char* luaFuncName,T arg1,T arg2,T arg3); private: lua_State* m_pState; }; CLua::CLua() { initLua(); openLuaLib(); add(); } CLua::~CLua() { closeLua(); } CLua* CLua::instance() { static CLua __instance; return &__instance; } int CLua::initLua() { m_pState= lua_open(); lua_tinker::init(m_pState); return 0; } int CLua::openLuaLib() { luaopen_base(m_pState); //luaopen_io(m_pState); luaopen_math(m_pState); luaopen_debug(m_pState); luaopen_string(m_pState); return 0; } int CLua::closeLua() { lua_close(m_pState); return 0; } void CLua::add() { lua_tinker::def(m_pState, "normal_add", normal_func_add); lua_tinker::def(m_pState, "normal_sub", normal_func_sub); lua_tinker::def(m_pState, "normal_mul", normal_func_mul); ////////////////////////////////////////////////////////////////////////// lua_tinker::class_add<CMathLib>(m_pState,"CMathLib"); lua_tinker::class_con<CMathLib>(m_pState, lua_tinker::constructor<CMathLib>); lua_tinker::class_def<CMathLib>(m_pState, "class_add", &CMathLib::add); lua_tinker::class_def<CMathLib>(m_pState, "class_sub", &CMathLib::sub); lua_tinker::class_def<CMathLib>(m_pState, "class_mul", &CMathLib::mul); ////////////////////////////////////////////////////////////////////////// lua_tinker::class_add<base>(m_pState, "base"); lua_tinker::class_def<base>(m_pState, "is_base", &base::is_base); lua_tinker::class_add<test>(m_pState, "test"); lua_tinker::class_inh<test, base>(m_pState); lua_tinker::class_con<test>(m_pState, lua_tinker::constructor<test,int>); lua_tinker::class_def<test>(m_pState, "is_test", &test::is_test); lua_tinker::class_def<test>(m_pState, "ret_void", &test::ret_void); lua_tinker::class_def<test>(m_pState, "ret_int", &test::ret_int); lua_tinker::class_def<test>(m_pState, "ret_mul", &test::ret_mul); lua_tinker::class_def<test>(m_pState, "get", &test::get); lua_tinker::class_def<test>(m_pState, "set", &test::set); lua_tinker::class_mem<test>(m_pState, "_test", &test::_test); lua_tinker::set(m_pState, "g_test", &g_test); ////////////////////////////////////////////////////////////////////////// lua_tinker::set(m_pState, "g_count", g_count); } void CLua::runLuaFile() { lua_tinker::dofile(m_pState, "..\\lua_file\\normal.lua"); lua_tinker::dofile(m_pState, "..\\lua_file\\mathlib.lua"); lua_tinker::dofile(m_pState, "..\\lua_file\\base.lua"); } template<typename T> T CLua::call(const char* luaFuncName,T arg) { T result = lua_tinker::call<T>(m_pState, luaFuncName, arg); return result; } template<typename T> T CLua::call(const char* luaFuncName,T arg1,T arg2) { T result = lua_tinker::call<T>(m_pState, luaFuncName, arg1,arg2); return result; } template<typename T> T CLua::call(const char* luaFuncName,T arg1,T arg2,T arg3) { T result = lua_tinker::call<T>(m_pState, luaFuncName, arg1,arg2,arg3); return result; } ////////////////////////////////////////////////////////////////////////// #define LUAINSTANCE() CLua::instance() int _tmain(int argc, _TCHAR* argv[]) { LUAINSTANCE()->runLuaFile(); cout<<"================="<<endl<<endl; int ret1 = LUAINSTANCE()->call("lua_add1",100); double ret2 = LUAINSTANCE()->call("lua_add2",1.2,2.5); double ret3 = LUAINSTANCE()->call("lua_add3",1.5,2.9,3.9); cout<<"ret1= "<<ret1<<endl; cout<<"ret2= "<<ret2<<endl; cout<<"ret3= "<<ret3<<endl; system("pause"); return 0; }
lua文件:
base.lua
print(g_test._test) print(g_test:is_test()) print(g_test:ret_int()) temp = test(4) print(temp._test) a = g_test:get() temp:set(a) print(temp._test) print(temp:is_base()) print(temp:is_test())
mathlib.lua
print("--------------------------") print("g_count " .. g_count) print("-----------------------") temp = CMathLib() print("math call add " .. tostring(temp:class_add(1,2))) print("math call sub " .. tostring(temp:class_sub(1,2))) print("math call mul " .. tostring(temp:class_mul(1,2))) print("--------------------------") function lua_add2(a,b) return a+b; end function lua_add3(a,b,c) return a+b+c; end function lua_add1(a) return a; end
print("normal call add " .. tostring(normal_add(1,2))) print("normal call sub " .. tostring(normal_sub(1,2))) print("normal call mul " .. tostring(normal_mul(1,2)))