[Boost.MPL and Boost.Fusion] Combinatorial game with typelists
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
I personally solved this with BOOST.Preprocessor to generate the many
combinations, but I think you could make an MPL/Fusion solution.
Chris
On Jan 3, 2008 1:03 PM,
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
class DerivedUVW : public Base {...}; And a helper registration macro: #define HELPER_DERIVED_UVW(U, V, W) \ Helper
> 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
Ulist; typedef boost::mpl::vector Vlist; typedef boost::mpl::vector 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
dariomt@gmail.com wrote:
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
class DerivedUVW : public Base {...};
...
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
...
And now the six million dollar question: Is there a way in which MPL and fusion can automatically do the registration?
I think so (see attachment)!
Here's an MPL-based solution. It does not require any preprocessor code
and compiles with GCC4.
Just for the fun of it, I reduced the Factory code to a one-liner using
the recently reviewed factory functional, which can be downloaded here:
http://tinyurl.com/35vlvb
Regards,
Tobias
#include <iostream>
#include <sstream>
#include <string>
#include <memory>
#include <map>
#include
Tobias Schwinger wrote:
dariomt@gmail.com wrote:
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
class DerivedUVW : public Base {...}; ...
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
...
And now the six million dollar question: Is there a way in which MPL and fusion can automatically do the registration?
I think so (see attachment)!
Here's an MPL-based solution. It does not require any preprocessor code and compiles with GCC4.
With a bug. It successfully determines whether there is nothing to do -
but also does nothing if there's almost nothing to do, that is only one
combination. It also won't work if one of the input sequences is empty.
The attached patches remove the entry condition. So the precondition is
"the sequences yield at least one combination" now. Checking it is left
an exercise to the reader :).
Regards,
Tobias
--- combinations.cpp.bak 2008-01-05 10:55:37.000000000 +0100
+++ combinations.cpp 2008-01-05 10:56:04.000000000 +0100
@@ -206,12 +206,7 @@
inline void register_factories()
{
typedef typename mpl::transform
participants (3)
-
Chris Weed
-
dariomt@gmail.com
-
Tobias Schwinger