JeVois  1.3
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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 F, typename S> inline
63 void jevois::paramValToString(std::pair<F, S> const & val, std::string & result)
64 {
65  std::stringstream s; s << val.first << ' ' << val.second; result = s.str();
66 }
67 
68 // ######################################################################
69 template <typename F, typename S> inline
70 void jevois::paramStringToVal(std::string const & valstring, std::pair<F, S> & result)
71 {
72  std::stringstream s(valstring); s >> result.first >> std::skipws >> result.second;
73 }
74 
75 // ######################################################################
76 template <typename T> inline
77 void jevois::paramValToString(cv::Point_<T> const & val, std::string & result)
78 {
79  std::stringstream s; s << val.x << ' ' << val.y; result = s.str();
80 }
81 
82 // ######################################################################
83 template <typename T> inline
84 void jevois::paramStringToVal(std::string const & valstring, cv::Point_<T> & result)
85 {
86  std::stringstream s(valstring); s >> result.x >> std::skipws >> result.y;
87 }
88 
89 // ######################################################################
90 template <typename T> inline
91 void jevois::paramValToString(cv::Size_<T> const & val, std::string & result)
92 {
93  std::stringstream s; s << val.width << ' ' << val.height; result = s.str();
94 }
95 
96 // ######################################################################
97 template <typename T> inline
98 void jevois::paramStringToVal(std::string const & valstring, cv::Size_<T> & result)
99 {
100  std::stringstream s(valstring); s >> result.width >> std::skipws >> result.height;
101 }
102 
103 // ######################################################################
104 template <typename K, typename V> inline
105 void jevois::paramValToString(std::map<K,V> const & val, std::string & result)
106 {
107  std::stringstream s; s << val; result = s.str();
108 }
109 
110 // ######################################################################
111 template <typename K, typename V> inline
112 void jevois::paramStringToVal(std::string const & valstring, std::map<K,V> & result)
113 {
114  std::stringstream s(valstring); s >> result;
115 }
116 
117 // ######################################################################
118 template <> inline
119 void jevois::paramValToString<bool>(bool const & val, std::string & result)
120 {
121  if (val) result = "true"; else result = "false";
122 }
123 
124 // ######################################################################
125 template <> inline
126 void jevois::paramStringToVal<bool>(std::string const & valstring, bool & result)
127 {
128  std::string vs = valstring; for (char & c : vs) c = std::tolower(c);
129 
130  if (vs == "" || vs == "true" || vs == "t" || vs == "yes" || vs == "y" || vs == "1") result = true;
131  else if (vs == "false" || vs == "f" || vs == "no" || vs == "n" || vs == "0") result = false;
132  else result = jevois::from_string<bool>(valstring);
133 }
134 
135 // ######################################################################
136 template <> inline
137 void jevois::paramValToString<std::string>(std::string const & val, std::string & result)
138 { result = val; }
139 
140 // ######################################################################
141 template <> inline
142 void jevois::paramStringToVal<std::string>(std::string const & valstring, std::string & result)
143 { result = valstring; }
144 
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.