iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterStringConversionImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_MODEL_DETAILS_PARAMETERSTRINGCONVERSIONIMPL_H
37 #define INCLUDE_NRT_CORE_MODEL_DETAILS_PARAMETERSTRINGCONVERSIONIMPL_H
38 
39 #include <boost/lexical_cast.hpp>
40 #include <boost/algorithm/string.hpp>
42 
43 // ######################################################################
44 template <class T> inline
45 void nrt::paramValToString(T const & val, std::string & result)
46 {
47  result = boost::lexical_cast<std::string>(val);
48 }
49 
50 // ######################################################################
51 template <class T> inline
52 void nrt::paramStringToVal(std::string const & valstring, T & result)
53 {
54  result = boost::lexical_cast<T>(valstring);
55 }
56 
57 // ######################################################################
58 template <class Q> inline
59 void nrt::paramValToString(std::vector<Q> const & val, std::string & result)
60 {
61  std::stringstream s; s << val; result = s.str();
62 }
63 
64 // ######################################################################
65 template <class Q> inline
66 void nrt::paramStringToVal(std::string const & valstring, std::vector<Q> & result)
67 {
68  std::stringstream s(valstring); s >> result;
69 }
70 
71 // ######################################################################
72 template <class K, class V> inline
73 void nrt::paramValToString(std::map<K,V> const & val, std::string & result)
74 {
75  std::stringstream s; s << val; result = s.str();
76 }
77 
78 // ######################################################################
79 template <class K, class V> inline
80 void nrt::paramStringToVal(std::string const & valstring, std::map<K,V> & result)
81 {
82  std::stringstream s(valstring); s >> result;
83 }
84 
85 // ######################################################################
86 template <> inline
87 void nrt::paramValToString<bool>(bool const & val, std::string & result)
88 {
89  if (val) result = "true"; else result = "false";
90 }
91 
92 // ######################################################################
93 template <> inline
94 void nrt::paramStringToVal<bool>(std::string const & valstring, bool & result)
95 {
96  std::string vs = valstring; boost::to_lower(vs);
97 
98  if (vs == "" || vs == "true" || vs == "t" || vs == "yes" || vs == "y" || vs == "1") result = true;
99  else if (vs == "false" || vs == "f" || vs == "no" || vs == "n" || vs == "0") result = false;
100  else result = boost::lexical_cast<bool>(valstring);
101 }
102 
103 // ######################################################################
104 template <> inline
105 void nrt::paramValToString<std::string>(std::string const & val, std::string & result)
106 { result = val; }
107 
108 // ######################################################################
109 template <> inline
110 void nrt::paramStringToVal<std::string>(std::string const & valstring, std::string & result)
111 { result = valstring; }
112 
113 #ifdef NRT_HAVE_EIGEN3
114 
115 // ######################################################################
116 template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
117 void nrt::paramValToString(Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> const & val,
118  std::string & result)
119 {
120  std::stringstream s; s << val; result = s.str();
121  for (char & c : result) if (c == '\n') c = ',';
122 }
123 
124 // ######################################################################
125 template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> inline
126 void nrt::paramStringToVal(std::string const & valstring,
127  Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & result)
128 {
129  std::vector<std::string> strvec;
130  boost::split(strvec, valstring, boost::is_any_of(", \n\r\t"), boost::token_compress_on);
131  if (strvec.size() != size_t(result.rows() * result.cols()))
132  throw nrt::exception::BadParameter("Input string's number of elements does not match matrix size");
133 
134  _Scalar * ptr = result.data();
135  for (std::string const & v : strvec) *ptr++ = boost::lexical_cast<_Scalar>(v);
136 }
137 
138 #endif // NRT_HAVE_EIGEN3
139 
140 #endif // INCLUDE_NRT_CORE_MODEL_DETAILS_PARAMETERSTRINGCONVERSIONIMPL_H
141