Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
serialize.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_SERIALIZE_H_
19 #define ION_BASE_SERIALIZE_H_
20 
21 #include <sstream>
22 #include <string>
23 #include <utility>
24 
25 #include "ion/base/stringutils.h"
26 
27 namespace ion {
28 namespace base {
29 
35 
37 
42 
43 
44 template <typename T>
45 inline bool StringToValue(std::istringstream& in, T* val) { // NOLINT
46  T value;
47  in >> value;
48  if (!in.fail()) {
49  *val = value;
50  return true;
51  } else {
52  return false;
53  }
54 }
55 
57 template <>
58 inline bool StringToValue(std::istringstream& in, bool* val) { // NOLINT
59  std::string value;
60  in >> value;
61  if (!in.fail() && (value == "true" || value == "false")) {
62  *val = value == "true";
63  return true;
64  } else {
65  return false;
66  }
67 }
68 
70 template <typename T, typename U, typename V>
71 inline bool StringToValue(std::istringstream& in, // NOLINT
72  std::basic_string<T, U, V>* val) {
73  std::string v;
74  bool done = false;
75  bool escaping = false;
76  if (GetExpectedChar<'"'>(in)) {
77  char c = 0;
78  do {
80  c = static_cast<char>(in.get());
81  done = !escaping && c == '"';
82  escaping = !escaping && c == '\\';
83  if (!done && !escaping)
84  v.push_back(c);
85  } while (!in.fail() && !done);
86  }
87  if (!in.fail() && done) {
88  *val = v;
89  return true;
90  } else {
91  return false;
92  }
93 }
94 
96 template <typename T, typename U>
97 inline bool StringToValue(std::istringstream& in, // NOLINT
98  std::pair<const T, U>* val) {
99  T key;
100  U value;
101  if (StringToValue(in, &key) && GetExpectedChar<':'>(in) &&
102  StringToValue(in, &value)) {
104  *const_cast<T*>(&val->first) = key;
105  val->second = value;
106  return true;
107  } else {
108  return false;
109  }
110 }
111 
114 template <typename ContainerType>
115 inline bool StringToStlContainer(std::istringstream& in, // NOLINT
116  ContainerType* val) {
117  if (GetExpectedChar<'{'>(in)) {
118  ContainerType v;
120  while (!in.fail()) {
121  typename ContainerType::value_type value;
122  if (StringToValue(in, &value)) {
123  v.insert(v.end(), value);
124  } else {
125  in.setstate(std::ios_base::failbit);
126  break;
127  }
130  if (GetExpectedChar<'}'>(in)) {
131  *val = v;
132  break;
133  } else {
134  in.clear();
135  if (!GetExpectedChar<','>(in))
136  break;
137  }
138  }
139  }
140  return !in.fail();
141 }
142 
144 template <typename T, typename U, typename V, typename W, typename X, bool B,
145  template <class, class, class, class, class, bool>
146  class ContainerType>
147 inline bool StringToValue(std::istringstream& in, // NOLINT
148  ContainerType<T, U, V, W, X, B>* val) {
149  return StringToStlContainer(in, val);
150 }
151 
153 template <typename T, typename U, typename V, typename W, typename X,
154  template <class, class, class, class, class> class ContainerType>
155 inline bool StringToValue(std::istringstream& in, // NOLINT
156  ContainerType<T, U, V, W, X>* val) {
157  return StringToStlContainer(in, val);
158 }
159 
161 template <typename T, typename U, typename V, typename W, bool B,
162  template <class, class, class, class, bool> class ContainerType>
163 inline bool StringToValue(std::istringstream& in, // NOLINT
164  ContainerType<T, U, V, W, B>* val) {
165  return StringToStlContainer(in, val);
166 }
167 
169 template <typename T, typename U, typename V, typename W,
170  template <class, class, class, class> class ContainerType>
171 inline bool StringToValue(std::istringstream& in, // NOLINT
172  ContainerType<T, U, V, W>* val) {
173  return StringToStlContainer(in, val);
174 }
175 
177 template <typename T, typename U, typename V,
178  template <class, class, class> class ContainerType>
179 inline bool StringToValue(std::istringstream& in, // NOLINT
180  ContainerType<T, U, V>* val) {
181  return StringToStlContainer(in, val);
182 }
183 
185 template <typename T, typename U, template <class, class> class ContainerType>
186 inline bool StringToValue(std::istringstream& in, // NOLINT
187  ContainerType<T, U>* val) {
188  return StringToStlContainer(in, val);
189 }
190 
193 template <typename T>
194 inline bool StringToValue(const std::string& s, T* val) {
195  std::istringstream in(s);
196  return StringToValue(in, val);
197 }
198 
200 
205 
206 
208 template <typename T>
209 inline std::string ValueToString(const T& val) {
210  std::ostringstream out;
211  out << val;
212  return out.str();
213 }
214 
216 template <>
217 inline std::string ValueToString(const bool& val) {
218  std::ostringstream out;
219  out << (val ? "true" : "false");
220  return out.str();
221 }
222 
224 template <>
225 inline std::string ValueToString(const float& val) {
226  std::ostringstream out;
227  out.precision(6);
228  out << val;
229  return out.str();
230 }
231 template <>
232 inline std::string ValueToString(const double& val) {
233  std::ostringstream out;
234  out.precision(12);
235  out << val;
236  return out.str();
237 }
238 
240 template <typename T, typename U, typename V>
241 inline std::string ValueToString(const std::basic_string<T, U, V>& val) {
242  std::ostringstream out;
243  out << '"' << EscapeString(val) << '"';
244  return out.str();
245 }
246 
248 template <>
249 inline std::string ValueToString(const char* const& val) {
250  return ValueToString(std::string(val));
251 }
252 
254 template <typename T, typename U>
255 inline std::string ValueToString(const std::pair<const T, U>& val) {
256  std::ostringstream out;
257  out << ValueToString(val.first) << " : " << ValueToString(val.second);
258  return out.str();
259 }
260 
262 template <typename ContainerType>
263 inline std::string StlContainerToString(const ContainerType& c) {
264  std::ostringstream out;
265  out << "{ ";
266  for (typename ContainerType::const_iterator it = c.begin(); it != c.end();
267  ++it) {
268  if (it != c.begin())
269  out << ", ";
270  out << ValueToString(*it);
271  }
272  out << " }";
273  return out.str();
274 }
275 
277 template <typename T, typename U, typename V, typename W, typename X, bool B,
278  template <class, class, class, class, class, bool>
279  class ContainerType>
280 inline std::string ValueToString(
281  const ContainerType<T, U, V, W, X, B>& val) {
282  return StlContainerToString(val);
283 }
284 
286 template <typename T, typename U, typename V, typename W, typename X,
287  template <class, class, class, class, class>
288  class ContainerType>
289 inline std::string ValueToString(
290  const ContainerType<T, U, V, W, X>& val) {
291  return StlContainerToString(val);
292 }
293 
295 template <typename T, typename U, typename V, typename W, bool B,
296  template <class, class, class, class, bool> class ContainerType>
297 inline std::string ValueToString(
298  const ContainerType<T, U, V, W, B>& val) {
299  return StlContainerToString(val);
300 }
301 
303 template <typename T, typename U, typename V, typename W,
304  template <class, class, class, class> class ContainerType>
305 inline std::string ValueToString(
306  const ContainerType<T, U, V, W>& val) {
307  return StlContainerToString(val);
308 }
309 
311 template <typename T, typename U, typename V,
312  template <class, class, class> class ContainerType>
313 inline std::string ValueToString(const ContainerType<T, U, V>& val) {
314  return StlContainerToString(val);
315 }
316 
318 template <typename T, typename U, template <class, class> class ContainerType>
319 inline std::string ValueToString(const ContainerType<T, U>& val) {
320  return StlContainerToString(val);
321 }
322 
323 } // namespace base
324 } // namespace ion
325 
326 #endif // ION_BASE_SERIALIZE_H_
double value
bool StringToStlContainer(std::istringstream &in, ContainerType *val)
Constructs a STL container from a stream.
Definition: serialize.h:115
std::string StlContainerToString(const ContainerType &c)
Serializes an STL container to a string.
Definition: serialize.h:263
bool StringToValue(std::istringstream &in, T *val)
This file defines two public functions: StringToValue() and ValueToString().
Definition: serialize.h:45
std::string ValueToString(const T &val)
ValueToString.
Definition: serialize.h:209
std::string ION_API EscapeString(const std::string &str)
Returns an escaped version of the passed string.
Definition: stringutils.cc:105
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
Definition: stringutils.h:215