Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use boost::mpl to compose policies?
    text
    copied!<p>I have used something like the following to compose policies for my application:</p> <p>The policy classes look like this:</p> <pre><code>struct Policy { static void init(); static void cleanup(); //... }; template &lt;class CarT, class CdrT&gt; struct Cons { static void init() { CarT::init(); CdrT::init(); } static void cleanup() { CdrT::cleanup(); CarT::cleanup(); } //... }; </code></pre> <p>To compose policies:</p> <pre><code>typedef Cons&lt;Policy1, Cons&lt;Policy2, Cons&lt;Policy3, Policy4&gt; &gt; &gt; MyPolicy; </code></pre> <p>To use MyPolicy:</p> <pre><code>init_with&lt;MyPolicy&gt;(...); //... cleanup_with&lt;MyPolicy&gt;(...); </code></pre> <p>where they'd call:</p> <pre><code>MyPolicy::init_options(); // calls Policy1 to 4's init in order </code></pre> <p>and</p> <pre><code>MyPolicy::cleanup(); // calls Policy1 to 4's cleanup in reverse order </code></pre> <p>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:</p> <pre><code>typedef CombinePolicy&lt;Policy1, Policy2, Policy3, Policy4&gt; MyPolicy; </code></pre> <p>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 <em>guess</em> I could use something like:</p> <pre><code>typedef mpl::list&lt;Policy, Policy2, Policy3, Policy4&gt; Policies; </code></pre> <p>and then calls:</p> <pre><code>init_with&lt;Policies&gt;(...); </code></pre> <p>which would then use:</p> <pre><code>typedef iter_fold&lt;Policies, begin&lt;Policies&gt;::type, some_magic_lambda_expression&gt;::type MyPolicy; </code></pre> <p>Obviously, I have a little trouble figuring out <em>some_magic_lambda_expression</em> here. I'm sure it's quite trivial for mpl experts here.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload