Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-09-01 19:20:54


Marco Costalba wrote:
> This is a simple yet interesting object factory.

Please see

     http://tinyurl.com/35vlvb

. It seems with that factory utility one could achieve the semantics of
your proposal somehow like this (note: untested code for illustration
only - see docs and tests):

    // create a map of functions
    map< string, function<base_t*(arg1_t,arg2_t)> > f;

    // register factories
    f[ "T1" ] = factory<T1>();
    // note that this one works too:
    f[ "T2" ] = bind( factory<T2>(), another_arg, _1, _2);

    // invoke them
    base_t* new_t1 = f["T1"](arg1,arg2);
    base_t* new_t2 = f["T2"](arg1,arg2);

It's pretty flexible, isn't it? It also works with smart pointers and
there's a variant for by-value construction :-).

Regards,
Tobias


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