iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterDefImpl.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_PARAMETERDEFIMPL_H
37 #define INCLUDE_NRT_CORE_MODEL_DETAILS_PARAMETERDEFIMPL_H
38 
42 #include <sstream>
43 
44 namespace nrt
45 {
46  class ParameterBase;
47 
48  namespace detail
49  {
50  // ######################################################################
51  //! Conversions to/from string with exception wrapping
52  template <class T> inline
53  T const paramValFromString(std::string const & valstring, nrt::ParameterBase const * param)
54  {
55  T val;
56  try { nrt::paramStringToVal(valstring, val); }
57  catch (boost::bad_lexical_cast & e) {
58  throw nrt::exception::
59  ParameterException(nrt::exception::ParameterException::InvalidValue, param,
60  "Failed to convert [" + valstring + "] to value of type [" + nrt::demangledName<T>() +
61  "]: " + e.what());
62  }
63  catch (...) {
64  throw nrt::exception::
65  ParameterException(nrt::exception::ParameterException::InvalidValue, param,
66  "Failed to convert [" + valstring + "] to value of type [" + nrt::demangledName<T>()+']');
67  }
68  return val;
69  }
70 
71  // ######################################################################
72  template <class T> inline
73  std::string const paramStringFromVal(T const & val, nrt::ParameterBase const * param)
74  {
75  std::string valstring;
76  try { nrt::paramValToString(val, valstring); }
77  catch (std::exception & e) {
78  throw nrt::exception::
79  ParameterException(nrt::exception::ParameterException::InvalidValue, param,
80  "Failed to convert value of type [" + nrt::demangledName<T>() +
81  "] to string: " + e.what());
82  }
83  catch (...) {
84  throw nrt::exception::
85  ParameterException(nrt::exception::ParameterException::InvalidValue, param,
86  "Failed to convert value of type [" + nrt::demangledName<T>() + "] to string");
87  }
88  return valstring;
89  }
90  } // namespace detail
91 } // namespace nrt
92 
93 // ######################################################################
94 // ######################################################################
95 // ######################################################################
96 constexpr nrt::ParameterFlags nrt::operator|(nrt::ParameterFlags f, nrt::ParameterFlags g)
97 { return static_cast<nrt::ParameterFlags>(static_cast<int>(f) | static_cast<int>(g)); }
98 
99 // ######################################################################
100 constexpr nrt::ParameterFlags nrt::operator&(nrt::ParameterFlags f, nrt::ParameterFlags g)
101 { return static_cast<nrt::ParameterFlags>(static_cast<int>(f) & static_cast<int>(g)); }
102 
103 // ######################################################################
104 // ######################################################################
105 // ######################################################################
106 template <class T> inline
107 nrt::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
108  nrt::ParameterCategory const & category, nrt::ParameterFlags const pflags) :
109  ParameterDefBase(name, description, category, pflags), itsDefaultValue(defaultValue),
110  itsValidValuesSpec(new nrt::ValidValuesSpecNone<T>())
111 { }
112 
113 // ######################################################################
114 template <class T> template <template <typename> class ValidValuesSpec> inline
115 nrt::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
116  ValidValuesSpec<T> const & validValuesSpec,
117  nrt::ParameterCategory const & category, nrt::ParameterFlags const pflags) :
118  ParameterDefBase(name, description, category, pflags), itsDefaultValue(defaultValue),
119  itsValidValuesSpec(new ValidValuesSpec<T>(validValuesSpec))
120 {
121  checkDefaultValue();
122 }
123 
124 // ######################################################################
125 template <class T> inline
126 nrt::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
127  std::vector<T> const & validvalues, nrt::ParameterCategory const & category,
128  nrt::ParameterFlags const pflags) :
129  ParameterDefBase(name, description, category, pflags), itsDefaultValue(defaultValue),
130  itsValidValuesSpec(new nrt::ValidValuesSpecList<T>(validvalues))
131 {
132  checkDefaultValue();
133 }
134 
135 // ######################################################################
136 template <class T> inline
137 nrt::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
138  nrt::Range<T> const & validrange, nrt::ParameterCategory const & category,
139  nrt::ParameterFlags const pflags) :
140  ParameterDefBase(name, description, category, pflags), itsDefaultValue(defaultValue),
141  itsValidValuesSpec(new nrt::ValidValuesSpecRange<T>(validrange))
142 {
143  checkDefaultValue();
144 }
145 
146 // ######################################################################
147 template <class T> inline
148 nrt::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
149  boost::regex const & validregex, nrt::ParameterCategory const & category,
150  nrt::ParameterFlags const pflags) :
151  ParameterDefBase(name, description, category, pflags), itsDefaultValue(defaultValue),
152  itsValidValuesSpec(new nrt::ValidValuesSpecRegex<T>(validregex))
153 {
154  checkDefaultValue();
155 }
156 
157 // ######################################################################
158 template <class T> inline
160 {
161  // Enforce that the default value is one of the valid ones:
162  if (itsValidValuesSpec->checkValueValidity(itsDefaultValue) == false)
163  {
164  // ooops, given default value is not valid, let's throw:
165  std::ostringstream os;
166  os << "Provided default value [" << itsDefaultValue << "] for ParameterDef [" << itsName
167  << "] not valid according to specification " << itsValidValuesSpec->str();
168 
169  throw nrt::exception::
170  ParameterException(nrt::exception::ParameterException::InvalidDefaultValue, nullptr, os.str());
171  }
172 }
173 
174 // ######################################################################
175 template <class T> inline
177 { return itsDefaultValue; }
178 
179 // ######################################################################
180 template <class T> inline
182 { return nrt::detail::paramStringFromVal(itsDefaultValue, nullptr); }
183 
184 // ######################################################################
185 template <class T> inline
187 { return itsValidValuesSpec->str(); }
188 
189 #endif // INCLUDE_NRT_CORE_MODEL_DETAILS_PARAMETERDEFIMPL_H
190