title: C++ Cache Design Advice
body: <p>I have a c++ application with several image types(RGB, Gray...) and every type has properties like Rotation or Scale. Every image type is generated via some computation from the other types. For example, A rotated <code>GrayImage</code> is generated by rotating a <code>GrayImage</code> which in turn is generated by "graying" an <code>RGBImage</code>.</p>
  
  <p>I would like to design a cache class with methods <code>GetX(...)</code> that caches the various images (and possibly all images in the computation path).
  This class would also know how to generate each image in case it is not in the cache.</p>
  
  <p>The class must fulfill some constraints:</p>
  
  <ol>
  <li><p>Since we are dealing with images of different types and representations(RGB, GrayScale etc.) the cache must return a concrete class for the calling code to be able to use it without some sort of casting. Thus, the cache mechanism must hold different cache structures containing concrete types. (fix me if I'm wrong here)</p>
  
  <pre><code>map&lt;...,RGBImage&gt; 
  map&lt;...,GrayImage&gt; 
  </code></pre>
  
  <p>for example. </p></li>
  <li><p>The cache must be flexible to changes in computation of images. Changes to code are acceptable as long as they are not too large.</p></li>
  </ol>
  
  <p>The current version I have attaches a <code>Key</code> struct to each image type.
  There's <code>GrayKey</code>, <code>RGBKey</code> and so on. the various keys hold properties like Scale and Rotation and can have properties specific to the image (e.g. toGrayConvertingMethod for <code>GrayKey</code>).
  The cache holds maps of the form: </p>
  
  <pre><code>    map &lt;XKey,XImage&gt;
  </code></pre>
  
  <p><code>GetX(...)</code> method receives a <code>Key</code> struct as parameter requesting a Rotated GrayImage for example.
  Though, this implementation forces the cache to apply lots of logic for computations of images. It must check whether GrayKey requests a rotated image or not and act accordingly.
  I would like to "encode" this image computation relationship in a more elegant way but can't seem to find one.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks a lot.</p>