JeVois  1.1
JeVois Smart Embedded Machine Vision Toolkit
ParameterDefImpl.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 
23 #include <jevois/Util/Demangle.H>
24 #include <sstream>
25 
26 namespace jevois
27 {
28  namespace detail
29  {
30  // ######################################################################
31  //! Conversions to/from string with exception wrapping
32  template <class T> inline
33  T const paramValFromString(std::string const & valstring, jevois::ParameterBase const * param)
34  {
35  T val;
36  try
37  {
38  jevois::paramStringToVal(valstring, val);
39  }
40  catch (std::exception & e)
41  {
42  throw std::range_error(param->descriptor() + ": Failed to convert [" + valstring + "] to type [" +
43  jevois::demangledName<T>() + "]: " + e.what());
44  }
45  return val;
46  }
47 
48  // ######################################################################
49  template <class T> inline
50  std::string const paramStringFromVal(T const & val, jevois::ParameterBase const * param)
51  {
52  std::string valstring;
53  try
54  {
55  jevois::paramValToString(val, valstring);
56  }
57  catch (std::exception & e)
58  {
59  throw std::range_error(param->descriptor() + ": Failed to convert value of type [" +
60  jevois::demangledName<T>() + "] to string: " + e.what());
61  }
62  return valstring;
63  }
64  } // namespace detail
65 } // namespace jevois
66 
67 // ######################################################################
68 // ######################################################################
69 // ######################################################################
70 template <class T> inline
71 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
72  jevois::ParameterCategory const & category) :
73  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
74  itsValidValuesSpec(new jevois::ValidValuesSpecNone<T>())
75 { }
76 
77 // ######################################################################
78 template <class T> template <template <typename> class ValidValuesSpec> inline
79 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
80  ValidValuesSpec<T> const & validValuesSpec,
81  jevois::ParameterCategory const & category) :
82  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
83  itsValidValuesSpec(new ValidValuesSpec<T>(validValuesSpec))
84 {
85  checkDefaultValue();
86 }
87 
88 // ######################################################################
89 template <class T> inline
90 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
91  std::vector<T> const & validvalues, jevois::ParameterCategory const & category) :
92  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
93  itsValidValuesSpec(new jevois::ValidValuesSpecList<T>(validvalues))
94 {
95  checkDefaultValue();
96 }
97 
98 // ######################################################################
99 template <class T> inline
100 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
101  jevois::Range<T> const & validrange, jevois::ParameterCategory const & category) :
102  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
103  itsValidValuesSpec(new jevois::ValidValuesSpecRange<T>(validrange))
104 {
105  checkDefaultValue();
106 }
107 
108 // ######################################################################
109 template <class T> inline
110 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
111  jevois::StepRange<T> const & validrange,
112  jevois::ParameterCategory const & category) :
113  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
114  itsValidValuesSpec(new jevois::ValidValuesSpecStepRange<T>(validrange))
115 {
116  checkDefaultValue();
117 }
118 
119 // ######################################################################
120 template <class T> inline
121 jevois::ParameterDef<T>::ParameterDef(std::string const & name, std::string const & description, T const & defaultValue,
122  boost::regex const & validregex, jevois::ParameterCategory const & category) :
123  ParameterDefBase(name, description, category), itsDefaultValue(defaultValue),
124  itsValidValuesSpec(new jevois::ValidValuesSpecRegex<T>(validregex))
125 {
126  checkDefaultValue();
127 }
128 
129 // ######################################################################
130 template <class T> inline
132 {
133  // Enforce that the default value is one of the valid ones:
134  if (itsValidValuesSpec->checkValueValidity(itsDefaultValue) == false)
135  {
136  // ooops, given default value is not valid, let's throw:
137  std::ostringstream os;
138  os << "Provided default value [" << itsDefaultValue << "] for ParameterDef [" << itsName
139  << "] not valid according to specification " << itsValidValuesSpec->str();
140  throw std::range_error(os.str());
141  }
142 }
143 
144 // ######################################################################
145 template <class T> inline
147 { return itsDefaultValue; }
148 
149 // ######################################################################
150 template <class T> inline
152 { return jevois::detail::paramStringFromVal(itsDefaultValue, nullptr); }
153 
154 // ######################################################################
155 template <class T> inline
157 { return itsValidValuesSpec->str(); }
158 
159 
void paramStringToVal(std::string const &valstring, T &result)
Machine-readable conversion from string to T, for use in jevois::Parameter.
ParameterDef(std::string const &name, std::string const &description, T const &defaultValue, ParameterCategory const &category)
Construct a ParameterDef with no given valid values, valid values are whatever T can take...
std::string const paramStringFromVal(T const &val, jevois::ParameterBase const *param)
T const defaultValue() const
Get the default value that was specified for this Parameter definition.
A generic range class with a step.
Definition: StepRange.H:69
virtual std::string const defaultValueString() const
Default value in string representation.
A Parameter Definition.
Definition: ParameterDef.H:88
A category to which multiple ParameterDef definitions can belong.
Definition: ParameterDef.H:33
void paramValToString(T const &val, std::string &result)
Machine-readable conversion of T to a string, for use in jevois::Parameter.
Range-based valid values spec, bounds are included.
virtual std::string descriptor() const =0
Get the parameter fully-qualified name, aka descriptor, including names of owning Component and all p...
Base class for a Parameter definition.
Definition: ParameterDef.H:47
Finite list valid values spec, everything listed at construction is valid, anything else is not...
Regex-based valid values spec, everything that is a match to the regex is considered valid...
Base class for Parameter.
Definition: Parameter.H:121
Open/None valid values spec, anything that T can take is valid.
StepRange-based valid values spec, bounds are included.
A generic range class.
Definition: Range.H:80
T const paramValFromString(std::string const &valstring, jevois::ParameterBase const *param)
Conversions to/from string with exception wrapping.
virtual std::string const validValuesString() const
Valid values in string representation.