Is there interest in a library for string-convertible enums?
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?
On 28 Oct 2014 at 11:09, Felix Uhl wrote:
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?
It's something we've all rolled our own before. Kinda like Boost.TypeIndex. As it turned out, TypeIndex grew into something quite special after a few rounds of review. I should think maybe your library might do the same. I look forward to seeing what you come up with. Niall -- ned Productions Limited Consulting http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/
Felix Uhl
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.
Sounds great!
What features would you want from such a library?
Aside from the conversion to strings, only those that enum class offers: being able to specify the underlying type, not being implicitly convertible to any other type, and being able to forward-declare them.
On October 28, 2014 7:09:35 AM EDT, Felix Uhl
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)
Hi Felix, Personally I'm interested, because so far I've been always using a silly macro for this, which doesn't make much :
#ifndef SWISSARMYKNIFE_ENUMS_ADAPT_ENUM_HPP #define SWISSARMYKNIFE_ENUMS_ADAPT_ENUM_HPP
#include <string> #include <ostream> #include
#include #include #define SWISSARMYKNIFE_ADAPT_ENUM_EACH_ENUMERATION_ENTRY_C( \ R, unused, ENUMERATION_ENTRY) \ case ENUMERATION_ENTRY: \ return BOOST_PP_STRINGIZE(ENUMERATION_ENTRY); \ break;
#define SWISSARMYKNIFE_ADAPT_ENUM(ENUM_TYPE, ENUMERATION_SEQ) \ inline std::string to_string(const ENUM_TYPE& enum_value) { \ switch (enum_value) { \ BOOST_PP_SEQ_FOR_EACH( \ SWISSARMYKNIFE_ADAPT_ENUM_EACH_ENUMERATION_ENTRY_C, \ unused, ENUMERATION_SEQ) \ default: \ return BOOST_PP_STRINGIZE(ENUM_TYPE); \ } \ } \ \ inline std::ostream& operator<<(std::ostream& os, const ENUM_TYPE& value) { \ os << to_string(value); \ return os; \ }
#endif
What I would like to is to have a complete adapted enum for Boost.Fusion, this was implemented at some point in some old version of Boost.Fusion but I cannot find it anymore. Because if this had the extension struct_member_name or some other better named extension, then I could print out all the possible states with some generic code like I print struct member name with "boost::fusion::for_each_member(enumValue, *this);"
#ifndef BOOST_FUSION_FOR_EACH_MEMBER_HPP #define BOOST_FUSION_FOR_EACH_MEMBER_HPP
#include <functional>
#include
#include
#include #include #include #include #include #include #include #include namespace boost { namespace fusion {
namespace detail {
template
inline void for_each_member_linear(First const& first, Last const& last, F const& f, boost::mpl::true_) {} template
inline void for_each_member_linear(First const& first, Last const& last, F const& f, boost::mpl::false_) { f( extension::struct_member_name< typename First::seq_type, First::index::value >::call(), *first );
for_each_member_linear( next(first), last, f, result_of::equal_to< typename result_of::next<First>::type, Last>() ); }
template
inline void for_each_member(Sequence& seq, F const& f) { detail::for_each_member_linear( fusion::begin(seq), fusion::end(seq), f, result_of::equal_to< typename result_of::begin<Sequence>::type, typename result_of::end<Sequence>::type>() ); }
}
template
inline void for_each_member(Sequence& seq, F f) { detail::for_each_member(seq, f); } }}
#endif
And before all it's important I believe that it doesn't ask the user to write the enums with macros, but that an additional call later to something like BOOST_ADAPT_ENUM adds the boilerplate code. It's naturally possible to offer both choice to the user like BOOST_FUSION_ADAPT_STRUCT / BOOST_FUSION_DEFINE_STRUCT. Cheers, -- Damien Buhl On 10/28/2014 12:09 PM, Felix Uhl 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.
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?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
On 29/10/2014 00:09, Felix Uhl 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.
Not the only way. The method I use is to create a lookup table via a little macro magic. Granted that I'm then doing a linear scan through the table (whereas a switch statement could be more efficient, or knowledge that the enum is contiguous and could therefore do an index lookup), but having a table allows for other useful queries, and I wanted something that could be defined statically. As a side-benefit (that undoubtedly will horrify some people), the way the code was implemented also allows it to act as a string-to-member-pointer lookup table, which can be quite handy for typed property bags and the like with a string lookup key.
What features would you want from such a library?
Features that I would expect of such a library: - support for arbitrary non-contiguous values. - enum value to string. - string to enum value. - list of all valid values, either as strings or values. - can indicate whether a given value (string or enum) is a member of the enumeration. - can round-trip (enum -> string -> enum) values that are not members of the enumeration; of course (string -> enum -> string) is not feasible for non-members. - can specify a custom string value that the enum value generates (not just the BOOST_PP_STRINGIZE). Features that would be nice, but not necessarily expected: - enum to string and back for "bitwise flag" style enums (eg. CSV names). - some mapping to std::bitset or similar. - can specify multiple string values that map to the same enum value. - can specify different conversion tables for the same enum type (for use in different contexts, eg. serialisation vs. user output).
Hi Felix, Are you aware of the earlier effort by Frank Laub? Once there was a boost.enum library (v4.6) in the boost Sandbox, but I think it can no longer be found (I contacted the author in 2011 and not even he could find it then!). It is also based on Boost.Preprocessor to generate most of the code. Obviously, it never made it into boost. I am currently using a version of this library that I adapted quite significantly to make it meet my own needs. I would be curious how your library would compare to this previous effort and I'd be happy to send you more information if you don't have it. Pieter -----Original Message----- From: Felix Uhl [mailto:felix.uhl@outlook.de] Sent: Tuesday, October 28, 2014 12:10 PM To: boost@lists.boost.org Subject: [boost] Is there interest in a library for string-convertible enums? 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?
Le 28/10/14 12:09, Felix Uhl a écrit :
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?
Hi, inn case you are interested http://htmlpreview.github.io/?https://github.com/viboes/enums/blob/master/li... Best, Vicente
participants (8)
-
Damien Buhl
-
Felix Uhl
-
Gavin Lambert
-
Marcel Raad
-
Niall Douglas
-
Pieter
-
Rob Stewart
-
Vicente J. Botet Escriba