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