title: Runtime value to type mapping
body: <p>I've got a list of types which can be send over the network, take this example:</p>
  
  <pre><code>enum types {
      E_T1,
      E_T2,
      E_T3,
      E_T4
  };
  </code></pre>
  
  <p>Now I have a list of classes which correspond to each of the types, let's say each is declared as <code>class E_T1 {...}</code>, <code>class E_T2 {...}</code>, etc.</p>
  
  <p>They are <strong>not derived from a common base class</strong> and it's not possible to do so. Each of the classes has a verification method I need to invoke with the data send over the network. The client sends the data <code>D</code> and a id correspointing to the message type. I need to get hold of the object corresponding to the type. I can use C++0x features if needed.</p>
  
  <p>What I've tried so far is using specialized templates for the <code>types</code>, holding a typedef for the object related to it. This was obviously a stupid idea as templates parameters need to be compile time constant so doing something along <code>getType&lt;data.id()&gt;::type</code> is not possible.</p>
  
  <p>Then I tried using Boost.Variant to get a common returnable type like this (used mpl vector to iterate over the registered types at runntime for debbuging):</p>
  
  <pre><code>template &lt;typename C&gt;
  struct getType() {
      typedef C type;
  }
  
  typedef boost::mpl::vector&lt;
      getType&lt;E_T1&gt;,
      getType&lt;E_T2&gt;,
      getType&lt;E_TX&gt;...
  &gt; _types;
  
  typedef boost::make_variant_over&lt;_types&gt;::type _type;
  
  //use a map to store each type &lt;-&gt; id
  boost::unorderd_map&lt;types, _type&gt; m;
  m[E_T1] = getType&lt;E_T1&gt;();
  
  m[data.id()]::type x; //&lt;- access type, can now call x.validate(data)
  </code></pre>
  
  <p>The problem with this is that it's limited to 20 entries per variant per default. This can be overwritten but from what I understood the overhead per type should be considered and we are talking about a few thousand types here.</p>
  
  <p>Also tried boost.any but it doesn't hold any type information so that's out of the question again. Has anyone any good ideas how this can be solved elegantly?
  Looking for something where I don't have to write a 1k switch statement anytime I handle a type.</p>
  
  <p>All types are nown at compile type, same goes for their corresponding IDs. 
  Id -> Type resolving needs to happen at runtime though.</p>
  
  <p>Thanks in advance,
  Robin.</p>