toLua:简洁的使用说明
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中调用!
toLua:简洁的使用说明相关推荐
- WT588F02B-8S(芯片代码C001_01)语音芯片在化妆品/保健品/食品行业保质期和使用期得应用解决方案
方案概述及食药品使用痛点 一般化妆品/保健品/食品/药品都有保质期(出厂或生产日开始计算)和有效期(打开包装多少天必须用完的时间)的规定,这些时间都是国标的强制规定,是通过大量实验得出的结果.有不少细 ...
- ID生成方案之号段模式
号段模式ID生成器组件地址 https://github.com/15928587230/os-component-idworker 开箱即用, github上面有简洁的使用说明. 一 Leaf号段模 ...
- 语音芯片在化妆品、保健品、食品行业,保质期解决方案WT588F02B
WT588F02B-8S(芯片代码C001_01)语音芯片在化妆品/保健品/食品行业保质期和使用期得应用解决方案 1. 方案概述及食药品使用痛点 一般化妆品/保健品/食品/药品都有保质期(出厂或生产日 ...
- 动窗口的制作暨CSizingControlBar类的使用说明
动窗口的制作暨CSizingControlBar类的使用说明 *********************************************************** 本文在实现 ...
- Gradle+IDEA使用说明
原文 导语: IDEA拥有大量的JAVA开发者拥护,相比于开源的eclipse,IDEA拥有更简洁直观的界面,拥有更强大的自动补全功能,号称能"一路敲回车完成编码".如果把IDEA ...
- Unity3D热更新技术点——ToLua(上)
注: 本文主要介绍tolua的基本原理及其在unity中的使用,希望阅读本文的读者有lua基础,可通过Lua教程 (其中也有IDE的推荐等)或其他途径先进行lua 的学习 热更新 在介绍tolua前, ...
- 项目开发-工具-版本控制Git完整系统化使用说明
Git使用说明 前言 1. 起步 1.1 关于版本控制 1.2 Git 简史 1.3 Git 是什么? 1.4 命令行 1.5 安装 Git 1.6 初次运行 Git 前的配置 1.7 获取帮助 1. ...
- VOT Toolkit工具使用说明(Python版)
VOT Toolkit工具使用说明(Python版) 一. 工具链接 vot-toolkit python版 github链: https://github.com/votchallenge/tool ...
- Unity热更新ToLua框架学习
一.Lua语言学习 二.ToLua框架 三.使用ToLua框架对Lua&C#进行交互 四.使用Lua对GameObj进行操作 五.将游戏对象打包&解包 六.将Lua脚本文件进行打包&a ...
最新文章
- mysql 判断日期是否在某范围内_判断时间是否在某个区间内
- 道家遁甲式(又名道家奇门)
- Bypass WAF实战总结
- 交换机两个链路相连一些设置
- 吴恩达深度学习1.3练习_Neural Networks and Deep Learning
- 慵懒中长大的人,只会挨生活留下的耳光
- 64位的windows server 2003运行IIS6运行32位的.NET程序
- 用WPS Office下五子棋(转)
- 找到Java中长整型(long)所能表示的最大素数: 9223372036854775783
- EOJ 2706 Fenwick Tree 树状数组找规律
- 关于阿里云主机数据丢失问题,是常态还是个例?如何保障数据安全?...
- 标准摇滚电吉他音源 Orange Tree Samples Evolution Rock Standard
- 提问的智慧/ 如何优雅的提问
- 我想开发一个小程序,大概需要多少钱?
- C++ 雇员信息保存及查询
- 代码注释规范之Doxygen
- 8-14:HTML学习#30
- 哥伦比亚 计算机 硕士几年,哥伦比亚大学金融工程硕士学制几年?
- 苹果Ipad锁屏密码忘记之后,如何不会变成砖
- 富士通扫描仪PaperStream系列软件喜迎新成员;B2B音乐授权市场Songtradr完成D轮融资并获超额认购| 全球TMT...
热门文章
- 西安信息资源网(计算机电子书)
- AI实现语音文字处理,PaddleSpeech项目安装使用 | 机器学习
- python量化分析系列之---5行代码实现1秒内获取一次所有股票的实时分笔数据
- R plot图片背景设置为透明_一文学会网络分析——Cooccurrence网络图在R中的实现...
- Rabbitmq学习笔记(尚硅谷2021)
- 分布式事务解决方案Seata
- 【基于STM32F103+AS608的智能打卡系统】
- 完美解决Idea unable to access git 错误
- 清除office多余的激活信息
- AutoFac基本使用-笔记