I have used something like the following to compose policies for my application:
The policy classes look like this:
struct Policy {
static void init();
static void cleanup();
//...
};
template <class CarT, class CdrT>
struct Cons {
static void init() {
CarT::init();
CdrT::init();
}
static void cleanup() {
CdrT::cleanup();
CarT::cleanup();
}
//...
};
To compose policies:
typedef Cons<Policy1, Cons<Policy2, Cons<Policy3, Policy4> > > MyPolicy;
To use MyPolicy:
init_with<MyPolicy>(...);
//...
cleanup_with<MyPolicy>(...);
where they'd call:
MyPolicy::init_options(); // calls Policy1 to 4's init in order
and
MyPolicy::cleanup(); // calls Policy1 to 4's cleanup in reverse order
Essentially, Cons constructs a type list here. It's pretty straight forward. However the typedef cons line is kinda ugly. It'll be ideal to have policy combiner that can do this:
typedef CombinePolicy<Policy1, Policy2, Policy3, Policy4> MyPolicy;
Since we can have arbitrary number of policies, the CombinePolicy would need variadic template support in C++0x, which is only available experimentally in cutting edge compilers. However, it seems that boost:mpl library solved/worked around the problem by using a bunch preprocessing tricks. I guess 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 trouble figuring out *some_magic_lambda_expression* here. I'm sure it's quite trivial for mpl experts here.
Thanks in advance.