![]() |
Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
|
Extension of enum classes.
This extension of C++11 enums has the following features:
std::bitset. All strings corresponding to the ID keywords are static and bounded just to the particular extended enum type.Usage:
\code
ELE_CLASS_ENUM(My, // The enum class name; base is int
kOne, // automatically assigned 0 by C++ stdrd
kTwo, // automatically assigned 1
kThree,
kFour,
kFive); // The enum ID keywords.
// Strings equal to these keywords
// (without trailing spaces) are
// statically associated to them.
// You must not use the keyword "kAll_" in the ID list: it is
// reserved for referring to all IDs at once, and will be
// created automatically by the macro.
\endcode
\code for (auto item: MyEnum::theEnum()) ... item is My ... \endcode
\code
MyEnum::Combination comb; // no ID in the combination
MyEnum::Combination comb{My::ID1, My::ID2, ...};
MyEnum::Combination comb{My::kAll_}; // all IDs combined
some_method_admitting_combinations( ..., ..., {My::ID1} )
// Notice you have not to write "Combination" due to implicit
// conversion from the initializer list to it. If you need just
// one ID in the combination, you can either use the initializer
// syntax as in this example or be explicit about the type with
// "Combination(My::ID1)"; you cannot use a single ID and
// hope it is implicitly converted to Combination unless it is
// in an initializer list with "{}".
\endcode
\code for (auto item: comb) ... item is My ... \endcode

Classes | |
| class | elementa::base::EnumExtPriv< BaseID, HelperIDString > |
| Implementation of all extended enum features. You will use its methods. More... | |
Macros | |
| #define | ELE_CLASS_ENUM(NameEnumClass, ...) |
| Define an extended enum type. More... | |
| class elementa::base::EnumExtPriv |
Implementation of all extended enum features. You will use its methods.
Public Types | |
| using | IDBase = BaseID |
| Just to leave the BaseID available here. More... | |
Static Public Member Functions | |
| static constexpr size_t | kSize (void) noexcept |
| Return the number of IDs defined in the enum. More... | |
| static BaseID | first (void) |
| Return the first ID. More... | |
| static BaseID | last (void) |
| Return the last ID. More... | |
| static constexpr BaseID | Nth (size_t n) |
| Return the n-th ID, or throw if out of range. More... | |
| static constexpr size_t | pos (BaseID id) |
| Return the position of the given ID, from 0, in the enum. More... | |
| static BaseID | findByName (const std::string &name) |
| Return the ID of a given enum from its name as a string. More... | |
| static std::string | IdName (BaseID id, bool leavek=false) |
| Return the string corresponding to the ID keyword, or "" if not defined. More... | |
| static constexpr std::string | kAllIdNames (void) noexcept |
| Return a copy of all the ID names of the extended enum. More... | |
| static EnumExtPriv & | theEnum (void) noexcept |
| Access to the singleton object of this class. More... | |
Public Member Functions | |
To iterate on enums | |
| Iterator | begin (void) const noexcept |
| Iterator | end (void) const noexcept |
| using elementa::base::EnumExtPriv< BaseID, HelperIDString >::IDBase = BaseID |
|
inlinestaticconstexprnoexcept |
|
inlinestatic |
|
inlinestatic |
|
inlinestaticconstexpr |
|
inlinestaticconstexpr |
|
inlinestatic |
Return the ID of a given enum from its name as a string.
Return kAll_ if it is not one of the enum elements.
Definition at line 384 of file enums.h.
References elementa::base::EnumExtPriv< BaseID, HelperIDString >::IdName(), and elementa::base::EnumExtPriv< BaseID, HelperIDString >::theEnum().
|
inlinestatic |
Return the string corresponding to the ID keyword, or "" if not defined.
The returned value is a copy of the internal static name. If LEAVEK == TRUE, return the original name; otherwise drop the initial "k" if it is a name beginning with "k" and going on with a capital letter. Cannot be constexpr.
Definition at line 397 of file enums.h.
References elementa::base::split().
Referenced by elementa::base::EnumExtPriv< BaseID, HelperIDString >::findByName(), and elementa::base::EnumExtPriv< BaseID, HelperIDString >::Combination::to_string().
|
inlinestaticconstexprnoexcept |
|
inlinestaticnoexcept |
Access to the singleton object of this class.
Needed since the class has no attributes (the singleton is empty); required for some operations that cannot be done with the class, such as iteration.
Example:
\code
// Also, BaseID instead of auto would be ok.
for (auto item: EnumFromIDs<...>::theEnum())
std::cout << "\tEnum ID to integer: "
<< static_cast<int>(item)
<< std::endl;
\endcode
Definition at line 441 of file enums.h.
Referenced by elementa::base::EnumExtPriv< BaseID, HelperIDString >::findByName().
|
inlinenoexcept |
|
inlinenoexcept |
| #define ELE_CLASS_ENUM | ( | NameEnumClass, | |
| ... | |||
| ) |
#include <elementa/base/enums.h>
Define an extended enum type.
This macro creates two new types useful for the user: NameEnumClassEnum (the extended enum type with all the new functionality and the Combination subclass) and NameEnumClass (the C++11 enum class type with all the enum IDs).