用tolua++把c++类转LUA调用的类库经历分享

                        c++类转成LUA调用的类

第一步:准备需要用的c++类

    例子如下:

    c++头文件定义 

#ifndef __testlua__
#define __testlua__

#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class testlua
{
private:

static std::string testluatxt ;

public:
static std::string getname();
static void setname(string txt);
};
#endif /* defined(__testlua__) */

    c++ CPP文件定义

#include "testlua.h"

string testlua::getname()
{
return "testlua";
}

void testlua::setname(string txt)
{
testluatxt =txt;

}

  

第二步:准备编译的pkg文件

      pkg文件里面得写法和c++的头文件类似,只不过没有关键字,如下:

class testlua
{
static std::string getname();
static void setname(std::string value);
};

第三步:用tolua++编译okg文件

用命令行进入tolua++所在文件夹,用命令编译,命令:./tolua++ -o testlua.cpp testlua.pkg 。运行完成后会生成所需要得testlua.cpp文件。

testlua.cpp文件中得代码如下:

/*
** Lua binding: testlua
** Generated automatically by tolua++-1.0.92 on Thu Sep 4 14:18:20 2014.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua++.h"

/* Exported function */
TOLUA_API int tolua_testlua_open (lua_State* tolua_S);


/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
tolua_usertype(tolua_S,"testlua");
}

/* method: getname of class testlua */
#ifndef TOLUA_DISABLE_tolua_testlua_testlua_getname00
static int tolua_testlua_testlua_getname00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"testlua",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
testlua* self = (testlua*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'getname'", NULL);
#endif
{
std::string tolua_ret = (std::string) self->getname();
tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'getname'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* method: setname of class testlua */
#ifndef TOLUA_DISABLE_tolua_testlua_testlua_setname00
static int tolua_testlua_testlua_setname00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"testlua",0,&tolua_err) ||
!tolua_iscppstring(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
testlua* self = (testlua*) tolua_tousertype(tolua_S,1,0);
std::string value = ((std::string) tolua_tocppstring(tolua_S,2,0));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'setname'", NULL);
#endif
{
self->setname(value);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'setname'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_testlua_open (lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_types(tolua_S);
tolua_module(tolua_S,NULL,0);
tolua_beginmodule(tolua_S,NULL);
tolua_cclass(tolua_S,"testlua","testlua","",NULL);
tolua_beginmodule(tolua_S,"testlua");
tolua_function(tolua_S,"getname",tolua_testlua_testlua_getname00);
tolua_function(tolua_S,"setname",tolua_testlua_testlua_setname00);
tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;
}


#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
TOLUA_API int luaopen_testlua (lua_State* tolua_S) {
return tolua_testlua_open(tolua_S);
};
#endif

第四步:把生成好的文件处理好后放入工程中

在添加一个testlua.h文件,文件内容如下:

extern "C"

{

#include "lua.h"

#include "tolua++.h"

#include "tolua_fix.h"

}

TOLUA_API int luaopen_testlua(lua_State* tolua_s);

然后在testlua.cpp文件中引入该头文件,在工程初始化的时候调用luaopen_testlua该方法。

第五步:在LUA中调用

在lua中调用如下:

if testlua then
local getnametxt = testlua:getname()
print("nametxt: " .. tostring(getnametxt) .. " ")
else
print("LUA ERROR:没有获取到")
end

 

以上是个人在项目中用的方法,如果有不对的地方希望大神指点,也欢迎大家一起学习讨论。QQ:837138108

原文地址:https://www.cnblogs.com/liwenxue88/p/3956037.html