Boost logo

Boost :

From: Johan Nilsson (johan.nilsson_at_[hidden])
Date: 2002-12-02 05:21:15


Sorry - the expression 'compile-time' below is (of course) a misnomer as the
table is implemented as a static array declared inside a static function.
What I meant to say is that you declare the contents of the table together
with the table definition, and that its contents are then fixed at run-time.

// Johan

"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