|
Boost : |
From: Frank Laub (frank.laub_at_[hidden])
Date: 2005-12-07 06:47:19
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk