Boost logo

Boost :

From: James Curran (jamescurran_at_[hidden])
Date: 2002-12-04 09:28:49


    I've just written something similar (mostly as an experiment, and not up
to Boost standards yet). It uses a code-generation step, to take a word
list file, and produces an enum .H file, a FSM lookup table (.CPP) and a
word array (.CPP).

If there's interest, I'll clean it up and post it for further review.

Use would be like this:

// machine generated
// defines enum
#include "symbols.h"

// defined is separate .CPP, machine generated
// string-> enum
extern short WordListTable[][64];

// defined is separate .CPP, machine generated
// enum -> string
extern const char* WordList[];

StaticLookupTable ColorTable(WordListTable); // ctor just stores the
pointer.
 int main()
 {

    // Simple array index.
    assert("Red" == WordList[Red]);

    // Lookup < O(n) where n is characters in string
      assert(Blue == ColorTable.Lookup("Blue"));

    // probably possible if I use boost::array instead of
    // a native array for WordList[]. std::vector<> isn't
    // really an option, as I am trying to avoid anything
    // with a non-trivial ctor.
> try
> {
> ColorTable::Lookup(static_cast<Colors>(7));
> assert(!"Lookup of invalid color succeeded");
> }
> catch(std::range_error&)
> {
> }
>

"Johan Nilsson" <johan.nilsson_at_[hidden]> wrote in message
news:asf9bt$7g6$1_at_main.gmane.org...
> 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
>


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