Hi,

I've been implementing some kind of compile-time initialized lookup table,
but it suddenly struck me: are there already similar functionality available
in boost? See attached sample code below.

// Johan

-------------------
#incude <cassert>
#include <stdexcept>
#include <string>
#include "StaticLookupTable.h"

enum Colors
{
  Red = 1,
  Blue = 2
};

struct ColorTable
  : public StaticLookupTable<ColorTable, Colors, std::string>
{
  LOOKUP_TABLE_BEGIN()
  LOOKUP_ENTRY(Red, "Red")
  LOOKUP_ENTRY(Blue, "Blue")
  LOOKUP_TABLE_END()
};

int main()
{
  assert("Red" == ColorTable::Lookup(Red));
  assert(Blue == ColorTable::ReverseLookup("Blue"));

  try
  {
    ColorTable::Lookup(static_cast<Colors>(7));
    assert(!"Lookup of invalid color succeeded");
  }
  catch(std::range_error&)
  {
  }

  return 0;
}





_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to