Boost logo

Boost Users :

From: dariomt_at_[hidden]
Date: 2008-01-03 13:03:43


Hello boost users,

I need some advice in using MPL and fusion.

Lets take the following case for a typical Factory pattern (full compilable
source code is attached):

class Base {...};
class Factory {...};
template <typename T> struct Helper {...}; // constructor registers creation
function for template parameter

Now we have a template defining a family of derived classes:
template <typename U, typename V, typename W> class DerivedUVW : public Base
{...};

And a helper registration macro:
#define HELPER_DERIVED_UVW(U, V, W) \
    Helper<DerivedUVW<U, V, W> > BOOST_PP_CAT(h, __LINE__) (#U #V #W);

And all possible U V and W classes in the combination game:
struct A1 {}; struct A2 {}; // for U
struct B1 {}; struct B2 {}; // for V
struct C1 {}; struct C2 {}; // for W

Now with manual registration its just a matter of patience:
// Manual registration:
// must type all possible combinations -> ugly
HELPER_DERIVED_UVW(A1, B1, C1);
HELPER_DERIVED_UVW(A1, B1, C2);
// etc...

To create the desired object just use
Factory::instance().create("A1B1C1")->doInterestingStuff()

And now the six million dollar question:
Is there a way in which MPL and fusion can automatically do the
registration?

Here registration means the instantiation of template Helper with all
possible combinations of DerivedUVW with elements from lists like:
typedef boost::mpl::vector<A1,A2> Ulist;
typedef boost::mpl::vector<B1,B2> Vlist;
typedef boost::mpl::vector<C1,C2> Wlist;
and registered with an appropiate name like "A1B1C1"

Of course the objective is to easily add A3, A4, B3, C4, etc... to allow
more combinations, without having to manually update all the registration
code, ideally just the typelists. And, let's not forget variants DerivedUV
or DerivedUVWZ with a different number of template parameters.

I'm trying to think how I would do combinations for values in runtime and
translate it to the MPL domain. Still no success.

Any help, suggestions or comments will be appreciated.

PS: the context of this problem is being able to expose a heavily
templatized library to a user selecting the combination of template classes
at runtime with a text string. I'm sure there's a suitable solution to this
problem somewhere out there, or at least some hints to build my own
solution...





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