We have class lua.
In lua class there is a method registerFunc() which is defined:
void lua::registerFun() {
lua_register( luaState, "asd", luaApi::asd);
lua_register( luaState, "zxc", luaApi::zxc);
}
lua_register is a built-in function from lua library: http://pgl.yoyo.org/luai/i/lua_register
it takes static methods from luaApi class as an 3rd argument.
Now some programmer wants to use the lua class, so he is forced to create his own class with definitions of the static methods, like:
class luaApi {
public:
static int asd();
static int zxc();
};
and now is the point. I don’t want (as a programmer) to create class named exactly “luaApi”, but e.g. myClassForLuaApi.
But for now it’s not possible because it is explicitly written in the code – in lua class:
lua_register( luaState, "asd", luaApi::asd);
I would have to change it to:
lua_register( luaState, "asd", myClassForLuaApi::asd);
but I don’t want to (let’s assume that the programmer has no access there).
If it’s still not understandable, I give up. 🙂
Thanks.
You should be able to pass any method as the third parameter of
lua_register()as long as it’s got the correct signature (int (*)(lua_State *L)a.k.a.lua_CFunction). It doesn’t have to be a static member of a class and can be any standalone function as well:If this doesn’t satisfy you, you should add some bigger code snipped, e.g. your
luaclass. How do you add new instances of calls tolua_register()? Are you using some predefined macro? Aren’t you allowed to modify the code inside theluaclass? Latter case there’s nothing you can do unless you use some preprocessor definitions or macros to “bend” your names.Edit:
If you’d like the users of your
luaclass to be able to register their own functions, then you should just allow them to pass it as a parameter to the method (essentially the way Lua does this as well):If you’d like to abstract away Lua’s state object, you could as well think of doing your own wrapper calling the user’s function with a custom object (having the
luaobject as well as all arguments and handling return values).