JeVois  1.2
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  template <typename Q>
57  void paramValToString(std::vector<Q> const & val, std::string & result);
58 
59  //! Machine-readable input from a string, for use in jevois::Parameter: reads [\c Element1, \c Element2, ...]
60  /*! Specialization for std::vector<T> which supports nesting of other vectors and vectors of other types */
61  template <typename Q>
62  void paramStringToVal(std::string const & valstring, std::vector<Q> & result);
63 
64  //! Machine-readable output to a string, for use in jevois::Parameter: outputs \c first \c second
65  template <typename F, typename S>
66  void paramValToString(std::pair<F, S> const & val, std::string & result);
67 
68  //! Machine-readable input from a string, for use in jevois::Parameter: reads \c first \c second
69  template <typename F, typename S>
70  void paramStringToVal(std::string const & valstring, std::pair<F, S> & result);
71 
72  //! Machine-readable output to a string, for use in jevois::Parameter: outputs [\c Key1:Value1, \c Key2:Value2, ...]
73  /*! Specialization for std::map which supports nesting of other maps/vectors */
74  template <typename K, typename V>
75  void paramValToString(std::map<K,V> const & val, std::string & result);
76 
77  //! Machine-readable input from a string, for use in jevois::Parameter: reads [\c Key1:Value1, \c Key2:Value2, ...]
78  /*! Specialization for std::map which supports nesting of other maps/vectors */
79  template <typename K, typename V>
80  void paramStringToVal(std::string const & valstring, std::map<K,V> & result);
81 
82  //! Machine-readable conversion of bool to a string, for use in jevois::Parameter
83  /*! @returns "true" or "false". */
84  template <>
85  void paramValToString<bool>(bool const & val, std::string & result);
86 
87  //! Machine-readable conversion from string to bool, for use in jevois::Parameter
88  /*! @throws jevois::exception::Exception if input string is malformed.
89 
90  Specialization for bool so that we can accept nice verbose terms rather than just 0 and 1. Acceptable ways to set
91  a bool Parameter are:
92 
93  - "true" or "false"
94  - "t" or "f"
95  - "yes" or "no"
96  - "y" or "n"
97  - 1 or 0
98  - An empty string will set the Parameter to true, so you may use boolean parameters as switches, e.g.,
99  <code>"--myFlag"</code> is equivalent to <code>"--myFlag=true"</code> */
100  template <>
101  void paramStringToVal<bool>(std::string const & valstring, bool & result);
102 
103  //! Machine-readable conversion of string to a string, for use in jevois::Parameter
104  /*! This implementation just returns the original string. */
105  template <>
106  void paramValToString<std::string>(std::string const & val, std::string & result);
107 
108  //! Machine-readable conversion from string to string, for use in jevois::Parameter
109  /*! This implementation just returns the original string. */
110  template <>
111  void paramStringToVal<std::string>(std::string const & valstring, std::string & result);
112 
113  /*! @} */ // **********************************************************************
114 
115 } // namespace jevois
116 
117 //! Include implementation details of no interest to the user
119 
120 
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.