JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
ParameterStringConversionImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 // This code is inspired by the Neuromorphic Robotics Toolkit (http://nrtkit.org)
19 
20 #pragma once
21 
22 #include <jevois/Util/Utils.H>
23 #include <cctype>
24 
25 // ######################################################################
26 template <typename T> inline
27 void jevois::paramValToString(T const & val, std::string & result)
28 {
29  result = jevois::to_string(val);
30 }
31 
32 // ######################################################################
33 template <> inline
34 void jevois::paramValToString<unsigned char>(unsigned char const & val, std::string & result)
35 {
36  // Convert to int so we don't interpret val as a character:
37  result = jevois::to_string(int(val));
38 }
39 
40 // ######################################################################
41 template <typename T> inline
42 void jevois::paramStringToVal(std::string const & valstring, T & result)
43 {
44  result = jevois::from_string<T>(valstring);
45 }
46 
47 // ######################################################################
48 template <typename Q> inline
49 void jevois::paramValToString(std::vector<Q> const & val, std::string & result)
50 {
51  std::stringstream s; s << val; result = s.str();
52 }
53 
54 // ######################################################################
55 template <typename Q> inline
56 void jevois::paramStringToVal(std::string const & valstring, std::vector<Q> & result)
57 {
58  std::stringstream s(valstring); s >> result;
59 }
60 
61 // ######################################################################
62 template <typename K, typename V> inline
63 void jevois::paramValToString(std::map<K,V> const & val, std::string & result)
64 {
65  std::stringstream s; s << val; result = s.str();
66 }
67 
68 // ######################################################################
69 template <typename K, typename V> inline
70 void jevois::paramStringToVal(std::string const & valstring, std::map<K,V> & result)
71 {
72  std::stringstream s(valstring); s >> result;
73 }
74 
75 // ######################################################################
76 template <> inline
77 void jevois::paramValToString<bool>(bool const & val, std::string & result)
78 {
79  if (val) result = "true"; else result = "false";
80 }
81 
82 // ######################################################################
83 template <> inline
84 void jevois::paramStringToVal<bool>(std::string const & valstring, bool & result)
85 {
86  std::string vs = valstring; for (char & c : vs) c = std::tolower(c);
87 
88  if (vs == "" || vs == "true" || vs == "t" || vs == "yes" || vs == "y" || vs == "1") result = true;
89  else if (vs == "false" || vs == "f" || vs == "no" || vs == "n" || vs == "0") result = false;
90  else result = jevois::from_string<bool>(valstring);
91 }
92 
93 // ######################################################################
94 template <> inline
95 void jevois::paramValToString<std::string>(std::string const & val, std::string & result)
96 { result = val; }
97 
98 // ######################################################################
99 template <> inline
100 void jevois::paramStringToVal<std::string>(std::string const & valstring, std::string & result)
101 { result = valstring; }
102 
void paramStringToVal(std::string const &valstring, T &result)
Machine-readable conversion from string to T, for use in jevois::Parameter.
void paramValToString(T const &val, std::string &result)
Machine-readable conversion of T to a string, for use in jevois::Parameter.
void paramValToString< unsigned char >(unsigned char const &val, std::string &result)
Machine-readable conversion of T to a string, specialization to convert unsigned char to number...
void paramValToString< bool >(bool const &val, std::string &result)
Machine-readable conversion of bool to a string, for use in jevois::Parameter.
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:51
void paramStringToVal< bool >(std::string const &valstring, bool &result)
Machine-readable conversion from string to bool, for use in jevois::Parameter.