Boost logo

Boost :

Subject: Re: [boost] Is there interest in a library for string-convertible enums?
From: Jeffrey Bush (jeff_at_[hidden])
Date: 2014-10-28 22:16:39


>
> 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.
>
>
> 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. Those can also be used with the std::cin and std::cout
> streams naturally, while maintaining all syntactic and semantic properties,
> regarding initialisation, assignment, type safety and conversion to
> underlying types.
>
>
> Currently, I am documenting the first version of the library to make it
> available on the Boost Library Incubator website, 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.
>
>
> What features would you want from such a library? Is there even a chance
> that such a library would get included in boost (given that it satisfied
> the quality requirements), or is there too little interest in this
> functionality?
>

I have also offered a class for doing this as well, although it didn't draw
the interest that this one did.
Here was my attempt at it with a test/example class:
https://gist.github.com/coderforlife/6d8bac451d49dd1a0c81

Supports value to string and string to value, including multiple values
with | between them, meaning all combinations can be round-tripped.
Supports default power-of-two values and custom values, listing all valid
values (as enum types, but each is easily convertible to integral and
string values),

Although you cannot specify custom string names for an enum value that is
not the "STRINGIZE" name, however you can choose to have multiple enum
entries with the same value and different names.

Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk