Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


Templates and Function pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Joshhua5   Members   -  Reputation: 1082

Like
0Likes
Like

Posted 11 June 2014 - 03:57 AM

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.



Sponsor:

#2 Jan2go   Crossbones+   -  Reputation: 1462

Like
0Likes
Like

Posted 11 June 2014 - 04:19 AM

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.



#3 Joshhua5   Members   -  Reputation: 1082

Like
0Likes
Like

Posted 11 June 2014 - 04:28 AM

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, 11 June 2014 - 04:29 AM.


#4 Jan2go   Crossbones+   -  Reputation: 1462

Like
0Likes
Like

Posted 11 June 2014 - 07:50 AM

If get() is a virtual function it will work.



#5 ProtectedMode   Members   -  Reputation: 1335

Like
2Likes
Like

Posted 11 June 2014 - 08:12 AM

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.



#6 Joshhua5   Members   -  Reputation: 1082

Like
0Likes
Like

Posted 11 June 2014 - 08:19 AM

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.



#7 vstrakh   Crossbones+   -  Reputation: 1073

Like
0Likes
Like

Posted 11 June 2014 - 02:00 PM

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, 11 June 2014 - 03:34 PM.


#8 Randy Gaul   Members   -  Reputation: 1426

Like
0Likes
Like

Posted 11 June 2014 - 08:40 PM

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, 11 June 2014 - 08:44 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS