1

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);

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

4

1 回答 1

1

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

在 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
于 2011-06-23T13:32:32.333 回答