Binding functions of derived class with luabind

Posted by Anamon on Stack Overflow See other posts from Stack Overflow or by Anamon
Published on 2010-03-25T19:51:59Z Indexed on 2010/03/25 19:53 UTC
Read the original article Hit count: 336

Filed under:
|
|

I am currently developing a plugin-based system in C++ which provides a Lua scripting interface, for which I chose to use luabind. I'm using Lua 5 and luabind 0.9, both statically linked and compiled with MSVC++ 8. I am now having trouble binding functions with luabind when they are defined in a derived class, but not its parent class.

More specifically, I have an abstract base class called 'IPlugin' from which all plugin classes inherit. When the plugin manager initialises, it registers that class and its functions like this:

luabind::open(L);
luabind::module(L) [
    luabind::class_("IPlugin")
    .def("start", (void(IPlugin::*)())&IPlugin::start)
];

As it is only known at runtime what effective plugin classes are available, I had to solve loading plugins in a kind of roundabout way. The plugin manager exposes a factory function to Lua, which takes the name of a plugin class and a desired object name. The factory then creates the object, registers the plugin's class as inheriting from the 'IPlugin' base class, and immediately calls a function on the created object that registers itself as a global with the Lua state, like this:

void PluginExample::registerLuaObject(lua_State *L, string a_name)
{
    luabind::globals(L)[a_name] = (PluginExample*)this;
}

I initially did this because I had problems with Lua determining the most derived class of the object, as if I register it from the StreamManager it is only known as a subtype of 'IPlugin' and not the specific subtype. I'm not sure anymore if this is even necessary though, but it works and the created object is subsequently accessible from Lua under 'a_name'.

The problem I have, though, is that functions defined in the derived class, which were not declared at all in the parent class, cannot be used. Virtual functions defined in the base class, such as 'start' above, work fine, and calling them from Lua on the new object runs the respective redefined code from the 'PluginExample' class. But if I add a new function to 'PluginExample', here for example a function taking no arguments and returning void, and register it like this:

luabind::module(L) [
luabind::class_("PluginExample")
    .def(luabind::constructor())
    .def("func", &PluginExample::func)
];

calling 'func' on the new object yields the following Lua runtime error:

No matching overload found, candidates:
void func(PluginExample&)

I am correctly using the ':' syntax so the 'self' argument is not needed and it seems suddenly Lua cannot determine the derived type of the object anymore. I am sure I am doing something wrong, probably having to do with the two-step binding required by my system architecture, but I can't figure out where. I'd much appreciate some help =)

© Stack Overflow or respective owner

Related posts about luabind

Related posts about c++