zen::Xml
Simple C++ XML Processing
 All Classes Namespaces Functions Variables
cvrt_text.h
00001 // **************************************************************************
00002 // * This file is part of the zen::Xml project. It is distributed under the *
00003 // * Boost Software License: http://www.boost.org/LICENSE_1_0.txt           *
00004 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
00005 // **************************************************************************
00006 
00007 #ifndef ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434
00008 #define ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434
00009 
00010 #include <zen/utf.h>
00011 #include <zen/string_tools.h>
00012 
00013 namespace zen
00014 {
00061 
00062 
00067 template <class T> bool readText(const std::string& input, T& value);
00069 
00073 template <class T> void writeText(const T& value, std::string& output);
00074 
00075 
00076 /* Different classes of data types:
00077 
00078 -----------------------------
00079 | structured                |  readStruc/writeStruc - e.g. string-convertible types, STL containers, std::pair, structured user types
00080 | ------------------------- |
00081 | | to-string-convertible | |  readText/writeText   - e.g. string-like types, all built-in arithmetic numbers, bool
00082 | | ---------------       | |
00083 | | | string-like |       | |  utfCvrtTo            - e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ...
00084 | | ---------------       | |
00085 | ------------------------- |
00086 -----------------------------
00087 */
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 //------------------------------ implementation -------------------------------------
00104 
00105 //Conversion from arbitrary types to text (for use with XML elements and attributes)
00106 enum TextType
00107 {
00108     TEXT_TYPE_BOOL,
00109     TEXT_TYPE_NUMBER,
00110     TEXT_TYPE_STRING,
00111     TEXT_TYPE_OTHER,
00112 };
00113 
00114 template <class T>
00115 struct GetTextType : StaticEnum<TextType,
00116         IsSameType<T, bool>::value ? TEXT_TYPE_BOOL   :
00117         IsStringLike<T>::value     ? TEXT_TYPE_STRING : //string before number to correctly handle char/wchar_t -> this was an issue with Loki only!
00118         IsArithmetic<T>::value     ? TEXT_TYPE_NUMBER : //
00119         TEXT_TYPE_OTHER> {};
00120 
00121 //######################################################################################
00122 
00123 template <class T, TextType type>
00124 struct ConvertText;
00125 /* -> expected interface
00126 {
00127     void writeText(const T& value, std::string& output) const;
00128     bool readText(const std::string& input, T& value) const;
00129 };
00130 */
00131 
00132 //partial specialization: type bool
00133 template <class T>
00134 struct ConvertText<T, TEXT_TYPE_BOOL>
00135 {
00136     void writeText(bool value, std::string& output) const
00137     {
00138         output = value ? "true" : "false";
00139     }
00140     bool readText(const std::string& input, bool& value) const
00141     {
00142         std::string tmp = input;
00143         zen::trim(tmp);
00144         if (tmp == "true")
00145             value = true;
00146         else if (tmp == "false")
00147             value = false;
00148         else
00149             return false;
00150         return true;
00151     }
00152 };
00153 
00154 //partial specialization: handle conversion for all built-in arithmetic types!
00155 template <class T>
00156 struct ConvertText<T, TEXT_TYPE_NUMBER>
00157 {
00158     void writeText(const T& value, std::string& output) const
00159     {
00160         output = numberTo<std::string>(value);
00161     }
00162     bool readText(const std::string& input, T& value) const
00163     {
00164         value = stringTo<T>(input);
00165         return true;
00166     }
00167 };
00168 
00169 //partial specialization: handle conversion for all string-like types!
00170 template <class T>
00171 struct ConvertText<T, TEXT_TYPE_STRING>
00172 {
00173     void writeText(const T& value, std::string& output) const
00174     {
00175         output = utfCvrtTo<std::string>(value);
00176     }
00177     bool readText(const std::string& input, T& value) const
00178     {
00179         value = utfCvrtTo<T>(input);
00180         return true;
00181     }
00182 };
00183 
00184 
00185 //partial specialization: unknown type
00186 template <class T>
00187 struct ConvertText<T, TEXT_TYPE_OTHER>
00188 {
00189     //###########################################################################################################################################
00190     assert_static(sizeof(T) == -1);
00191     /*
00192         ATTENTION: The data type T is yet unknown to the zen::Xml framework!
00193 
00194         Please provide a specialization for T of the following two functions in order to handle conversions to XML elements and attributes
00195 
00196         template <> void zen::writeText(const T& value, std::string& output)
00197         template <> bool zen::readText(const std::string& input, T& value)
00198 
00199         If T is structured and cannot be converted to a text representation specialize these two functions to allow at least for conversions to XML elements:
00200 
00201         template <> void zen::writeStruc(const T& value, XmlElement& output)
00202         template <> bool zen::readStruc(const XmlElement& input, T& value)
00203     */
00204     //###########################################################################################################################################
00205 };
00206 
00207 
00208 template <class T> inline
00209 void writeText(const T& value, std::string& output)
00210 {
00211     ConvertText<T, GetTextType<T>::value>().writeText(value, output);
00212 }
00213 
00214 
00215 template <class T> inline
00216 bool readText(const std::string& input, T& value)
00217 {
00218     return ConvertText<T, GetTextType<T>::value>().readText(input, value);
00219 }
00220 }
00221 
00222 #endif //ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434