title: c++ automatic factory registration of derived types
body: <p>Like many before me, I'm trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn't find there.</p>
  
  <p>I've got everything running nicely except the automatic registration.</p>
  
  <p><strong>My Goals:</strong></p>
  
  <ol>
  <li>automatically register any derived class of my base class <strong>Base</strong>
  
  <ol>
  <li>only classes I mark as <em>registrable</em></li>
  <li>not only direct sub-classes of <strong>Base</strong> 
  
  <ul>
  <li><strong>ex:</strong> Base -> Device -> Camera -> <strong>Webcam</strong></li>
  <li>this would make using the <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="noreferrer">CRTP</a> like described in <a href="https://stackoverflow.com/questions/6399804/automatic-static-invocation-of-derived-types">this question</a> dificult</li>
  </ul></li>
  </ol></li>
  <li>minimal changes to the classes I want registered - <em>dummies proof</em></li>
  <li>would prefer using a <strong>registrator class</strong> than macros
  
  <ul>
  <li>like in <a href="https://stackoverflow.com/questions/6399804/automatic-static-invocation-of-derived-types">this question</a>, but I'm not sure if this is dependent on <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="noreferrer">CRTP</a></li>
  </ul></li>
  </ol>
  
  <p><strong>What I have:</strong></p>
  
  <pre><code>template &lt;class T&gt;
  class abstract_factory
  {
      public:
          template &lt; typename Tsub &gt; static void register_class();
          static T* create( const std::string&amp; name );
      private:
          // allocator&lt;T&gt; is a helper class to create a pointer of correct type
          static std::map&lt;std::string, boost::shared_ptr&lt;allocator&lt;T&gt; &gt; &gt; s_map;
  };
  </code></pre>
  
  <ul>
  <li>templated abstract factory, with <strong>std::string</strong> as <em>key type</em></li>
  <li>abstract factory has all members and methods <strong>static</strong></li>
  <li>class name is recovered automatically with <strong>typeid</strong> (no need for text name when registering)</li>
  <li>registration by calling: <code>abstract_factory&lt;Base&gt;::register_class&lt;MyDerived&gt;();</code></li>
  </ul>
  
  <p><strong>What I tried (or would like to but don't know how to properly):</strong></p>
  
  <ul>
  <li><code>registrator&lt;Derived&gt; class</code>: templateded class that is instantiated statically in <code>Derived.cpp</code> and should call <code>abstract_factory::register_class&lt;Derived&gt;()</code> in it's constructor
  
  <ul>
  <li>never gets called or instantiated</li>
  <li>if I make an instance of <code>Derived</code> in <code>main()</code> this works -> kinda defeats the purpose though </li>
  </ul></li>
  <li>declare a simple static variable in each <code>Derived.hpp</code> and set it with the static registration method in <code>Derived.cpp</code> -> again, never gets called.</li>
  <li>make <code>abstract_factory</code> a true singleton instead of having everything static?</li>
  </ul>
  
  <p>Could use any advice, large or small, thanx.</p>