Boost logo

Boost :

Subject: Re: [boost] Is there interest in a library for string-convertible enums?
From: Felix Uhl (felix.uhl_at_[hidden])
Date: 2014-10-28 19:18:33


On 28/10/2014, Rob Stewart wrote:


>That sounds interesting. Ease of use and compilation overhead are concerns, of course.



I’ll answer your ease of use concern with example code for definition

of the enum class Zott:


>BOOST_ADVANCED_ENUM_DEFINE( Zott,

> zero,

> one,

> two,

> three

>);


And now for the mentioned “flag” values:


>BOOST_ADVANCED_ENUM_DEFINE_W_SUPPLY(

> TwoToThePowerOf,

> ::boost::advanced_enum::supplies:shiftL1::values,

> zero,

> one,

> two,

> three

>);


Seems easy enough. The second one could be shortened to another macro,

of course. Arbitrary values have a bit strange syntax, though:


>BOOST_ADVANCED_ENUM_DEFINE_W_SUPPLY(

> BOOST_ADCANCED_ENUM_ARBITRARY(4, 8, 6, 20),

> four,

> eight,

> six,

> twenty

>);


I’ll see what I can do about that.


Conversions to and from string are done via static_cast (not sure

if that’s the ideal solution), in- and output work naturally like this:


>Zott myZott = Zott:one;

>std::cin >> myZott; //if input is not convertible, no changes are made

>std::cout << myZott;


I think that's as easy as it can get :)
Compilation overhead indeed needs to be considered.


>What about enum classes?



My mistake, with “named enums” I meant “enum classes”, but now that

you say it, I should try to implement string convertible

regular enums as well.


Thank you for your feedback!

---

Felix Uhl





Von: Rob Stewart
Gesendet: ‎Dienstag‎, ‎28‎. ‎Oktober‎ ‎2014 ‎22‎:‎23
An: boost_at_[hidden]





On October 28, 2014 7:09:35 AM EDT, Felix Uhl <felix.uhl_at_[hidden]> wrote:
>So far, the only way to convert an enumeration value to a string is a
>lot of boilerplate code consisting of switch case statements for every
>single possible value of the enum.

As Niall noted, most of us have done that before.

>I’ve written a small header-only library that uses Boost.Preprocessor
>and templates to ease the definition of named enumerations that are
>convertible to and from strings.

That sounds interesting. Ease of use and compilation overhead are concerns, of course.

>Currently, I am documenting the first version of the library to make it
>available on the Boost Library Incubator website,

I can hear Robert yelling, "Yes!"

> but I wanted to get
>some initial feedback from the developers mailing list, too. I would be
>glad to extend the library to a general extension of enumeration
>behaviour, like changing the generation of underlying values from a
>continuous increment to a continuous shift left by one, effectively
>making the underlying values binary flags.

That would certainly be useful.

What about enum classes?

___
Rob

(Sent from my portable computation engine)

_______________________________________________
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