I'm trying to set up some stuff with Lua, but the specifics of Lua aren't important for my question.
What I would like to be able to do is call a function, say OpenLib<T>(L)
, and have it get the table name for a particular class (as well as it's table) and register it with Lua. It essentially boils down to this:
template <class T>
static void OpenLib(lua_State* L)
{
// this func does some other stuff too that I'm omitting, important bit below
if (T::myTable && T::myTableName)
{
luaL_openlib(L, T::myTableName, T::myTable, 0);
}
}
I've tried this a few different ways and I can't get it to work right. I tried making a base class that contains myTable and myTableName like so:
class LuaInfo
{
public:
static const char* myTableName;
static luaL_reg* myTable;
}
Then I could just inherit from LuaInfo, and then fill in the info that I needed. That didn't work because all classes that inherit from LuaInfo would get the same info, so I looked around and got the idea of doing this:
template <class t>
class LuaInfo
// ...
Which made the syntax to initialize it a little silly as I now have to do class Widget : public LuaInfo, but it was closer to working.
template <class T>
void OpenLib(lua_State* L)
{
if (T::myTable && T::myTableName)
{
luaL_openlib(L, LuaInfo<T>::myTableName, LuaInfo<T>::myTable, 0);
}
}
I've tried a few variants of this to try to get it right but I keep getting errors like
undefined reference to `ag::LuaInfo<ag::ui::Widget>::myTable'
Is what I want to do possible, and if so, whats the right way to go about doing it?