Convenient class template to support conversion of enum types to/from strings. More...
#include <enum2str.h>
Classes | |
struct | enum_entry |
Type of entry in enum_table. More... | |
Static Public Member Functions | |
static const char * | enum2str (E e) throw (std::logic_error) |
Convert enum value to string. | |
static E | str2enum (const std::string &s) throw (std::logic_error) |
Convert string to enum value. | |
Static Public Attributes | |
static enum_entry | enum_table [] |
Table containing one enum_entry for each E value, as well as a sentinel entry (with s=0). | |
static const char * | E_NAME |
Name of enum type, should be defined by user. |
Convenient class template to support conversion of enum types to/from strings.
Example:
template <> const char* Dv::Util::enum_parser<X>::E_NAME("X"); // "name" of enum type X template<> Dv::Util::enum_parser<X>::enum_entry Dv::Util::enum_parser<X>::enum_table[] = { { A, "A" }, { B, "B" }, { C, "C" }, { A, 0 } // should end with 0 in 2nd component of last array element! }; std::cout << Dv::Util::enum2str(A) << std::endl; std::cout << Dv::Util::str2enum<X>("A") << std::endl;
Definition at line 31 of file enum2str.h.
static const char* Dv::Util::enum_parser< E >::enum2str | ( | E | e | ) | throw (std::logic_error) [inline, static] |
Convert enum value to string.
e | enum value |
std::logic_error | if string representation not found. |
Definition at line 46 of file enum2str.h.
References Dv::Util::enum_parser< E >::E_NAME, Dv::Util::enum_parser< E >::enum_table, Dv::Util::enum_parser< E >::enum_entry::s, and Dv::tostring().
Referenced by Dv::Util::enum2str().
static E Dv::Util::enum_parser< E >::str2enum | ( | const std::string & | s | ) | throw (std::logic_error) [inline, static] |
Convert string to enum value.
s | string to convert. |
std::logic_error | if s does not represent an E value. |
Definition at line 59 of file enum2str.h.
References Dv::Util::enum_parser< E >::enum_entry::e, Dv::Util::enum_parser< E >::E_NAME, Dv::Util::enum_parser< E >::enum_table, and Dv::Util::enum_parser< E >::enum_entry::s.
Referenced by Dv::Util::str2enum().
enum_entry Dv::Util::enum_parser< E >::enum_table[] [static] |
Table containing one enum_entry for each E value, as well as a sentinel entry (with s=0).
Definition at line 38 of file enum2str.h.
Referenced by Dv::Util::enum_parser< E >::enum2str(), and Dv::Util::enum_parser< E >::str2enum().
const char* Dv::Util::enum_parser< E >::E_NAME [static] |
Name of enum type, should be defined by user.
Definition at line 40 of file enum2str.h.
Referenced by Dv::Util::enum_parser< E >::enum2str(), and Dv::Util::enum_parser< E >::str2enum().
dvutil-1.0.10 | [ 5 December, 2009] |