Boost logo

Boost Users :

Subject: [Boost-users] Member-Function to String conversion.
From: Siegfried Kettlitz (siegfried.kettlitz_at_[hidden])
Date: 2008-11-03 09:20:09


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 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