I used something like the following to write the policy for my application:
The policy classes are as follows:
struct Policy { static void init(); static void cleanup();
To create policies:
typedef Cons<Policy1, Cons<Policy2, Cons<Policy3, Policy4> > > MyPolicy;
To use MyPolicy:
init_with<MyPolicy>(...); //... cleanup_with<MyPolicy>(...);
where will they call:
MyPolicy::init_options(); // calls Policy1 to 4 init in order
and
MyPolicy::cleanup(); // calls Policy1 to 4 cleanup in reverse order
Essentially, Cons creates a list of types here. This is pretty straight forward. However, the typedef string cons looks ugly. This will be ideal if you have a policy collector that can do this:
typedef CombinePolicy<Policy1, Policy2, Policy3, Policy4> MyPolicy;
Since we can have an arbitrary number of policies, CombinePolicy needs to support a variational template in C ++ 0x, which is only available experimentally in advanced compilers. However, it seems that boost: the mpl library solved / worked on the problem using bundle preprocessing tricks. I think I could use something like:
typedef mpl::list<Policy, Policy2, Policy3, Policy4> Policies;
and then calls:
init_with<Policies>(...);
which would then use:
typedef iter_fold<Policies, begin<Policies>::type, some_magic_lambda_expression>::type MyPolicy;
Obviously, I have a little problem with the output of some_magic_lambda_expression. I am sure this is pretty trivial for mpl experts.
Thanks in advance.
c ++ templates metaprogramming boost-mpl
ididak
source share