Member-Function to String conversion.

Hello,
I'm searching for a good solution for the following problem of making
a pair of a string and a pointer to a member function.
---
struct foo1 {
int bar1( int );
void bar2( );
};
int main() {
someobject.set_string( &foo1::bar1, "foobar1" );
someobject.set_string( &foo1::bar2, "foobar2" );
string = someobject.get_string( &foo1::bar1 );
pointer = someobject.get_pointer( "foobar2" );
}
---
The conversion of the string to the function pointer is possible, when
the function pointer is stored in some wrapper like mem_fn.
But the other way is more complicated. Since the member function
pointers are each types of their own, the pointers cannot be compared
and therefore cannot directly be stored in some list. Mem_fn also
doesn't seem to do this job. Using some compiler dependent extension
also isn't useful.
Using templated overloads does work for the conversion of member
function pointer to string - to some extent:
---
global template for setting the string:
template< typename foobar1_pointer_type, foobar1_pointer_type foobar1_pointer>
std::string overloaded_template() {
return "foobar1";
}
local call for getting the string:
string = overloaded_template

Hello, You need two maps (string->pointer and pointer->string), a bimap should do the work. Isn't it possible to cast a function pointer to a generic void * pointer as the base class for the bimap? Andrea
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] Per conto di Siegfried Kettlitz Inviato: lunedì 3 novembre 2008 15.20 A: boost-users@lists.boost.org Oggetto: [Boost-users] Member-Function to String conversion.
Hello,
I'm searching for a good solution for the following problem of making a pair of a string and a pointer to a member function.
--- struct foo1 { int bar1( int ); void bar2( ); };
int main() { someobject.set_string( &foo1::bar1, "foobar1" ); someobject.set_string( &foo1::bar2, "foobar2" );
string = someobject.get_string( &foo1::bar1 );
pointer = someobject.get_pointer( "foobar2" ); } ---
The conversion of the string to the function pointer is possible, when the function pointer is stored in some wrapper like mem_fn.
But the other way is more complicated. Since the member function pointers are each types of their own, the pointers cannot be compared and therefore cannot directly be stored in some list. Mem_fn also doesn't seem to do this job. Using some compiler dependent extension also isn't useful.
Using templated overloads does work for the conversion of member function pointer to string - to some extent:
--- global template for setting the string: template< typename foobar1_pointer_type, foobar1_pointer_type foobar1_pointer> std::string overloaded_template() { return "foobar1"; }
local call for getting the string: string = overloaded_template
() --- But this solution has way too much overhead and doesn't provide the call in the form of "someobject.get_string(pointer)" because of the typed template parameter. It is possible to use macros to make it look better and type less, but macros are ugly and don't solve the problem properly.
So does anyone know a good way to store a string-pointer-pair, so that it can be accessed by the corresponding member function pointer? The solution should work with GCC4.4 for now and can use the c++0x-extensions since i use them anyways.
Kind regards, Siegfried Kettlitz _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

You need two maps (string->pointer and pointer->string), a bimap should do the work. Isn't it possible to cast a function pointer to a generic void * pointer as the base class for the bimap?
Sorry, this doesn't work. A "pointer to a member function" is _not_ a "function pointer" and not a general (void*) pointer. It's size is bigger than a pointer to a normal function because it stores additional information about the object. I haven't found a conversion between all "pointer to member functions" to something "comparable" like a normal pointer or a number or a string. (Maybe typeinfo helps? Is it portable?) Take a look at this for the strange objects that member function pointers are:

