function / method by macro

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

function / method by macro

Post by Pangamini »

Hi there.
Do you think it is possible (in visual c++) to use some macro to define a function / method and register it somewhere at the same time? Like:

Code: Select all

LUAAPI int SetMaxForce(float force)
{
...
}
the LUAAPI macro would define a function like normal, but parse its header definition and create an object - so the result from preprocessor and above code would be like:

Code: Select all

int SetMinMaxForce(float maxforce, float minforce)
{
....
}

ApiRegistrator( TYPE_INT,  &SetMaxForce, TYPE_FLOAT, TYPE_FLOAT);
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: function / method by macro

Post by Pangamini »

Or even better

Code: Select all

int SetMinMaxForce(float maxforce, float minforce)
{
....
}

ApiRegistrator( TYPE_INT, "SetMinMaxForce"  &SetMaxForce, TYPE_FLOAT, "maxforce", TYPE_FLOAT, "minforce");
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 50

Re: function / method by macro

Post by madmarx »

It's possible to do something like :

DeclareFunction(int,SetMinMaxForce,floa,maxforce,float,minforce)
{
....
}

which will generate :

Code: Select all

int SetMinMaxForce(float maxforce, float minforce);
ApiRegistrator( TYPE_INT, "SetMinMaxForce"  &SetMaxForce, TYPE_FLOAT, "maxforce", TYPE_FLOAT, "minforce");

int SetMinMaxForce(float maxforce, float minforce);
{
....
}
But it won't work if you got comma in your types (like std::map<int,int>). So another one that is available is :

Code: Select all

DeclareFunction((int)(SetMinMaxForce)(float)(maxforce)(float)(minforce))
{
....
}
I already did one like this once.

But before you got further here are 2 other potential ways of doing it.
A/ use your own code parser/generator. In that case, what you proposed becomes completely doable, and more readable/maintainable.
B/ use boost to extract the function characteristics. So you just need to get the function name as input, and everything else can be generated. I think boost::function + boost::fusion should be able to do it well. Have a look at how ChaiScript works (in its c++ side, when you declare function), because I think it can do just that.
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: function / method by macro

Post by Pangamini »

Thanks for reply. I don't know though, how to use "your own code parser / generator". It's like writing your own preprocessor or some layer in it? Do i have to use some tools like cmake, or can i do this in visual studio?
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 50

Re: function / method by macro

Post by madmarx »

Basically, you read the file yourself, and parse it as a string and then generate the .h or .cpp.
Personnaly i use "boost.spirit" to do the parsing stuff into a struct representing an header file, and then I generate the output manually (edit : I mean "without using spirit auto generator code", but my own generator from the struct).
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
User avatar
Pangamini
Gremlin
Posts: 179
Joined: Fri Sep 17, 2010 2:21 pm
Location: Kosice, Slovakia
x 3

Re: function / method by macro

Post by Pangamini »

I discovered LuaBind... not exactly the solution for the given, but it does prety much what i wanted and even more!
Ancient Greeks did have a culture, but they didn't have gasoline.
...and we have gasoline.