zen::Xml
Simple C++ XML Processing
 All Classes Namespaces Functions Variables Pages
cvrt_text.h
1 // **************************************************************************
2 // * This file is part of the FreeFileSync project. It is distributed under *
3 // * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
4 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
5 // **************************************************************************
6 
7 #ifndef ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434
8 #define ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434
9 
10 #include <zen/utf.h>
11 #include <zen/string_tools.h>
12 
13 namespace zen
14 {
61 
67 template <class T> bool readText(const std::string& input, T& value);
69 
73 template <class T> void writeText(const T& value, std::string& output);
74 
75 
76 /* Different classes of data types:
77 
78 -----------------------------
79 | structured | readStruc/writeStruc - e.g. string-convertible types, STL containers, std::pair, structured user types
80 | ------------------------- |
81 | | to-string-convertible | | readText/writeText - e.g. string-like types, all built-in arithmetic numbers, bool
82 | | --------------- | |
83 | | | string-like | | | utfCvrtTo - e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ...
84 | | --------------- | |
85 | ------------------------- |
86 -----------------------------
87 */
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 //------------------------------ implementation -------------------------------------
104 
105 //Conversion from arbitrary types to text (for use with XML elements and attributes)
106 enum TextType
107 {
108  TEXT_TYPE_BOOL,
109  TEXT_TYPE_NUMBER,
110  TEXT_TYPE_STRING,
111  TEXT_TYPE_OTHER,
112 };
113 
114 template <class T>
115 struct GetTextType : StaticEnum<TextType,
116  IsSameType<T, bool>::value ? TEXT_TYPE_BOOL :
117  IsStringLike<T>::value ? TEXT_TYPE_STRING : //string before number to correctly handle char/wchar_t -> this was an issue with Loki only!
118  IsArithmetic<T>::value ? TEXT_TYPE_NUMBER : //
119  TEXT_TYPE_OTHER> {};
120 
121 //######################################################################################
122 
123 template <class T, TextType type>
124 struct ConvertText;
125 /* -> expected interface
126 {
127  void writeText(const T& value, std::string& output) const;
128  bool readText(const std::string& input, T& value) const;
129 };
130 */
131 
132 //partial specialization: type bool
133 template <class T>
134 struct ConvertText<T, TEXT_TYPE_BOOL>
135 {
136  void writeText(bool value, std::string& output) const
137  {
138  output = value ? "true" : "false";
139  }
140  bool readText(const std::string& input, bool& value) const
141  {
142  std::string tmp = input;
143  zen::trim(tmp);
144  if (tmp == "true")
145  value = true;
146  else if (tmp == "false")
147  value = false;
148  else
149  return false;
150  return true;
151  }
152 };
153 
154 //partial specialization: handle conversion for all built-in arithmetic types!
155 template <class T>
156 struct ConvertText<T, TEXT_TYPE_NUMBER>
157 {
158  void writeText(const T& value, std::string& output) const
159  {
160  output = numberTo<std::string>(value);
161  }
162  bool readText(const std::string& input, T& value) const
163  {
164  value = stringTo<T>(input);
165  return true;
166  }
167 };
168 
169 //partial specialization: handle conversion for all string-like types!
170 template <class T>
171 struct ConvertText<T, TEXT_TYPE_STRING>
172 {
173  void writeText(const T& value, std::string& output) const
174  {
175  output = utfCvrtTo<std::string>(value);
176  }
177  bool readText(const std::string& input, T& value) const
178  {
179  value = utfCvrtTo<T>(input);
180  return true;
181  }
182 };
183 
184 
185 //partial specialization: unknown type
186 template <class T>
187 struct ConvertText<T, TEXT_TYPE_OTHER>
188 {
189  //###########################################################################################################################################
190  assert_static(sizeof(T) == -1);
191  /*
192  ATTENTION: The data type T is yet unknown to the zen::Xml framework!
193 
194  Please provide a specialization for T of the following two functions in order to handle conversions to XML elements and attributes
195 
196  template <> void zen::writeText(const T& value, std::string& output)
197  template <> bool zen::readText(const std::string& input, T& value)
198 
199  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:
200 
201  template <> void zen::writeStruc(const T& value, XmlElement& output)
202  template <> bool zen::readStruc(const XmlElement& input, T& value)
203  */
204  //###########################################################################################################################################
205 };
206 
207 
208 template <class T> inline
209 void writeText(const T& value, std::string& output)
210 {
211  ConvertText<T, GetTextType<T>::value>().writeText(value, output);
212 }
213 
214 
215 template <class T> inline
216 bool readText(const std::string& input, T& value)
217 {
218  return ConvertText<T, GetTextType<T>::value>().readText(input, value);
219 }
220 }
221 
222 #endif //ZEN_XML_CONVERT_TEXT_HEADER_018727339083427097434
bool readText(const std::string &input, T &value)
Convert text to user data - used by XML elements and attributes.
Definition: cvrt_text.h:216
void writeText(const T &value, std::string &output)
Convert user data into text - used by XML elements and attributes.
Definition: cvrt_text.h:209