Boost logo

Boost :

From: Vesa Karvonen (vesa_karvonen_at_[hidden])
Date: 2002-03-20 04:30:43


>>Of course, you might actually want to do something a bit more
>>complicated, such as an inline wrapper for each function:
>>
>> public: int foo(int p0, float p1) {return foo_ptr(p0,p1);}
>> private: int (__stdcall *foo_ptr)(int,float);
>>
>That is what the current macros do. I was playing around with the
>typedefs to get a hang on BPL.
[...]
>Currently I have hand coded DECLARE_FUNCTION_N makros for N in [0, 9]
>which I would like to refactor.

Ok. Here is what I'd do. I'd make a macro looking something like this:

#define DECLARE_FUNCTION(cc,ret_type,name,param_cnt,param_type_tuple)\
  public: \
    ret_type name(FORMAL_PARAM_LIST(param_cnt,param_type_tuple,p)) \
    {return name##_ptr(BOOST_PP_ENUM_PARAMS(param_cnt,p));} \
  private: \
    int (cc *name##_ptr)(FORMAL_PARAM_LIST(param_cnt,param_type_tuple,p))

The macro would then be used like this:

  DECLARE_FUNCTION(__stdcall, int, foo, 2, (int, float))
  DECLARE_FUNCTION(__cdecl, int, bar, 1, (double))

(Alternatively you might use a list to pass the parameter types, but this
would require more typing from the user.)

The helper macro FORMAL_PARAM_LIST() generates a sequence of formal
parameters given the count and tuple of parameter types. The easiest way to
make the macro is to convert the tuple into a list and use the
BOOST_PP_LIST_FOR_EACH_I() macro, which is not yet in the current release of
Boost, but is available in the CVS and will be in the next release. Here is
how you can implement the FORMAL_PARAM_LIST() macro:

  #define FORMAL_PARAM_LIST(param_cnt,param_type_tuple,param_name)\
    BOOST_PP_LIST_FOR_EACH_I\
    ( FORMAL_PARAM_LIST_F\
    , param_name\
    , BOOST_PP_TUPLE_TO_LIST(param_cnt,param_type_tuple)\
    )

  #define FORMAL_PARAM_LIST_F(R,param_name,I,param_type)\
    BOOST_PP_COMMA_IF(I) param_type BOOST_PP_CAT(param_name,I)

The above macros generate a list of formal parameters like this:

   FORMAL_PARAM_LIST(2,(int,float),p) -> int p0, float p1

_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk