Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
Enum types extension

Description


Extension of enum classes.

This extension of C++11 enums has the following features:

Note
- The extended enumerate type is thread-safe as long as the first calls to its IdName() and theEnum() are not multithreadly executed.
- The Combination subclass of the type is not thread-safe.
- The memory footprint is kept to a minimum except for combinations of IDs, that rely on std::bitset. All strings corresponding to the ID keywords are static and bounded just to the particular extended enum type.
- Some methods may throw an exception when referring to an ID not existing in the enum: in that case, a std exception will be thrown, not an Elementa Exception, to avoid circular inclusion of header files.

Usage:

Author
Juan-Antonio Fernandez-Madrigal. http://jafma.net
Date
2018-2019
Collaboration diagram for Enum types extension:

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 Documentation

◆ elementa::base::EnumExtPriv

class elementa::base::EnumExtPriv
template<typename BaseID, class HelperIDString>
class elementa::base::EnumExtPriv< BaseID, HelperIDString >

Implementation of all extended enum features. You will use its methods.

Note
- This declaration is not for the user to create classes or objects directly (use the ELE_CLASS_EXTENUM macro instead), but to inform about the functionality.
- No derived classes are allowed from this enum, just like from original enums.

Definition at line 156 of file enums.h.

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 EnumExtPrivtheEnum (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
 

Member Typedef Documentation

◆ IDBase

template<typename BaseID , class HelperIDString >
using elementa::base::EnumExtPriv< BaseID, HelperIDString >::IDBase = BaseID

Just to leave the BaseID available here.

Definition at line 161 of file enums.h.

Member Function Documentation

◆ kSize()

template<typename BaseID , class HelperIDString >
static constexpr size_t elementa::base::EnumExtPriv< BaseID, HelperIDString >::kSize ( void  )
inlinestaticconstexprnoexcept

Return the number of IDs defined in the enum.

Definition at line 360 of file enums.h.

◆ first()

template<typename BaseID , class HelperIDString >
static BaseID elementa::base::EnumExtPriv< BaseID, HelperIDString >::first ( void  )
inlinestatic

Return the first ID.

Definition at line 364 of file enums.h.

◆ last()

template<typename BaseID , class HelperIDString >
static BaseID elementa::base::EnumExtPriv< BaseID, HelperIDString >::last ( void  )
inlinestatic

Return the last ID.

Definition at line 367 of file enums.h.

◆ Nth()

template<typename BaseID , class HelperIDString >
static constexpr BaseID elementa::base::EnumExtPriv< BaseID, HelperIDString >::Nth ( size_t  n)
inlinestaticconstexpr

Return the n-th ID, or throw if out of range.

Definition at line 372 of file enums.h.

◆ pos()

template<typename BaseID , class HelperIDString >
static constexpr size_t elementa::base::EnumExtPriv< BaseID, HelperIDString >::pos ( BaseID  id)
inlinestaticconstexpr

Return the position of the given ID, from 0, in the enum.

Definition at line 380 of file enums.h.

◆ findByName()

template<typename BaseID , class HelperIDString >
static BaseID elementa::base::EnumExtPriv< BaseID, HelperIDString >::findByName ( const std::string &  name)
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().

◆ IdName()

template<typename BaseID , class HelperIDString >
static std::string elementa::base::EnumExtPriv< BaseID, HelperIDString >::IdName ( BaseID  id,
bool  leavek = false 
)
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().

◆ kAllIdNames()

template<typename BaseID , class HelperIDString >
static constexpr std::string elementa::base::EnumExtPriv< BaseID, HelperIDString >::kAllIdNames ( void  )
inlinestaticconstexprnoexcept

Return a copy of all the ID names of the extended enum.

Return a concatenated string with all ID names, as they have been specified by the user ID list (may include extra spaces, unlike the single ID names returned by IDName()).

Definition at line 423 of file enums.h.

◆ theEnum()

template<typename BaseID , class HelperIDString >
static EnumExtPriv & elementa::base::EnumExtPriv< BaseID, HelperIDString >::theEnum ( void  )
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().

◆ begin()

template<typename BaseID , class HelperIDString >
Iterator elementa::base::EnumExtPriv< BaseID, HelperIDString >::begin ( void  ) const
inlinenoexcept

Definition at line 449 of file enums.h.

◆ end()

template<typename BaseID , class HelperIDString >
Iterator elementa::base::EnumExtPriv< BaseID, HelperIDString >::end ( void  ) const
inlinenoexcept

Definition at line 452 of file enums.h.

Macro Definition Documentation

◆ ELE_CLASS_ENUM

#define ELE_CLASS_ENUM (   NameEnumClass,
  ... 
)

#include <elementa/base/enums.h>

Value:
enum class NameEnumClass { __VA_ARGS__ , kAll_ }; \
struct NameEnumClass##HelperIDString \
{ \
static const char * IdNames(void) noexcept \
{ return(#__VA_ARGS__); } \
}; \
using NameEnumClass##Enum = elementa::base::EnumExtPriv<\
NameEnumClass, \
NameEnumClass##HelperIDString>
Implementation of all extended enum features. You will use its methods.
Definition: enums.h:157

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).

Note
- This macro can be used both inside and outside other classes.
- You must not use any ID with the keyword "kAll_" in the list of IDs: that is reserved for referring to all IDs at once.

Definition at line 502 of file enums.h.