title: Object lookup with optional help of a lookup table which must be optimized away if unused
body: <p>I want to speed up glyph lookup in my Cortex-M4 application for those fonts that are heavily used, by adding an optional lookup table. What I have now is - simplified and pseudo-ish - this:</p>
  
  <p>in fontname-font.cpp:</p>
  
  <pre><code>static const uint8_t someFont = {
    headerbytes[knownHeaderSize],
    glyph[knownGlyphCount]
  };
  </code></pre>
  
  <p>The size of each glyph depends on the glyph. Some are basically empty (space, for example), some are larger (like 'M'). The size of each glyph is stored in the glyph's header, so I can loop through the font and find out at which offset (<code>someFont[offset]</code>) a glyph with code <code>code</code> starts.</p>
  
  <p>in fontname-map.cpp:</p>
  
  <pre><code>static const uint16_t someFont_lut[knownGlyphCount];
  </code></pre>
  
  <p>so I can use <code>someFont[someFont_lut[code]]</code> if the LUT is included.</p>
  
  <p>How can I combine the font and an optional LUT in a class that provides a convenient interface? It seems to me that splitting the information into two files is already a bad idea (I can change that, though), but even if they were in the same file, how can I design a class that allows optional usage of a LUT, and allows the LUT to be optimized away if it is not used?</p>
  
  <p>I'd like reduce the application interface to something like</p>
  
  <pre><code>Glyph Font::operator[](const char&amp; c);
  </code></pre>
  
  <p>where <code>Glyph</code> can be just a pointer to the glyph stored in flash or a proxy object that can be used to retrieve glyph data from flash. Existence and usage of the LUT should be hidden behind this operator.</p>
  
  <p>I'm using gcc, and suggestions may be gcc-specific. Which fonts should contain a LUT is known at compile-time.</p>
  
  <hr>
  
  <p><strong>Added:</strong> I'd like the solution to prevent me from mixing one font's glyphs with another font's LUT. I want to specify <em>if</em> the LUT is to used, not <em>which</em> that is. The font reading class must find it on its own. </p>