
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

On 12/07/2005 05:47 AM, Frank Laub wrote:
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
I'm interested.
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?
The boost file vault: http://boost-consulting.com/vault/ under the 'Preprocessor Metaprogramming' directory.
#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; }
[snip] Tha above looks great, and I remember, several weeks or months ago, there was a request for just such a utility on comp.lang.c++.moderated. Thanks.

*OK, here's my first pass of the code: http://tinyurl.com/bnorw* On 12/7/05, Larry Evans <cppljevans@cox-internet.com> wrote:
On 12/07/2005 05:47 AM, Frank Laub wrote:
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
I'm interested.
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?
The boost file vault:
http://boost-consulting.com/vault/
under the 'Preprocessor Metaprogramming' directory.
#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; }
[snip] Tha above looks great, and I remember, several weeks or months ago, there was a request for just such a utility on comp.lang.c++.moderated.
Thanks.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On 12/07/2005 07:27 AM, Larry Evans wrote: [snip]
Tha above looks great, and I remember, several weeks or months ago, there was a request for just such a utility on comp.lang.c++.moderated. It's at:
http://groups.google.com/group/comp.lang.c++.moderated/msg/9afe2aa4eca21c1a

[snip]
Tha above looks great, and I remember, several weeks or months ago, there was a request for just such a utility on comp.lang.c++.moderated. It's at:
Oh cool, I think that discussion is more torwards the BOOST_STRINGTABLE idea which is very similar to the BOOST_ENUM. Thanks for the link! -Frank

Frank Laub wrote:
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 certainly always interested in stuff that ca minimuize all kind of redundancy in programs. As a related issue, it might be good to have the a similar struture which you can use to map from the enum value to the string value and vice versa. This runtime mapping should not require any heap-allocations . -Thorsten

As a related issue, it might be good to have the a similar struture which you can use to map from the enum value to the string value and vice versa. This runtime mapping should not require any heap-allocations .
-Thorsten
Definately on the same page here. This was my next feature I wanted to add. Everything works fine so far until the user specifies a value for an element. Then the parse() feature blows up because it was expecting the values to be sequential from 0. This is where I want to go with BOOST_ENUM_VALUES(). It would require an extra column of data where the user would put their value portion. Then I could construct a map just as you are describing. -Frank

Frank Laub wrote:
As a related issue, it might be good to have the a similar struture which you can use to map from the enum value to the string value and vice versa. This runtime mapping should not require any heap-allocations .
values to be sequential from 0. This is where I want to go with BOOST_ENUM_VALUES(). It would require an extra column of data where the user would put their value portion. Then I could construct a map just as you are describing.
You kinda already have the values because the enumeration starts with value 0 and then increments each new item. Of course it would be nice/necessary to be able to specify the value if the defaults are not what one wants. -Thorsten

Frank Laub wrote:
As a related issue, it might be good to have the a similar struture which you can use to map from the enum value to the string value and vice versa. This runtime mapping should not require any heap-allocations .
values to be sequential from 0. This is where I want to go with BOOST_ENUM_VALUES(). It would require an extra column of data where the user would put their value portion. Then I could construct a map just as you are describing.
You kinda already have the values because the enumeration starts with value 0 and then increments each new item. Of course it would be nice/necessary to be able to specify the value if the defaults are not what one wants.
Ya, that's the trick I'm looking for, I want users to be able to do: enum Foo { a = 0x10, b = 0x20 }; In this case I need a map because I can't assume that the value they want me to parse is an index in the enum. That's why I'm thinking I could add another column of sequences to provide this information.
participants (3)
-
Frank Laub
-
Larry Evans
-
Thorsten Ottosen