GEOS
Classes | Namespaces | Macros
EnumStrings.hpp File Reference
#include "common/format/StringUtilities.hpp"
#include "common/DataTypes.hpp"
#include "common/logger/Logger.hpp"
#include "common/format/Format.hpp"
#include <iostream>
#include <type_traits>
#include <algorithm>

Go to the source code of this file.

Classes

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

Namespaces

 geos
 

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:
inline auto const & getEnumStrings( ENUM const ) \
{ \
static constexpr char const * ss[] { __VA_ARGS__ }; \
return ss; \
} \
\
inline auto const & getEnumTypeNameString( ENUM const ) \
{ \
return #ENUM; \
} \
\
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; \
} \
\
inline string toString( ENUM const e ) \
{ \
return EnumStrings< ENUM >::toString( e ); \
} \
\
static_assert( std::is_enum< ENUM >::value, "Not an enumeration" )
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:152
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).

enum struct VTKOutputMode { BINARY, ASCII }; ENUM_STRINGS( VTKOutputMode, "binary", "ascii" );

Definition at line 79 of file EnumStrings.hpp.