Rune FS
Rune FS

Reputation: 21742

getting the name of the invoked function

when using either the '.', '->' or '->*' operators is there any way of getting the name of the function invoked (or the name of the variable in the case of '->*' and everything goes.

EDIT: Just to clarify I'm not talking about reflection but more something in the line of 'function' basically I want to create an overload of an operator (preferrably one of the three mentioned) which is aware of the right side of the operator

What I'm trying to accomplish is basically a compile time transformation of

obj->Function(a,b);

to

obj->Map("Function")(a,b); //Where Map(string) returns a function pointer

EDIT: Just to further clarify, The requirement is that the code will have to look like one of these three

obj.SomeMethod(args);
obj->SomeMethod(args);
obj->*SomeMethod(args);

any solution that fits that is acceptable as long as SomeMethod can be any valid identifier (I.e. any thing that can be used as a function name) and the solution will have to use SomeMethod as a lookup in a Map. The Map part is already implemented it's only the reinterpretation of what looks like a method call I seeking a solution to.

if that (as I'm affraid) can't be done then any solution with a new "operator" syntax will be accepted. Such as obj __ SomeMethod(args); as long as the lookup is based on the RHS of the "operator"

Upvotes: 3

Views: 718

Answers (5)

Karel Petranek
Karel Petranek

Reputation: 15154

I have solved the problem you are facing just recently. I was doing Javascript bindings to C++ and needed to have a map of functions with name -> function. I am calling the functions like this:

vector<WebValue> params;  // Parameters for the function, I get these from the JS engine usually
params.push_back(asValue("Some string"));
params.push_back(asValue(123456));
params.push_back(asValue(1.0f));
functionMap["SomeFunction"]->call(params);  // SomeFunction must accept char*, int and float compatible parameters

and registering it like this:

functionMap["SomeFunction"] = jsExport(this, &MyClass::SomeFunction);

or like this if it's a member function and "this" is available in the current scope:

regMember(SomeFunction);

I used Boost Fusion and Boost Function Types to do the dirty job for me. It's available under the sources of OpenLieroX project:
http://bit.ly/dd70G6

The usage of it:
http://bit.ly/9Sa84q - contains methods for registering the functions to the map
http://bit.ly/b7oWNo - contains the handy regMember macro for registering member functions

You may want to look at other files in the directory as well for a better understanding:
http://bit.ly/b7oWNo

It is not exactly the way you described it in your question but hopefully it helps you.

Upvotes: 1

wilx
wilx

Reputation: 18228

No. C++ has nearly no reflection. Unless you want this for debugging purposes you should not really need it. And debugging can be done even without it.

Answer to the EDIT: Either you use some kind of functor with several overloaded operator() functions or your map will have to contain only functions of same type, or at least of same parameters and return value (if you are willing to use something like boost::function<>). If you use functor with overloaded operator() then you are giving up compile time type safety and you will be limited to some N parameters.

What you have decided to do is akin to boost::bind/boost::function. Take a look at their headers. Unless you want to severally limit your implementation, it is unlikely you can end up with anything simpler than they use.

Upvotes: 0

SigTerm
SigTerm

Reputation: 26409

when using either the '.', '->' or '->' operators is there any way of getting the name of the function invoked (or the name of the variable in the case of '->' and everything goes.

There is no built-in mechanism (i know of) that allows that.

You can get a name of current function during compile time, using __FUNCTION__ macro. It is not standard, but it is supported on MSVC, and may be supported on g++ (not sure).

If you want to know function name during runtime, then you'll have to implement some kind of special mechanism for that - build a table of function names and addresses (which will be initialized at startup) and search it for function name every time you need a function name from address. If compiler supports __FUNCTION__ macros or have any other means of getting name for current function, then you can simplify that by using macros (you can wrap entire "registration" procedure into single macro call that will be placed within function you want to register). This probably will be non-portable.

Program's debug information may also contain information you're looking for but it is even worse than relying on __FUNCTION__ macro.

Upvotes: 1

Stefano Borini
Stefano Borini

Reputation: 143785

As far as I know, no. C++ does not support any form of reflection, so in order to get this kind of information you have to design your own tools and tricks.

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89172

C++ doesn't have any kind of reflection. The names of the functions are gone by the time that your executable is running. The only way to do it is to pass them in.

If you were planning to do something based on the method name, perhaps you just need virtual methods or overloading so that the right code is called.

Upvotes: 1

Related Questions