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:
开发者_如何学Goluabind::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.
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
精彩评论