JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
ParameterStringConversion.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 <string>
23 #include <iostream>
24 #include <sstream>
25 #include <vector>
26 #include <opencv2/core/core.hpp> // for Point_ and Size
27 
28 namespace jevois
29 {
30  /*! \defgroup parameterstrconv String conversions for Parameter
31  \ingroup parameter */
32 
33  /*! @{ */ // **********************************************************************
34 
35  //! Machine-readable conversion of T to a string, for use in jevois::Parameter
36  /*! A default implementation is provided which internally just uses jevois::to_string(). When implementing new types
37  that can be used as parameters, users should supply the appropriate template specialization for this
38  function. This function is used internally by Parameter to get a Parameter value as a string. */
39  template <typename T>
40  void paramValToString(T const & val, std::string & result);
41 
42  //! Machine-readable conversion of T to a string, specialization to convert unsigned char to number, not character
43  template <>
44  void paramValToString<unsigned char>(unsigned char const & val, std::string & result);
45 
46  //! Machine-readable conversion from string to T, for use in jevois::Parameter
47  /*! @throws Implementation-dependent exception encountered during conversion
48 
49  A default implementation is provided which internally just uses jevois::from_string(). When implementing new types
50  that can be used as parameters, users should supply the appropriate template specialization for this
51  function. This function is used internally by Parameter to set a Parameter value from string. */
52  template <typename T>
53  void paramStringToVal(std::string const & valstring, T & result);
54 
55  //! Machine-readable output to a string, for use in jevois::Parameter: outputs [\c Element1, \c Element2, ...]
56  /*! Specialization for std::vector<T> which supports nesting of other vectors and vectors of other types */
57  template <typename Q>
58  void paramValToString(std::vector<Q> const & val, std::string & result);
59 
60  //! Machine-readable input from a string, for use in jevois::Parameter: reads [\c Element1, \c Element2, ...]
61  /*! Specialization for std::vector<T> which supports nesting of other vectors and vectors of other types */
62  template <typename Q>
63  void paramStringToVal(std::string const & valstring, std::vector<Q> & result);
64 
65  //! Machine-readable output to a string, for use in jevois::Parameter: outputs \c first \c second
66  template <typename F, typename S>
67  void paramValToString(std::pair<F, S> const & val, std::string & result);
68 
69  //! Machine-readable input from a string, for use in jevois::Parameter: reads \c first \c second
70  template <typename F, typename S>
71  void paramStringToVal(std::string const & valstring, std::pair<F, S> & result);
72 
73  //! Machine-readable output to a string, for use in jevois::Parameter: outputs \c x \c y
74  template <typename T>
75  void paramValToString(cv::Point_<T> const & val, std::string & result);
76 
77  //! Machine-readable input from a string, for use in jevois::Parameter: reads \c x \c y
78  template <typename T>
79  void paramStringToVal(std::string const & valstring, cv::Point_<T> & result);
80 
81  //! Machine-readable output to a string, for use in jevois::Parameter: outputs \c width \c height
82  template <typename T>
83  void paramValToString(cv::Size_<T> const & val, std::string & result);
84 
85  //! Machine-readable input from a string, for use in jevois::Parameter: reads \c width \c height
86  template <typename T>
87  void paramStringToVal(std::string const & valstring, cv::Size_<T> & result);
88 
89  //! Machine-readable output to a string, for use in jevois::Parameter: outputs [\c Key1:Value1, \c Key2:Value2, ...]
90  /*! Specialization for std::map which supports nesting of other maps/vectors */
91  template <typename K, typename V>
92  void paramValToString(std::map<K,V> const & val, std::string & result);
93 
94  //! Machine-readable input from a string, for use in jevois::Parameter: reads [\c Key1:Value1, \c Key2:Value2, ...]
95  /*! Specialization for std::map which supports nesting of other maps/vectors */
96  template <typename K, typename V>
97  void paramStringToVal(std::string const & valstring, std::map<K,V> & result);
98 
99  //! Machine-readable conversion of bool to a string, for use in jevois::Parameter
100  /*! @returns "true" or "false". */
101  template <>
102  void paramValToString<bool>(bool const & val, std::string & result);
103 
104  //! Machine-readable conversion from string to bool, for use in jevois::Parameter
105  /*! @throws jevois::exception::Exception if input string is malformed.
106 
107  Specialization for bool so that we can accept nice verbose terms rather than just 0 and 1. Acceptable ways to set
108  a bool Parameter are:
109 
110  - "true" or "false"
111  - "t" or "f"
112  - "yes" or "no"
113  - "y" or "n"
114  - 1 or 0
115  - An empty string will set the Parameter to true, so you may use boolean parameters as switches, e.g.,
116  <code>"--myFlag"</code> is equivalent to <code>"--myFlag=true"</code> */
117  template <>
118  void paramStringToVal<bool>(std::string const & valstring, bool & result);
119 
120  //! Machine-readable conversion of string to a string, for use in jevois::Parameter
121  /*! This implementation just returns the original string. */
122  template <>
123  void paramValToString<std::string>(std::string const & val, std::string & result);
124 
125  //! Machine-readable conversion from string to string, for use in jevois::Parameter
126  /*! This implementation just returns the original string. */
127  template <>
128  void paramStringToVal<std::string>(std::string const & valstring, std::string & result);
129 
130  /*! @} */ // **********************************************************************
131 
132 } // namespace jevois
133 
134 //! Include implementation details of no interest to the user
136 
137 
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.
void paramStringToVal< bool >(std::string const &valstring, bool &result)
Machine-readable conversion from string to bool, for use in jevois::Parameter.