转自:http://hi.baidu.com/xjxxjx1017/blog/item/8fdcbbfce75b1b89b801a037.html
网上关于这方面的资料真少啊... 我自己都不得不从google找资料.
google时, 有时候中国的IP会被屏蔽, 打不开的网页可以试一试在线代理, 比如酷摆.
下面是正题:
在我仔细看了luabind的说明之后, 发现luabind对引用和指针都有很好的支持, 并不是没有办法.
下面这一段就专门说了, luabind对于引用和指针用了一个叫Pilicies的处理办法.
//--14 Policies
Sometimes it is necessary to control how luabind passes arguments and return value, to do this we have policies. All policies use an index to associate them with an argument in the function signature. These indices are result and _N (where N >= 1). When dealing
with member functions _1 refers to the this pointer.
14.1 adopt
14.2 dependency
14.3 out_value
14.4 pure_out_value
14.5 return_reference_to
14.6 copy
14.7 discard_result
14.8 return_stl_iterator
14.9 raw
14.10 yield
具体的就google吧,http://www.rasterbar.com/products/luabind/docs.html,
这个用在线代理就看得到, 是luabind的说明书.
然后呢...我自己总结了一些开发工作中常用的东西, 大致都写在了一个叫备注的文档里, 现在贴出来, 看不懂的也没关系, 觉得好用的就拿来用吧.
//--lua && stl (仅遍历适用)-------OK
#include <luabind/iterator_policy.hpp>
struct X
{
std::vector<std::string> names;
};
module(L)
[
class_<X>("X")
.def (constructor<>())
.def_readwrite("names", &X::names, return_stl_iterator)
];
> x = X()
> for name in x.names do
> print(name)
> end
//参照网上的, 自己添加了一个iterator_map_policy.hpp, 使得lua可对map进行遍历. 要的话留个邮箱.
//iterator做返回值. --注意下面那个的返回值是引用
vector<string>& getNames () {...}
.def("getNames", &getNames, return_stl_iterator)
> for name in getNames() do
> print(name)
> end
//--返回指针-----OK
#include <luabind/adopt_policy.hpp>
X* create()
{
return new X;
}
module(L)
[
def("create", &create, adopt(result))
];
//--返回引用
#include <luabind/dependency_policy.hpp>
struct X
{
B member;
B& get() { return member; }
};
module(L)
[
class_<X>("X")
.def("get", &X::get, dependency(result, _1)),
];
//--nil可以用作逻辑判断
both nil and the boolean value false represent false in a logical expression
//--在lua脚本中对以下语句的尝试------OK
a = 1
if (a ~= nil) then a = nil end
if (a == nil) then a = 1 end
//--多行注释
用"--[["和"]]"将多行的字符串
//全局变量的操作 lua→c++, c++→lua
int ggg = object_cast<int>(globals(L)["ggg"]);
luabind::globals(myLuaState)["MyResourceManager"] = &MyResourceManager
//hge+lua的错误处理
try
{
//调用lua函数
}
catch(luabind::error& e)
{
string tmp = e.what () + string (":\n") + lua_tostring(lua, -1);
MessageBox(NULL, tmp.c_str (), "luaError", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
}
//成员变量
module(L)
[
class_<A>("A")
.def_readwrite("val", &A::val)
];
//文件之间共享全局变量
luaL_dofile(L, "selfadd.txt");
//传递参数的引用给lua函数
call_function<A<int>>(L, "selfadd", boost::ref(tmp));
分享到:
相关推荐
按官方diff文件修改过的luabind代码,修正了在使用lua5.1时,编译luabind出现编译错误的兼容性问题: error C2065: \'LUA_NOREF\' : undeclared identifier
为了使用LuaBind, 你必须包含 lua.h 和 LuaBind 的主要头文件: extern "C" { #include "lua.h" } #include <luabind/luabind.hpp> 这些头文件提供了注册函数和类的功能. 如果你只是想获得函数或者类的支持,你可以...
用于lua 脚本测试,luabind,里面可以自己加脚本
BIN_LuaBind 是一个和 luabind、tolua 类似的库,提供了简洁的 C/C 到 lua 的绑定机制,但与之不同的是BIN_LuaBind 比他们更安全:lua 层不在直接依赖于 C/C 层指针,避免野指针引起宕机。除此之外,BIN_LuaBind 对 ...
Luabind 提供对lua的类型安全绑定的支持的C ++库 示例LUA: function init(id) mylib.print_message("in init. got: " .. id) local announce_id = mylib.announce("henry") mylib.print_message("announce got...
LuaBind 是一个帮助你绑定C++和Lua的库.她有能力暴露 C++ 函数和类到 Lua . 她也有 能力支持函数式的定义一个Lua类,而且使之继承自C++或者Lua. Lua类可以覆写从 C++ 基类 继承来的虚函数. 她的目标平台是Lua 5.0 ,不...
luabind:Luabind是一个库,可帮助您在C ++和Lua之间创建绑定
直接下载使用,已经编译好的lib文件 有需要的直接拿走,绝对可以使用,本人自己也在使用,lua5.14版本和luabind9.01版本
介绍lua,及luabind在vs2012下的搭建方法
lua库应用可以绑顶C和C++语言应用的结合功能,交互调用执行此功能
自已学lua和luabina的时候,写的测试例子,可供初学者使用
luabind-0.9.1.lib 使用VS2008,lua5.1.2,boost1.47.0编译好的luabind-0.9.1,由于是采用静态编译的LIB,文件比较大。使用方法也可参照我的CSDN的博客。
导出c++类、函数、变量等到lua的实现代码,提供思路。类似luabind、tolua++,部分特性如继承导出等暂未实现。
luabind源码包,支持c++与lua的交互;它可以处理继承和模板类
protoc-gen-luabind Google protobuf lua 端口,luabind 基础
Luabind 使用手册(http://www.rasterbar.com/products/luabind/docs.html) 中文版 高清PDF 一年前传过一个当时Luabind版本才0.8,后来出了0.9.1,今天有空了就更新了一下。有些语句意思是懂了,但却不知道怎么用...
压缩包里面有 1.lua5.1开发手册中文版 CHM 2.luabind开发手册PDF 3.国内强人写的Lua开发指南 , pdf 4.program in lua chm
Luabind中文手册.pdf
luabind is a library, inspired by and similar to Boost.Python, that helps you create bindings between C++ and Lua. It has the ability to expose functions and classes, written in C++, to Lua.
lua做的一个自动测试框架,里面包含了编译通过的luabind luatinker等三方库