找回密码
 立即注册
查看: 230|回复: 0

toLua++ 下载地址

[复制链接]
发表于 2023-4-9 11:44 | 显示全部楼层 |阅读模式
toLua:简洁的使用说明

2013年09月18日⁄ 综合 ⁄ 共 3926字 ⁄ 字号   小 中 大⁄ 评论关闭



tolua的最新版本是5.0,下载地址:http://www.tecgraf.puc-rio.br/~celes/tolua/
以下是简单的使用说明:
功能内容:可以在LUA脚本中使用C++的方便对象,包含创建,释放,调用成员函数
文件包括:Main.cpp,tClass.cpp,tClass.h,tClass.pkg,tClassLua.cpp(自动生成)
tClass.h(定义提供给LUA使用的类)
#ifndef TCLASS_H
#define TCLASS_H
// 简单的类
class A
{
public:
  int a;
  A( int nTemp );
  int Get();
};
#endif
tClass.cpp(类的简单实现)
#include "tClass.h"
A::A( int nTemp )
{
  a = nTemp;
}
int A::Get()
{
  return a;
}
tClass.pkg(重要的一环节)
tolua会根据tClass.pkg来自动生产tClassLua.cpp,tClass.pkg是手动编写的,而已格式也有规定
,以下是tClass.pkg内容:
$#include "tClass.h"
class A
{
  int a;
  A( int nTemp );
  int Get();
};
.pkg的书写格式与C++的格式差不多,是只没有大部分的关键字,例如:public等,注意,需要包函
tClass.h的头文件,格式是$#include "tClass.h",还有一点书注意的,由于类中的权限关键字
不存在,所以生成代码的时候可能会出现权限访问错误,例如在int a是私有的情况,可能生产的代
码会出问题。所以设计类的时候需要注意.
生成代码:在命令行中输入:tolua -o tClassLua.cpp,tClass.pkg
生成的代码
/*
** Lua binding: tClass
** Generated automatically by tolua 5.0a on 05/25/06 17:00:17.
*/
#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"
#include "tolua.h"
/* Exported function */
TOLUA_API int tolua_tClass_open (lua_State* tolua_S);
#include "tClass.h"
/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
  tolua_usertype(tolua_S,"A");
}
/* get function: a of class  A */
static int tolua_get_A_A_a(lua_State* tolua_S)
{
   A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in accessing variable 'a'",NULL);
#endif
  tolua_pushnumber(tolua_S,(lua_Number)self->a);
  return 1;
}
/* set function: a of class  A */
static int tolua_set_A_A_a(lua_State* tolua_S)
{
   A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
  tolua_Error tolua_err;
  if (!self) tolua_error(tolua_S,"invalid 'self' in accessing variable 'a'",NULL);
  if (!tolua_isnumber(tolua_S,2,0,&tolua_err))
  tolua_error(tolua_S,"#vinvalid type in variable assignment.",&tolua_err);
#endif
   self->a = ((int)  tolua_tonumber(tolua_S,2,0));
  return 0;
}
/* method: new of class  A */
static int tolua_tClass_A_new00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
  tolua_Error tolua_err;
  if (
  !tolua_isusertable(tolua_S,1,"A",0,&tolua_err) ||
  !tolua_isnumber(tolua_S,2,0,&tolua_err) ||
  !tolua_isnoobj(tolua_S,3,&tolua_err)
  )
  goto tolua_lerror;
  else
#endif
  {
   int nTemp = ((int)  tolua_tonumber(tolua_S,2,0));
  {
   A* tolua_ret = (A*)  new A(nTemp);
  tolua_pushusertype(tolua_S,(void*)tolua_ret,"A");
  }
  }
  return 1;
#ifndef TOLUA_RELEASE
  tolua_lerror:
  tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err);
  return 0;
#endif
}
/* method: Get of class  A */
static int tolua_tClass_A_Get00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
  tolua_Error tolua_err;
  if (
  !tolua_isusertype(tolua_S,1,"A",0,&tolua_err) ||
  !tolua_isnoobj(tolua_S,2,&tolua_err)
  )
  goto tolua_lerror;
  else
#endif
  {
   A* self = (A*)  tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Get'",NULL);
#endif
  {
   int tolua_ret = (int)  self->Get();
  tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
  }
  }
  return 1;
#ifndef TOLUA_RELEASE
  tolua_lerror:
  tolua_error(tolua_S,"#ferror in function 'Get'.",&tolua_err);
  return 0;
#endif
}
/* Open function */
TOLUA_API int tolua_tClass_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,"A","A","",NULL);
  tolua_beginmodule(tolua_S,"A");
  tolua_variable(tolua_S,"a",tolua_get_A_A_a,tolua_set_A_A_a);
  tolua_function(tolua_S,"new",tolua_tClass_A_new00);
  tolua_function(tolua_S,"Get",tolua_tClass_A_Get00);
  tolua_endmodule(tolua_S);
  tolua_endmodule(tolua_S);
  return 1;
}
如果生成成功就没有任何的提示.现在可以把tClassLua.cpp加到我们的工程中;
到最后的使用过程
注意:由于生成的过程中,根据包的文件生成的一个函数,这里我们需要在Main中定义一下!
TOLUA_API int tolua_tClass_open (lua_State* tolua_S);
然后需要调用它,让它把生成的代码内嵌到LUA中.
Main.cpp
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "tolua.h"
#include "tClass.h"
#include <string>
TOLUA_API int tolua_tClass_open (lua_State* tolua_S);
int main ()
{
  lua_State* L = lua_open();
  luaopen_base(L);

  tolua_tClass_open( L );
  // 调用脚本过程
  // 在脚本中就可以随意的使用这个类型
  std::string str = "obj = A:new(2); print(obj:Get() )";
  lua_dostring( L,str.c_str() );
  lua_close(L);
  return 0;
}
tolua还有比较多的功能,都是能够将C/C++的类型,函数,结构等等,方便的内嵌入LUA中调用!


懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-22 14:55 , Processed in 0.110817 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表