Okay so for a bit of background, I need to create a list of function names and their function pointers so I can tell Lua how to use the objects I create. How I've tried to do this is with a template which uses a dynamic_cast to see if it inherits from a class, if it does it'll add the function pointers to the vector and keep checking other classes. I did it like this because as an example I have a GUID class which basically everything will inherit from, if I didn't do it like this I'd have to repeat the function pointer and it's name for every metatable and I'd have a massive list for each class.
My issue is I had a method of doing this, that didn't compile
template<typename T> vector<luaL_Reg> createMetatable(){
vector<luaL_Reg> out;
T object;
if (dynamic_cast<Component::ActiveState*>(&object)){
out.push_back({ "activate", &T::lActivate });
out.push_back({ "deactivate", &T::lDeactivate });
out.push_back({ "isActive", &T::lIsActive });
}
if (dynamic_cast<Component::Area*>(&object)){
out.push_back({ "getDimensions", &T::lGetDimensions });
out.push_back({ "setDimensions", &T::lSetDimensions });
}
...
does anyone have a recommendation of an alternative or a way of getting this to work, your help would be much appreciated.
The exact issue with this code is that at compile time let's say you pass in Camera to the template, all those if statements that wouldn't run anyway because the dynamic cast will fail, will still have the Camera::lGetDimensions and that'll break the build because it can't find that function.