If you want to use that pointer, i.e. call the member function, then you must define it in some way (and you must retrieve the member object too). How you define the function? pointer_type pointer; pointer = someobject.get_pointer( "foobar2" ); What is pointer_type? Once you define pointer_type you can use it in the bimap. A pointer to a member function is still a simple function pointer, this is why you need the member pointer to call it. A member function is nothing more than a normal function with the special *this* pointer assigned to the member object that called the function (and of course the code inside the function use implicitly the this pointers when accessing any member item). With virtual functions the function pointer is not static but stored in the class. Andrea
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] Per conto di Siegfried Kettlitz Inviato: lunedì 3 novembre 2008 15.48 A: boost-users@lists.boost.org Oggetto: Re: [Boost-users] Member-Function to String conversion.
pointer as the base class for the bimap?
You need two maps (string->pointer and pointer->string), a bimap should do the work. Isn't it possible to cast a function pointer to a generic void
Sorry, this doesn't work. A "pointer to a member function" is _not_ a "function pointer" and not a general (void*) pointer. It's size is bigger than a pointer to a normal function because it stores additional information about the object. I haven't found a conversion between all "pointer to member functions" to something "comparable" like a normal pointer or a number or a string. (Maybe typeinfo helps? Is it portable?)
Take a look at this for the strange objects that member function pointers are:
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

pointer_type pointer; pointer = someobject.get_pointer( "foobar2" );
What is pointer_type?
That would be something like mem_fn - not actually a pointer but an object that stores the pointer. The actual pointer can be extracted from that object when it is known, or the function can be called via this object.
Once you define pointer_type you can use it in the bimap.
As far as i know, comparing mem_fn or similar functions cannot compare the pointers because they are of different types.

Then use a pointer to mem_fn for the bimap and not directly the function pointer.
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] Per conto di Siegfried Kettlitz Inviato: lunedì 3 novembre 2008 16.37 A: boost-users@lists.boost.org Oggetto: Re: [Boost-users] Member-Function to String conversion.
pointer_type pointer; pointer = someobject.get_pointer( "foobar2" );
What is pointer_type?
That would be something like mem_fn - not actually a pointer but an object that stores the pointer. The actual pointer can be extracted from that object when it is known, or the function can be called via this object.
Once you define pointer_type you can use it in the bimap.
As far as i know, comparing mem_fn or similar functions cannot compare the pointers because they are of different types. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I mean to use a pointer to mem_fn, not a pointer to the member function. pointer_type pointer; pointer = someobject.get_pointer( "foobar2" ); What is pointer_type? You said a mem_fn object. Then use a pointer to this mem_fn object for the bimap item.
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] Per conto di Siegfried Kettlitz Inviato: lunedì 3 novembre 2008 16.47 A: boost-users@lists.boost.org Oggetto: Re: [Boost-users] Member-Function to String conversion.
Then use a pointer to mem_fn for the bimap and not directly the function pointer.
That's not possible because when: mem_fn a( &class::function ); mem_fn b( &class::function ); then a and b are different objects but internally point to the same function. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I mean to use a pointer to mem_fn, not a pointer to the member function.
pointer_type pointer; pointer = someobject.get_pointer( "foobar2" );
What is pointer_type? You said a mem_fn object. Then use a pointer to this mem_fn object for the bimap item.
This would work as you say, but that only moves the problem. How do i get from "&foo1::bar1" to the mem_fn object?

Of course with multiple inheritance or other cases the member function pointers may contain some other information about the class, but still you need to define it to be able to use it.
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] Per conto di Siegfried Kettlitz Inviato: lunedì 3 novembre 2008 15.48 A: boost-users@lists.boost.org Oggetto: Re: [Boost-users] Member-Function to String conversion.
pointer as the base class for the bimap?
You need two maps (string->pointer and pointer->string), a bimap should do the work. Isn't it possible to cast a function pointer to a generic void
Sorry, this doesn't work. A "pointer to a member function" is _not_ a "function pointer" and not a general (void*) pointer. It's size is bigger than a pointer to a normal function because it stores additional information about the object. I haven't found a conversion between all "pointer to member functions" to something "comparable" like a normal pointer or a number or a string. (Maybe typeinfo helps? Is it portable?)
Take a look at this for the strange objects that member function pointers are:
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Andrea Denzler
-
Siegfried Kettlitz