GEOSX
Classes | Namespaces | Macros
EnumStrings.hpp File Reference
#include "codingUtilities/StringUtilities.hpp"
#include "common/DataTypes.hpp"
#include "common/Logger.hpp"
#include <iostream>
#include <sstream>
#include <array>
#include <type_traits>
#include <algorithm>

Go to the source code of this file.

Classes

struct  geosx::EnumStrings< ENUM >
 Provides enum <-> string conversion facilities. More...
 

Namespaces

 geosx
 

Macros

#define ENUM_STRINGS(ENUM, ...)
 Associate a list of string names with enumeration values. More...
 

Detailed Description

Collection of utilities to facilitate I/O of enumeration types. Provides a macro definition that allows associating string names with enumeration constants and a set of functions that make use of these strings, like stream insertion/extraction operators.

Definition in file EnumStrings.hpp.

Macro Definition Documentation

◆ ENUM_STRINGS

#define ENUM_STRINGS (   ENUM,
  ... 
)
Value:
static_assert( std::is_enum< ENUM >::value, "Not an enumeration" ); \
\
inline auto const & getEnumStrings( ENUM const ) \
{ \
constexpr int N = internal::countArgs( __VA_ARGS__ ); \
static constexpr std::array< char const *, N > ss{ __VA_ARGS__ }; \
return ss; \
} \
\
inline std::ostream & operator<<( std::ostream & os, ENUM const e ) \
{ \
os << EnumStrings< ENUM >::toString( e ); \
return os; \
} \
\
inline std::istream & operator>>( std::istream & is, ENUM & e ) \
{ \
string s; is >> s; \
e = EnumStrings< ENUM >::fromString( s ); \
return is; \
}
std::ostream & operator<<(std::ostream &os, const KeyIndexT< KEY_TYPE, INDEX_TYPE, INVALID_INDEX > key)
Print the KeyIndex to an output stream.
Definition: KeyIndexT.hpp:151
std::istream & operator>>(std::istream &is, PlotLevel &plotLevel)
Reads a PlotLevel enum from a stream.

Associate a list of string names with enumeration values.

Parameters
ENUMthe enumeration type
...list of names (C-string literals)

Conditions (not enforced but won't work correctly if violated):

  • the macro must be called in the same namespace the enumeration type is defined in
  • the number and order of string arguments passed must match the enum values
  • enumeration constants must not have custom values assigned

After the macro has been called, template instantiation EnumStrings<ENUM> may be used to get access to strings at runtime. While not strictly necessary, it is recommended that macro call immediately follows the enum definition (or the class definition, if enum is defined inside a class).

Definition at line 69 of file EnumStrings.hpp.