Boost logo

Boost Users :

Subject: Re: [Boost-users] Member-Function to String conversion.
From: Andrea Denzler (andrea_at_[hidden])
Date: 2008-11-03 09:38:27


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_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] Per conto di Siegfried Kettlitz
> Inviato: lunedì 3 novembre 2008 15.20
> A: boost-users_at_[hidden]
> 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<foobar1_pointer_type, &foo1::bar1>()
> ---
>
> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net