Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

enum2str.h

Go to the documentation of this file.
00001 #ifndef DV_UTIL_ENUM2STR_H 00002 #define DV_UTIL_ENUM2STR_H 00003 // $Id: enum2str.h,v 1.5 2003/08/15 08:34:16 dvermeir Exp $ 00004 #include <stdexcept> 00005 #include <dvutil/tostring.h> 00006 00007 namespace Dv { 00008 namespace Util { 00009 /** Convenient class template to support conversion of enum types 00010 * to/from strings. 00011 * 00012 * Example: 00013 * @code 00014 * const char* Dv::Util::enum_parser<X>::E_NAME("X"); // "name" of enum type X 00015 * Dv::Util::enum_parser<X>::enum_entry 00016 * Dv::Util::enum_parser<X>::enum_table[] = { 00017 * { A, "A" }, 00018 * { B, "B" }, 00019 * { C, "C" }, 00020 * { A, 0 } // should end with 0 in 2nd component of last array element! 00021 * }; 00022 * 00023 * std::cout << Dv::Util::enum2str(A) << std::endl; 00024 * std::cout << Dv::Util::str2enum<X>("A") << std::endl; 00025 * @endcode 00026 */ 00027 template<typename E> 00028 class enum_parser { 00029 public: 00030 /** Type of entry in enum_table. */ 00031 typedef struct { E e; const char* s; } enum_entry; 00032 /** Table containing one enum_entry for each E value, 00033 * as well as a sentinel entry (with s=0). 00034 */ 00035 static enum_entry enum_table[]; 00036 /** Name of enum type, should be defined by user. */ 00037 static const char* E_NAME; 00038 /** Convert enum value to string. 00039 * @param e enum value 00040 * @return pointer to C-string representing e, never 0. 00041 * @exception std::logic_error if string representation not found. 00042 */ 00043 static const char* enum2str(E e) throw (std::logic_error) { 00044 static const std::string NAME("enum2str"); 00045 for (unsigned int i=0; enum_table[i].s; ++i) 00046 if (enum_table[i].e == e) 00047 return enum_table[i].s; 00048 throw std::logic_error(NAME + ": cannot convert " + 00049 E_NAME + " value " + tostring(static_cast<int>(e))); 00050 } 00051 /** Convert string to enum value. 00052 * @param s string to convert. 00053 * @return enum value corresponding to s. 00054 * @exception std::logic_error if s does not represent an E value. 00055 */ 00056 static E str2enum(const std::string& s) throw (std::logic_error) { 00057 static const std::string NAME("str2enum"); 00058 for (unsigned int i=0; enum_table[i].s ; ++i) 00059 if (enum_table[i].s == s) 00060 return enum_table[i].e; 00061 throw std::logic_error(NAME + ": cannot convert '" + s + "' to " + E_NAME); 00062 } 00063 }; 00064 00065 /** Convenience function to convert an enum value to a string. 00066 * @param e Enum value. 00067 * @return C-string representation of e (never 0). 00068 * @exception std::logic_error if string representation not found. 00069 */ 00070 template<typename E> 00071 const char* 00072 enum2str(E e) { 00073 return enum_parser<E>::enum2str(e); 00074 } 00075 00076 /** Convenience function to convert a string to an enum value. 00077 * @param s string representing an enum value. 00078 * @return enum value represented by string. 00079 * @exception std::logic_error if string does not represent an E value. 00080 */ 00081 template<typename E> 00082 E 00083 str2enum(const std::string& s) { 00084 return enum_parser<E>::str2enum(s); 00085 } 00086 00087 }} 00088 #endif

dvutil-0.13.15 [30 December, 2004]