Luabridge:返回C ++的寿命管理对象 [英] Luabridge: Returning C++ lifetime managed object

查看:473
本文介绍了Luabridge:返回C ++的寿命管理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这剪断基本类型的作品:

  INT CreateBasicObject(lua_State * L)
{
    INT ret0;    lua_pushinteger(L,ret0);    返回1;
}

和Lua中,它看起来是这样的:

 本地NewObject的= CreateBasicObject()

我怎么会去的返回班,而不是整数

 推(L,放大器; myObject的);
返回1;

似乎并没有正常工作,卢阿部分看起来是这样的:

  self.MyObject = Screen.MyObject();

和错误是:

 试图索引字段'为MyObject'(一个数值)


解决方案

最新LuaBridge 版本,可以使用 RefCountedPtr 例如:

一些C ++定义

 结构A {};静态RefCountedPtr< A> SomeA(){
 返回RefCountedPtr< A>(新一);
}

和绑定:

  luabridge :: getGlobalNamespace(L)
  .beginClass< A>(A)
   .addConstructor<无效(*)(),RefCountedPtr< A> >()
  .endClass()  .addFunction(SomeA,&放大器; SomeA);

现在,你可以返回 A 对象安全并传递给其他的Lua绑定功能 RefCountedPtr< A>

在LUA然后你可以使用它像:

 本地A = A()
--do用的东西...当地B = SomeA()
--do用b东西...

This snipped works for basic types:

int CreateBasicObject(lua_State *L)
{
    int ret0;

    lua_pushinteger(L, ret0);

    return 1;
}

and in lua it looks like this:

local NewObject=CreateBasicObject()

How would I go about returning classes instead of ints?

push(L,&MyObject);
return 1;

does not seem to work correctly, lua portion looks like this:

self.MyObject=Screen.MyObject(); 

And the error is:

attempt to index field 'MyObject' (a number value)

解决方案

In the newest LuaBridge version you can use RefCountedPtr for example:

some C++ definition

struct A {};

static RefCountedPtr<A> SomeA() {
 return RefCountedPtr<A>(new A);
}

and the binding:

luabridge::getGlobalNamespace(L)
  .beginClass<A>("A")
   .addConstructor< void (*) (), RefCountedPtr<A> >()
  .endClass()

  .addFunction("SomeA",&SomeA);

now you can return A objects safely and pass them to other Lua-bound functions as RefCountedPtr<A>

in lua you can then use it like that:

local a=A()
--do something with a...

local b=SomeA()
--do something with b...

这篇关于Luabridge:返回C ++的寿命管理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