Boost logo

Boost :

From: Johan Nilsson (johan.nilsson_at_[hidden])
Date: 2002-12-02 04:35:03


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;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk