Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-09-27 00:24:17


Simon Buchan wrote:

>Jason Hise wrote:
>
>
>>Btw, if you are interested this is what said code looks like in one part
>>of my singleton implementation:
>>
>>// generate create functions taking any number of params that are
>>// forwarded to factory create function
>>#define BOOST_PP_LOCAL_LIMITS (1, \
>> BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS)
>>#define BOOST_PP_LOCAL_MACRO(n) \
>>template < BOOST_PP_ENUM_PARAMS(n, typename P) > \
>>void create ( BOOST_PP_ENUM_BINARY_PARAMS(n, const P, & p) ) \
>>{ \
>> if ( ptr ) return; \
>> ptr = factory.create ( BOOST_PP_ENUM_PARAMS(n, p) ); \
>>}
>>#include BOOST_PP_LOCAL_ITERATE()
>>
>>
>>
>Wow... Even after reading the docs I can't understand that :D
>I assume it expands to:
>template < P1, P2, .. Pn >
>void create ( const P1& p1, const P2& p2, ... const Pn& pn )
>{
> if ( ptr ) return;
> ptr = factory.create ( p1, p2, ... pn );
>}
>right? What about nullary c'tors? Is there any way to do O(n^2)
>declarations like T const& and T& overloads for all parameters?
>
>
Assuming BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS has a value of 8,
this small block actually expands to:

template < typename P1 >
void create ( const P1 & p1 )
{
     if ( ptr ) return;
     ptr = factory.create ( p1 );
}

template < typename P1, typename P2 >
void create ( const P1 & p1, const P2 & p2 )
{
     if ( ptr ) return;
     ptr = factory.create ( p1, p2 );
}

/*...*/

template < typename P1, typename P2, /*...*/ typename P8 >
void create ( const P1 & p1, const P2 & p2, /*...*/ const P8 & p8 )
{
     if ( ptr ) return;
     ptr = factory.create ( p1, p2, /*...*/ p8 );
}

and I do provide the untemplated overload as well, separately:

void create ( )
{
     if ( ptr ) return;
     ptr = factory.create ( );
}

I am actually doing even more fancy stuff, in that I have additional
overloads which take a 'dynamic_tag_type < Type >' as a first parameter,
which tells the factory to call Type's constructor instead of
SingletonType's constructor, and pass the remaining parameters to its
constructor. This gives client code the ability to create the singleton
as a derived type.

If you really want to see the implementation of that craziness the first
beta of the singleton_ptr library should be available very soon. Oh,
and kudos to Pavel, since he clued me in to using BOOST_PP for this in
the first place. :)

-Jason


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