• Advertisement
Sign in to follow this  

Templates and Function pointers

This topic is 1320 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

I don't really see why createMetatable() is a template function.

Anyway, you try to move the push_back()s into other functions (one for ActiveState*, one for Area*. etc.). That way you should be able to get rid of the compiler errors.

Share this post


Link to post
Share on other sites

that raises a question,

if I have a Camera which has a GUID

is it safe to get the a GUID's function pointer for Camera with GUID::get instead of Camera::get

 

that way I could replace T:: with the base class of the function

Edited by Joshhua5

Share this post


Link to post
Share on other sites

I recommend reading this, it also has complete source code.

 

Your method won't work, except if the l* functions have the same signature as the Lua C function typedef (which is a bit ugly and that shouldn't be needed). You could also use an existing library like LuaBridge or luawrapper.

Share this post


Link to post
Share on other sites

the l* functions are all following the lua_CFunction, because they're the c++ functions that lua can call, I added on the l to make that easier to understand.

Share this post


Link to post
Share on other sites

A lot of unneded work...

When you mimic c++ behavior in lua, then metatables created just once.

Every lua object (userdata, holding pointer to c++ object) will share the same metatable, storing class' methods.

But the fact this function exists implies it will be called many times, for every created c++ object, even if it's of the same class.

 

Sorry if it looks offtopic, but I see it as XY problem.

Edited by vstrakh

Share this post


Link to post
Share on other sites

I know of an alternative you might enjoy reading about. It is a bit advanced and makes use of some more tricky C++, but overall the idea is pretty simple. Luckily I wrote an article about this already, here's the link (I'll post a short summary here): http://www.randygaul.net/2014/01/01/automated-lua-binding/

 

So there's a few different ways to go about the implementation of binding objects, but there's only two ways to send things to Lua (that I know of). The first way is to send things be value, and the other is by reference. Sending things by reference means you send a pointer to some memory that C++ manages. By value means you create some sort of copy (probably with tables) of a C++ object, and the memory of the copy is managed by Lua. The former is generally easier to implement than the latter (in my opinion).

 

My favorite way to bind functions and class/struct methods is with some kind of introspection that automates the process.

 

As for metatables specifically, I just create one per class type (like you seem to have). With a simple implementation of some introspection you can detect if some class derives from another, and make use of some kind of virtual overriding without much trouble. I haven't really gone into all that simply because I just didn't think it would be necessary for my purposes.

Edited by Randy Gaul

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement