Dv::Util::enum_parser< E > Class Template Reference

Convenient class template to support conversion of enum types to/from strings. More...

#include <enum2str.h>

Collaboration diagram for Dv::Util::enum_parser< E >:
Collaboration graph
[legend]

List of all members.

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.

Detailed Description

template<typename E>
class Dv::Util::enum_parser< E >

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.


Member Function Documentation

template<typename E >
static const char* Dv::Util::enum_parser< E >::enum2str ( e  )  throw (std::logic_error) [inline, static]

Convert enum value to string.

Parameters:
e enum value
Returns:
pointer to C-string representing e, never 0.
Exceptions:
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().

template<typename E >
static E Dv::Util::enum_parser< E >::str2enum ( const std::string &  s  )  throw (std::logic_error) [inline, static]

Convert string to enum value.

Parameters:
s string to convert.
Returns:
enum value corresponding to s.
Exceptions:
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().


Member Data Documentation

template<typename E >
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().

template<typename E >
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().


The documentation for this class was generated from the following file:

dvutil-1.0.10 [ 5 December, 2009]