Boost logo

Boost Users :

From: Bernabe Cantos (xphere81_at_[hidden])
Date: 2007-05-09 07:11:22


Bernabe Cantos <xphere81 <at> gmail.com> writes:

>
> Hello, I'm using BOOST libraries in my developments.
>
> I need a trait returning the class type from a pointer to member type.
>
> An example:
>
> member_of<int(A::*)(void)>::type // this equals to 'A'
> member_of<void(B::*)(int) const>::type // this equals to 'B const'
> member_of<void(void)>::type // this equals to an undefined struct
>
> I don't know if the functionality I need exists in BOOST. I hope so.
> If not, should it be difficult to provide?
> Maybe doing some modifications to is_member_function_pointer.
>
> Thanks in advance
>

Assuming that BOOST has no member_of<> traits or similar, I made some little
preprocessor macros to implement it.
This is my first attempt with boost::preprocessor, so I'm asking if this can be
improved easily.
Variable arguments and calling conventions are ugly, I know, but I need them.

#ifndef MEMBER_OF_MAX_PARAMS
  #define MEMBER_OF_MAX_PARAMS 12
#endif

struct undefined { };

template<class TT> struct member_of { typedef undefined type; };

#define MEMBER_OF_impl(z, cv_, np_) \
template< \
  class TT, class RT \
  BOOST_PP_COMMA_IF(np_) BOOST_PP_ENUM_PARAMS(np_, class P) \
> \
struct member_of< \
  RT(MEMBER_OF_callconv TT::*MEMBER_OF_cv(cv_)) \
    (BOOST_PP_ENUM_PARAMS(np_, P)MEMBER_OF_ellipsis(np_)) \
> { \
  typedef typename MEMBER_OF_cv(cv_) TT type; \
};

#define MEMBER_OF_cv(i) \
BOOST_PP_APPLY( \
  BOOST_PP_TUPLE_ELEM(4, i, \
    (BOOST_PP_NIL, (const), (volatile), (const volatile)) \
  ) \
)

#define MEMBER_OF(z, n, unused) \
  BOOST_PP_REPEAT(4, MEMBER_OF_impl, n)

#undef MEMBER_OF_ellipsis
#define MEMBER_OF_ellipsis(n)

#undef MEMBER_OF_callconv
#define MEMBER_OF_callconv
BOOST_PP_REPEAT(MEMBER_OF_MAX_PARAMS, MEMBER_OF, ~)

#undef MEMBER_OF_callconv
#define MEMBER_OF_callconv __stdcall
BOOST_PP_REPEAT(MEMBER_OF_MAX_PARAMS, MEMBER_OF, ~)

#undef MEMBER_OF_callconv
#define MEMBER_OF_callconv __cdecl
BOOST_PP_REPEAT(MEMBER_OF_MAX_PARAMS, MEMBER_OF, ~)

#undef MEMBER_OF_callconv
#define MEMBER_OF_callconv
#undef MEMBER_OF_ellipsis
#define MEMBER_OF_ellipsis(n) BOOST_PP_COMMA_IF(n)...
BOOST_PP_REPEAT(MEMBER_OF_MAX_PARAMS, MEMBER_OF, ~)

#undef MEMBER_OF_ellipsis
#undef MEMBER_OF_callconv
#undef MEMBER_OF
#undef MEMBER_OF_cv
#undef MEMBER_OF_impl


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