
Hi Boost! I have developed a type-safe bit-wise flags class for my own personal use and am wondering if it would be of interest for me to submit it as a Boost library. At the moment it is not ready for submission (code is messy) but I am testing the waters so I present an overview below. * Fully ISO standard C++11 (except for an MSVC version which is API-compatible except it doesn't support as many an infinite number of flag names) * They are type-safe, requiring explicit casts to and from integers/booleans * Supports standard bit-wise operations, and always keep values within the possible set of flags * Supports getting the name of a flag and looking up flags by name at run-time (when a flag is actually multiple-flags it gives something like "A|B") * Supports getting an array of all flags at run-time Now the bad things, which may be a deal-breaker: * Because of limitations in C++ the syntax can feel a bit awkward. You have to use a macro to define flag-names (which actually creates a class) then you can pass those flag-names as template arguments to the flags. An example: FLAG_NAME(A); FLAG_NAME(B); FLAG_NAME(C); typedef Flags<std::uint8_t, Flag_A, Flag_B, Flag_C>::WithValues<0x01, 0x02, 0x04> MyFlags; * I also couldn't get around using the ::WithValues nested class * I use a set of macros to make this cleaner (e.g. the above could be FLAGS(A, B, C)) and once defined there is no more ugliness Thanks for consideration! Jeff

Is the code available online? On Tue, Jun 17, 2014 at 6:09 PM, Jeffrey Bush <jeff@coderforlife.com> wrote:
Hi Boost!
I have developed a type-safe bit-wise flags class for my own personal use and am wondering if it would be of interest for me to submit it as a Boost library. At the moment it is not ready for submission (code is messy) but I am testing the waters so I present an overview below. * Fully ISO standard C++11 (except for an MSVC version which is API-compatible except it doesn't support as many an infinite number of flag names) * They are type-safe, requiring explicit casts to and from integers/booleans * Supports standard bit-wise operations, and always keep values within the possible set of flags * Supports getting the name of a flag and looking up flags by name at run-time (when a flag is actually multiple-flags it gives something like "A|B") * Supports getting an array of all flags at run-time
Now the bad things, which may be a deal-breaker: * Because of limitations in C++ the syntax can feel a bit awkward. You have to use a macro to define flag-names (which actually creates a class) then you can pass those flag-names as template arguments to the flags. An example: FLAG_NAME(A); FLAG_NAME(B); FLAG_NAME(C); typedef Flags<std::uint8_t, Flag_A, Flag_B, Flag_C>::WithValues<0x01, 0x02, 0x04> MyFlags; * I also couldn't get around using the ::WithValues nested class * I use a set of macros to make this cleaner (e.g. the above could be FLAGS(A, B, C)) and once defined there is no more ugliness
Thanks for consideration! Jeff
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hello, The syntax is quite messy, but it can be hidden. However I think, it is too complicated and the same can be achieved in simpler way. I personally use slightly modified flags from http://sourceforge.net/projects/lordodinscpplib/ It is simple macro to generate necessary operators for a scoped enum. Unfortunately, this code requires C++11 and I am not sure if this can be rewritten for older compilers. There is some implementation of scoped enums in boost, so it may work. But type safe flags are common enough to be part of boost. The implementation details are just a matter of taste. I would use flags from boost myself. Jiří

On 06/18/2014 02:09 AM, Jeffrey Bush wrote:
I have developed a type-safe bit-wise flags class for my own personal use and am wondering if it would be of interest for me to submit it as a Boost library.
How does it compare to using enum and BOOST_BITMASK? http://www.boost.org/boost/detail/bitmask.hpp

Hi Boost!
I have developed a type-safe bit-wise flags class for my own personal use and am wondering if it would be of interest for me to submit it as a Boost library. At the moment it is not ready for submission (code is messy) but I am testing the waters so I present an overview below. * Fully ISO standard C++11 (except for an MSVC version which is API-compatible except it doesn't support as many an infinite number of flag names) * They are type-safe, requiring explicit casts to and from
integers/booleans
* Supports standard bit-wise operations, and always keep values within the possible set of flags * Supports getting the name of a flag and looking up flags by name at run-time (when a flag is actually multiple-flags it gives something like "A|B") * Supports getting an array of all flags at run-time
Now the bad things, which may be a deal-breaker: * Because of limitations in C++ the syntax can feel a bit awkward. You have to use a macro to define flag-names (which actually creates a class) then you can pass those flag-names as template arguments to the flags. An example: FLAG_NAME(A); FLAG_NAME(B); FLAG_NAME(C); typedef Flags<std::uint8_t, Flag_A, Flag_B, Flag_C>::WithValues<0x01, 0x02, 0x04> MyFlags; * I also couldn't get around using the ::WithValues nested class * I use a set of macros to make this cleaner (e.g. the above could be FLAGS(A, B, C)) and once defined there is no more ugliness
Thanks for consideration! Jeff
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Jeffrey Bush <jeff <at> coderforlife.com> writes: I have posted the current (ugly) code online: https://gist.github.com/coderforlife/6d8bac451d49dd1a0c81#file-flags-hpp Above that is the even uglier MSCV-specific implementation. I focused on function not readability when making it, I understand that it would take significant work to make it Boost ready. Differences from BOOST_BITMASK or lordodinscpplib are that it supports ~ that is value-constrained (only flips bits that are covered by flags, it supports names at run-time (converting to and from strings), and it can default-value then flags with powers of two. If those features are undesired, then this class is unnecessary seeing that those other solutions are significantly simpler or more portable. However, looking at those has given me some ideas on how to simplify my code and make it possibly work in Clang. Jeff
participants (5)
-
Bjorn Reese
-
David Stone
-
Jeffrey Bush
-
Jiří Havel
-
Marcel Raad