Boost logo

Boost :

From: Job Mulder (Job_at_[hidden])
Date: 2005-12-07 08:49:35


It looks nice, but does it also work with other structures than strings?
i.e.

BOOST_ENUM(id,
        (ab_device)("device for your abs", /*activation code*/ 13,
/*typical % of time used*/ 0.0) (pc_device)("personal computer",
/*activation code*/ 42, /*typical % of time used*/ 100.0)
)

-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Frank Laub
Sent: Wednesday, December 07, 2005 12:47
To: boost_at_[hidden]
Subject: [boost] BOOST_ENUM proposal

Hello fellow boosters,

I've recently come up with a way to use the boost preprocesor to
generate a 'better' enum type. The following is an example of some
typical usage. Is anyone else interested in this sort of thing? I'm
hoping to get suggestions for how to make it more 'boost-like' in both
its implementation and its usage. Is this very readable? Is there a
place I can upload the code for people to peruse?

#include <iostream>
#include <boost/enum.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

BOOST_ENUM(Level,
    (Abort)("unrecoverable problem")
    (Error)("recoverable problem")
    (Alert)("unexpected behavior")
    (Info)("expected behavior")
    (Trace)("normal flow of execution")
    (Debug)("detailed object state listings")
)

int main()
{
    foreach(const string& name, Level::names)
    {
        cout << name << endl;
    }
    cout << "1: " << Level::names[1] << endl;
    cout << "toString(Abort): " << Level::toString(Level::Abort) <<
endl;
    cout << "getValue(Abort): " << Level::getValue(Level::Abort) <<
endl;

    Level::type value = Level::Invalid;
    bool ret = Level::fromString("Abort", value);
    cout << "fromString(\"Abort\"): " << ret << ", " << value << endl;

    value = Level::Invalid;
    ret = Level::fromString("Debug", value);
    cout << "fromString(\"Debug\"): " << ret << ", " << value << endl;

    value = Level::Invalid;
    ret = Level::fromString("not in enum", value);
    cout << "fromString(\"not in enum\"): " << ret << ", " << value <<
endl;

    return 0;
}

With the program output being:

Abort
Error
Alert
Info
Trace
Debug
Invalid
1: Error
toString(Abort): Abort
getValue(Abort): unrecoverable problem
fromString("Abort"): 1, 0
fromString("Debug"): 1, 5
fromString("not in enum"): 0, 6

-Frank
_______________________________________________
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