Boost logo

Boost :

From: Markus Schöpflin (markus.schoepflin_at_[hidden])
Date: 2002-03-19 10:06:49

> From: "Vesa Karvonen" <vesa_karvonen_at_[hidden]>
> >I'm trying to use the BPL for the following and I'm kind of stuck.
> [...]
> I have trouble understanding the intention. If you just want a new syntax
> for specifying function types, then I would recommend to not do it. If you
> want a macro for specifying functions types that take N arguments of a
> single type, then there might be some sense. What is it that you are trying
> to macroize?

Ok, I'll explain a little more. I have written a utility header which allows to load DLLs dynamically without much typing for the user. It basically looks like this (of the top of my head...):

class my_dll : public dynamic_dll_loader
  DECLARE_FUNCTION_2(__stdcall, int, foo, int, float);
  DECLARE_FUNCTION_1(__cdecl, int, bar, double);
  my_dll() : dynamic_dll_loader("path_to_my_dll")

The base class takes care of loading and unloading the DLL. The DECLARE_FUNCTION_N makros insert all the necessary declarations and variables and the LOAD_FUNCTION() makros retrieve the function pointers needed.

Later on, you use it like this:

my_dll dll;, 1);

The ultimate goal is to use the BPL to implement the DECLARE_FUNCTION_N makros.

> Are you positive about the syntax of function typedefs that you seem to be
> trying to achieve? The following is a representative of a function
> declaration (of 1 parameter):
> ret_type fun_name(param);
> To get a typedef from any declaration, you just put `typedef´ in front of
> the declaration:
> typedef ret_type fun_type(param);

The following works for me (it's from the DECLARE_FUNCTION_1 makro):

typedef ret_type (cc function_type)(param1);

This gets expanded to

typedef int (__cdecl function_type)(double);

and my compiler (MSVC6SP5) is happy with it.

> Anyway, you obviously need to pass the param to the macro. Try the
> following:
> #include <boost/preprocessor.hpp>
> #define MAKE_FUNCTION_TYPE_NAME(f_name)\
> BOOST_PP_CAT(f_name,_type)
> #define MAKE_TYPEDEF_N(ret_type,f_name,n_params,param)\
> typedef ret_type f_name(MAKE_FUNCTION_TYPE_NAME(f_name))\
> (BOOST_PP_ENUM_PARAMS(n_params,param))
> #define TYPEDEF_0(ret_type,f_name)\
> MAKE_TYPEDEF_N(ret_type,f_name,0,_)
> #define TYPEDEF_1(ret_type,f_name,param0)\
> MAKE_TYPEDEF_N(ret_type,f_name,1,param0)
> The above (probably) isn't what you were after, however. You probably want
> to be able to pass an arbitrary number of parameters:
> TYPEDEF_3(void,bar,foo_type,int,long)
> And get:
> typedef void bar(bar_type)(foo_type,int,long)

Yes, exactly.

> The above can be done, but I do not understand the motivation. You might
> want to consider using lists.


Thanks, Markus

Boost list run by bdawes at, gregod at, cpdaniel at, john at