Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hmm, I don't think I understand your problem correctly, so apologies if this answer is way off the mark, but could you not have a static member of <code>S</code>, which has a static instance of <code>ftype</code>, and return a reference to that, this way, you don't need to explicitly have an instance defined in your implementation files... i.e.</p> <pre><code>template&lt;int plugin_id, class T&gt; struct S { typedef T (*ftype)(T); static ftype&amp; instance() { static ftype _fp = T::create(); return _fp; } }; </code></pre> <p>and instead of accessing <code>S&lt;SOME_PLUGIN,double&gt;::fp</code>, you'd do <code>S&lt;SOME_PLUGIN,double&gt;::instance()</code>. To instantiate, at some point you have to call <code>S&lt;&gt;::instance()</code>. Do you need this to happen automagically as well? </p> <p>EDIT: just noticed that you have a copy constructor, for <code>ftype</code>, changed the above code.. now you have to define a factory method in <code>T</code> called <code>create()</code> to really create the instance.</p> <p>EDIT: Okay, I can't think of a clean way of doing this automatically, i.e. I don't believe there is a way to (at compile time) build a list of types, and then instantiate. However you could do it using a mix... Hopefully the example below will give you some ideas...</p> <pre><code>#include &lt;iostream&gt; #include &lt;typeinfo&gt; #include &lt;boost/fusion/include/vector.hpp&gt; #include &lt;boost/fusion/algorithm.hpp&gt; using namespace std; // This simply calls the static instantiate function struct instantiate { template &lt;typename T&gt; void operator()(T const&amp; x) const { T::instance(); } }; // Shared header, presumably all plugin developers will use this header? template&lt;int plugin_id, class T&gt; struct S { typedef T (*ftype)(T); static ftype&amp; instance() { cout &lt;&lt; "S: " &lt;&lt; typeid(S&lt;plugin_id, T&gt;).name() &lt;&lt; endl; static ftype _fp; // = T::create(); return _fp; } }; // This is an additional struct, each plugin developer will have to implement // one of these... template &lt;int plugin_id&gt; struct S_Types { // All they have to do is add the types that they will support to this vector static void instance() { boost::fusion::vector&lt; S&lt;plugin_id, double&gt;, S&lt;plugin_id, int&gt;, S&lt;plugin_id, char&gt; &gt; supported_types; boost::fusion::for_each(supported_types, instantiate()); } }; // This is a global register, so once a plugin has been developed, // add it to this list. struct S_Register { S_Register() { // Add each plugin here, you'll only have to do this when a new plugin // is created, unfortunately you have to do it manually, can't // think of a way of adding a type at compile time... boost::fusion::vector&lt; S_Types&lt;0&gt;, S_Types&lt;1&gt;, S_Types&lt;2&gt; &gt; plugins; boost::fusion::for_each(plugins, instantiate()); } }; int main(void) { // single instance of the register, defining this here, effectively // triggers calls to instanc() of all the plugins and supported types... S_Register reg; return 0; } </code></pre> <p>Basically uses a fusion vector to define all the possible instances that could exist. It will take a little bit of work from you and the developers, as I've outlined in the code... hopefully it'll give you an idea...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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