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