c++使用luabind示例
来源:互联网 发布:人海战术 知乎 编辑:程序博客网 时间:2024/05/22 04:38
直接上代码
//main.cpp
#include <iostream>#include <lua.hpp>#include <luabind/luabind.hpp>extern "C"{ #include "lua.h" #include "lualib.h" #include "lauxlib.h"}bool LoadScript(lua_State* L, const std::string& fname){ if (luaL_dofile(L, fname.c_str())) { std::cerr << lua_tostring(L, -1) << std::endl; return false; } return true;}class NumberPrinter{public: NumberPrinter(int number) : m_number(number) { } NumberPrinter() { m_number = 100; } void printNum() { std::cout << "C++ printNum" << std::endl; std::cout << m_number << std::endl; }private: int m_number;};int main(int argc, char* argv[]){ lua_State* L = luaL_newstate(); luaL_openlibs(L); luabind::open(L); luabind::module(L) [ luabind::class_<NumberPrinter>("NumberPrinter") .def(luabind::constructor<int>()) .def("printNum", &NumberPrinter::printNum) ]; LoadScript(L, "test.lua"); luabind::object tobj = luabind::globals(L)["Game"]; if (luabind::type(tobj) == LUA_TTABLE) { NumberPrinter np1(100); luabind::call_member<void>(tobj, "enter", &np1); } luabind::object tobj2 = luabind::globals(L)["NumberPrinter"]; if (luabind::type(tobj2) == LUA_TTABLE) { luabind::call_member<void>(tobj2, "enter"); } lua_close(L);}
print("BEFORE", _G["NumberPrinter"])---[[local meth = {}meth.__index = methlocal tbl = {}setmetatable(tbl, meth)_G["NumberPrinter"] = tblprint("AFTER", _G["NumberPrinter"])--]]---[[function NumberPrinter:printNum() print("printNum")end--]]--[[p1 = NumberPrinter(1000)p1:printNum()--]]--NumberPrinter:printNum()function NumberPrinter:test() print("THIS IS A TEST")endNumberPrinter:test()function NumberPrinter:enter() print("THIS NUM ENTER")endGame = {}function Game:enter(np) print("THIS IS ENTER") print("USE", _G["NumberPrinter"]) np:printNum()end
//makefile
test:main.og++ main.cpp -o test -I../base/xlib/ -L/usr/lib64/ -llua -ldl -lluabindclean:rm -rf *.orm -rf testrm -rf core*clear:rm -rf core*
0 0
- c++使用luabind示例
- 从零开始使用luabind
- luabind-0.9.1在windows、linux下的使用详解及示例
- 使用luabind生成dll文件
- lua和luabind编译测试使用
- 使用luabind绑定box2d的lua接口
- C#: GDI+使用示例
- Linux下Luabind编译和使用环境的搭建
- c中的atexit函数使用示例
- iOS Objective-C Block 的使用示例
- C语言基础-fscanf函数使用示例
- C#Socket的使用与示例
- Client-Server C程序示例C (使用 Sockets 和 TCP)
- c示例
- baidu luaplus luabind
- luabind常用方法总结
- Quick Introduction to LuaBind
- 神奇的luabind
- 使用html5,css,实现品字形布局
- 重磅!8大策略让你对抗机器学习数据集里的不均衡数据
- Struts+Spring+Hibernate整合入门详解
- strstr函数与翻转字符串
- Ubuntu 安装rabbitvcs
- c++使用luabind示例
- 使用HTML5技术控制电脑或手机上的摄像头
- 求二进制数中1的个数
- redis和memcache
- open系统调用
- CodeForces 534C - Polycarpus' Dice
- iOS errorcode为-1011 问题及解答
- java调用函数参数的传递机制及java内存管理
- JavaScript跨域总结与解决办法