Ardour  9.0-pre0-582-g084a23a80d
string_convert.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef PBD_STRING_CONVERT_H
20 #define PBD_STRING_CONVERT_H
21 
22 #include <string>
23 #include <stdint.h>
24 
25 #include "pbd/libpbd_visibility.h"
26 
33 namespace PBD {
34 
35 LIBPBD_API bool bool_to_string (bool val, std::string& str);
36 
37 LIBPBD_API bool int16_to_string (int16_t val, std::string& str);
38 
39 LIBPBD_API bool uint16_to_string (uint16_t val, std::string& str);
40 
41 LIBPBD_API bool int32_to_string (int32_t val, std::string& str);
42 
43 LIBPBD_API bool uint32_to_string (uint32_t val, std::string& str);
44 
45 LIBPBD_API bool int64_to_string (int64_t val, std::string& str);
46 
47 LIBPBD_API bool uint64_to_string (uint64_t val, std::string& str);
48 
49 LIBPBD_API bool float_to_string (float val, std::string& str);
50 
51 LIBPBD_API bool double_to_string (double val, std::string& str);
52 
53 LIBPBD_API bool string_to_bool (const std::string& str, bool& val);
54 
55 LIBPBD_API bool string_to_int16 (const std::string& str, int16_t& val);
56 
57 LIBPBD_API bool string_to_uint16 (const std::string& str, uint16_t& val);
58 
59 LIBPBD_API bool string_to_int32 (const std::string& str, int32_t& val);
60 
61 LIBPBD_API bool string_to_uint32 (const std::string& str, uint32_t& val);
62 
63 LIBPBD_API bool string_to_int64 (const std::string& str, int64_t& val);
64 
65 LIBPBD_API bool string_to_uint64 (const std::string& str, uint64_t& val);
66 
67 LIBPBD_API bool string_to_float (const std::string& str, float& val);
68 
69 LIBPBD_API bool string_to_double (const std::string& str, double& val);
70 
71 template <class T>
72 inline bool to_string (T val, std::string& str)
73 {
74  // This will cause a compile time error if this function is ever
75  // instantiated, which is useful to catch unintended conversions
76  typename T::TO_STRING_TEMPLATE_NOT_DEFINED_FOR_THIS_TYPE invalid_type;
77  return false;
78 }
79 
80 template <class T>
81 inline bool to_string (bool val, std::string& str)
82 {
83  return bool_to_string (val, str);
84 }
85 
86 template <class T>
87 inline bool to_string (int8_t val, std::string& str)
88 {
89  return int16_to_string (val, str);
90 }
91 
92 template <class T>
93 inline bool to_string (uint8_t val, std::string& str)
94 {
95  return uint16_to_string (val, str);
96 }
97 
98 template <class T>
99 inline bool to_string (int16_t val, std::string& str)
100 {
101  return int16_to_string (val, str);
102 }
103 
104 template <class T>
105 inline bool to_string (uint16_t val, std::string& str)
106 {
107  return uint16_to_string (val, str);
108 }
109 
110 template <class T>
111 inline bool to_string (int32_t val, std::string& str)
112 {
113  return int32_to_string (val, str);
114 }
115 
116 template <class T>
117 inline bool to_string (uint32_t val, std::string& str)
118 {
119  return uint32_to_string (val, str);
120 }
121 
122 template <class T>
123 inline bool to_string (int64_t val, std::string& str)
124 {
125  return int64_to_string (val, str);
126 }
127 
128 template <class T>
129 inline bool to_string (uint64_t val, std::string& str)
130 {
131  return uint64_to_string (val, str);
132 }
133 
134 template <class T>
135 inline bool to_string (float val, std::string& str)
136 {
137  return float_to_string (val, str);
138 }
139 
140 template <class T>
141 inline bool to_string (double val, std::string& str)
142 {
143  return double_to_string (val, str);
144 }
145 
146 template <class T>
147 inline bool string_to (const std::string& str, T& val)
148 {
149  // This will cause a compile time error if this function is ever
150  // instantiated, which is useful to catch unintended conversions
151  typename T::TO_STRING_TEMPLATE_NOT_DEFINED_FOR_THIS_TYPE invalid_type;
152  return false;
153 }
154 
155 template <class T>
156 inline bool string_to (const std::string& str, bool& val)
157 {
158  return string_to_bool (str, val);
159 }
160 
161 template <class T>
162 inline bool string_to (const std::string& str, int8_t& val)
163 {
164  int16_t tmp = val;
165  bool success = string_to_int16 (str, tmp);
166  if (!success) return false;
167  val = tmp;
168  return true;
169 }
170 
171 template <class T>
172 inline bool string_to (const std::string& str, uint8_t& val)
173 {
174  uint16_t tmp = val;
175  bool success = string_to_uint16 (str, tmp);
176  if (!success) return false;
177  val = tmp;
178  return true;
179 }
180 
181 template <class T>
182 inline bool string_to (const std::string& str, int16_t& val)
183 {
184  return string_to_int16 (str, val);
185 }
186 
187 template <class T>
188 inline bool string_to (const std::string& str, uint16_t& val)
189 {
190  return string_to_uint16 (str, val);
191 }
192 
193 template <class T>
194 inline bool string_to (const std::string& str, int32_t& val)
195 {
196  return string_to_int32 (str, val);
197 }
198 
199 template <class T>
200 inline bool string_to (const std::string& str, uint32_t& val)
201 {
202  return string_to_uint32 (str, val);
203 }
204 
205 template <class T>
206 inline bool string_to (const std::string& str, int64_t& val)
207 {
208  return string_to_int64 (str, val);
209 }
210 
211 template <class T>
212 inline bool string_to (const std::string& str, uint64_t& val)
213 {
214  return string_to_uint64 (str, val);
215 }
216 
217 template <class T>
218 inline bool string_to (const std::string& str, float& val)
219 {
220  return string_to_float (str, val);
221 }
222 
223 template <class T>
224 inline bool string_to (const std::string& str, double& val)
225 {
226  return string_to_double (str, val);
227 }
228 
230 // Variation that disregards conversion errors
232 
233 template <class T>
234 inline std::string to_string (T val)
235 {
236  // This will cause a compile time error if this function is ever
237  // instantiated, which is useful to catch unintended conversions
238  typename T::TO_STRING_TEMPLATE_NOT_DEFINED_FOR_THIS_TYPE invalid_type;
239  return std::string();
240 }
241 
242 template <>
243 inline std::string to_string (bool val)
244 {
245  std::string tmp;
246  bool_to_string (val, tmp);
247  return tmp;
248 }
249 
250 template <>
251 inline std::string to_string (int8_t val)
252 {
253  std::string tmp;
254  int16_to_string (val, tmp);
255  return tmp;
256 }
257 
258 template <>
259 inline std::string to_string (uint8_t val)
260 {
261  std::string tmp;
262  uint16_to_string (val, tmp);
263  return tmp;
264 }
265 
266 template <>
267 inline std::string to_string (int16_t val)
268 {
269  std::string tmp;
270  int16_to_string (val, tmp);
271  return tmp;
272 }
273 
274 template <>
275 inline std::string to_string (uint16_t val)
276 {
277  std::string tmp;
278  uint16_to_string (val, tmp);
279  return tmp;
280 }
281 
282 template <>
283 inline std::string to_string (int32_t val)
284 {
285  std::string tmp;
286  int32_to_string (val, tmp);
287  return tmp;
288 }
289 
290 template <>
291 inline std::string to_string (uint32_t val)
292 {
293  std::string tmp;
294  uint32_to_string (val, tmp);
295  return tmp;
296 }
297 
298 template <>
299 inline std::string to_string (int64_t val)
300 {
301  std::string tmp;
302  int64_to_string (val, tmp);
303  return tmp;
304 }
305 
306 template <>
307 inline std::string to_string (uint64_t val)
308 {
309  std::string tmp;
310  uint64_to_string (val, tmp);
311  return tmp;
312 }
313 
314 template <>
315 inline std::string to_string (float val)
316 {
317  std::string tmp;
318  float_to_string (val, tmp);
319  return tmp;
320 }
321 
322 template <>
323 inline std::string to_string (double val)
324 {
325  std::string tmp;
326  double_to_string (val, tmp);
327  return tmp;
328 }
329 
330 template <class T>
331 inline T string_to (const std::string& str)
332 {
333  // This will cause a compile time error if this function is ever
334  // instantiated, which is useful to catch unintended conversions
335  typename T::STRING_TO_TEMPLATE_NOT_DEFINED_FOR_THIS_TYPE invalid_type;
336  return T();
337 }
338 
339 template <>
340 inline bool string_to (const std::string& str)
341 {
342  bool tmp;
343  string_to_bool (str, tmp);
344  return tmp;
345 }
346 
347 template <>
348 inline int8_t string_to (const std::string& str)
349 {
350  int16_t tmp;
351  string_to_int16 (str, tmp);
352  return (int8_t) tmp;
353 }
354 
355 template <>
356 inline uint8_t string_to (const std::string& str)
357 {
358  uint16_t tmp;
359  string_to_uint16 (str, tmp);
360  return (uint8_t) tmp;
361 }
362 
363 template <>
364 inline int16_t string_to (const std::string& str)
365 {
366  int16_t tmp;
367  string_to_int16 (str, tmp);
368  return tmp;
369 }
370 
371 template <>
372 inline uint16_t string_to (const std::string& str)
373 {
374  uint16_t tmp;
375  string_to_uint16 (str, tmp);
376  return tmp;
377 }
378 
379 template <>
380 inline int32_t string_to (const std::string& str)
381 {
382  int32_t tmp;
383  string_to_int32 (str, tmp);
384  return tmp;
385 }
386 
387 template <>
388 inline uint32_t string_to (const std::string& str)
389 {
390  uint32_t tmp;
391  string_to_uint32 (str, tmp);
392  return tmp;
393 }
394 
395 template <>
396 inline int64_t string_to (const std::string& str)
397 {
398  int64_t tmp;
399  string_to_int64 (str, tmp);
400  return tmp;
401 }
402 
403 template <>
404 inline uint64_t string_to (const std::string& str)
405 {
406  uint64_t tmp;
407  string_to_uint64 (str, tmp);
408  return tmp;
409 }
410 
411 template <>
412 inline float string_to (const std::string& str)
413 {
414  float tmp;
415  string_to_float (str, tmp);
416  return tmp;
417 }
418 
419 template <>
420 inline double string_to (const std::string& str)
421 {
422  double tmp;
423  string_to_double (str, tmp);
424  return tmp;
425 }
426 
427 } // namespace PBD
428 
429 #endif // PBD_STRING_CONVERT_H
#define LIBPBD_API
Definition: axis_view.h:42
bool float_to_string(float val, std::string &str)
bool string_to_uint64(const std::string &str, uint64_t &val)
bool string_to_uint16(const std::string &str, uint16_t &val)
bool int16_to_string(int16_t val, std::string &str)
bool uint64_to_string(uint64_t val, std::string &str)
bool string_to_int64(const std::string &str, int64_t &val)
bool int64_to_string(int64_t val, std::string &str)
bool string_to_float(const std::string &str, float &val)
bool string_to_int32(const std::string &str, int32_t &val)
bool string_to_uint32(const std::string &str, uint32_t &val)
bool to_string(ARDOUR::AnyTime const &at, std::string &str)
bool string_to_int16(const std::string &str, int16_t &val)
bool string_to_double(const std::string &str, double &val)
bool uint32_to_string(uint32_t val, std::string &str)
bool double_to_string(double val, std::string &str)
bool string_to_bool(const std::string &str, bool &val)
bool int32_to_string(int32_t val, std::string &str)
bool string_to(std::string const &str, ARDOUR::AnyTime &at)
bool bool_to_string(bool val, std::string &str)
bool uint16_to_string(uint16_t val, std::string &str)