将 luabind 派生成员作为协程调用

发布于 2024-11-16 15:18:18 字数 706 浏览 6 评论 0原文

luabind 文档说要从 C++ 调用 Lua 派生的虚拟成员,您需要创建一个派生自 luabind::wrap_base 的包装类,并像这样调用该函数:

class BaseWrapper : public Base, public luabind::wrap_base
{
    public:
        virtual void foo()
        {
            call<void>("foo");
        }
};

到目前为止一切顺利 - 我有这么多工作。

但是我如何实现BaseWrapper::foo()来调用重写的foo(在Lua端)作为协程(使用resume_function)而不是直接使用 call 调用它?

这就是非成员函数的实现方式:

luabind::object func = luabind::globals(L)["bar"];
luabind::resume_function<void>(func);

我想我需要知道的是如何获取 foofunc (由 Lua 派生类实现),然后我现有的 resume_function 逻辑应该按原样工作。

The luabind documentation says to call a Lua-derived virtual member from C++, you create a wrapper class derived from luabind::wrap_base and call the function like so:

class BaseWrapper : public Base, public luabind::wrap_base
{
    public:
        virtual void foo()
        {
            call<void>("foo");
        }
};

So far so good - I have this much working.

But how do I implement BaseWrapper::foo() to call the overridden foo (on the Lua side) as a coroutine (using resume_function) instead of calling it directly with call?

This is how it's done with non-member functions:

luabind::object func = luabind::globals(L)["bar"];
luabind::resume_function<void>(func);

I think what I need to know is how to get func for foo (as implemented by the Lua-derived class), and then my existing resume_function logic should work as-is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2024-11-23 15:18:18

所以我已经找到了这个问题的答案。看起来最简单的解决方案是在构造对象时从 Lua 传递 self,然后从其表中查找函数:

在 C++ 方面:

BaseWrapper::BaseWrapper(luabind::object self) : _self(self)
{ }

virtual void BaseWrapper::foo()
{
  luabind::object func = _self["foo"];

  /* now put func in coroutine scheduler queue and when appropriate call: 

     luabind::resume_function<void>(func, _self);
  */
}

在 Lua 中:

class 'Derived' (Base)
  function Derived:__init()
    Base.__init(self, self)     -- the second self is param to BaseWrapper()
  end

  function Derived:foo()
    -- here is the target function
  end
end

So I've figured out the answer to this problem. It seems that the simplest solution is to pass self from Lua when you construct the object and then look up the function from its table:

On the C++ side:

BaseWrapper::BaseWrapper(luabind::object self) : _self(self)
{ }

virtual void BaseWrapper::foo()
{
  luabind::object func = _self["foo"];

  /* now put func in coroutine scheduler queue and when appropriate call: 

     luabind::resume_function<void>(func, _self);
  */
}

And in Lua:

class 'Derived' (Base)
  function Derived:__init()
    Base.__init(self, self)     -- the second self is param to BaseWrapper()
  end

  function Derived:foo()
    -- here is the target function
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文