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