libvisiontransfer  10.6.0
parameterinfo.cpp
1 #include "visiontransfer/parameterinfo.h"
2 #include "visiontransfer/exceptions.h"
3 
4 namespace visiontransfer {
5 
6 class ParameterInfo::Pimpl {
7 public:
8  Pimpl(): type(ParameterInfo::TYPE_INT), value({0}), min({0}), max({0}), inc({0}) { }
9  template<typename T> void set(const std::string& name, bool writeable,
10  T value, T min, T max, T inc);
11  inline std::string getName() const { return name; }
12  inline ParameterType getType() const { return type; }
13  inline bool isWriteable() const { return writeable; }
14  template<typename T> T getTypedValue(const ParameterValue& val) const;
15  template<typename T> T getValue() const { return getTypedValue<T>(value); }
16  template<typename T> T getMin() const { return getTypedValue<T>(min); }
17  template<typename T> T getMax() const { return getTypedValue<T>(max); }
18  template<typename T> T getInc() const { return getTypedValue<T>(inc); }
19 private:
20  std::string name;
21  ParameterType type;
22  bool writeable;
23  ParameterValue value;
24  ParameterValue min;
25  ParameterValue max;
26  ParameterValue inc;
27 };
28 
29 // ParameterInfo, for abstracted enumerations of parameters
30 
31 ParameterInfo::ParameterInfo()
32 {
33  pimpl = new ParameterInfo::Pimpl();
34 }
35 
36 template<> void ParameterInfo::Pimpl::set(const std::string& name, bool writeable, int value, int min, int max, int inc)
37 {
38  this->name = name;
39  this->type = ParameterInfo::TYPE_INT;
40  this->writeable = writeable;
41  this->value.intVal = value;
42  this->min.intVal = min;
43  this->max.intVal = max;
44  this->inc.intVal = inc;
45 }
46 template<> void ParameterInfo::Pimpl::set(const std::string& name, bool writeable, double value, double min, double max, double inc)
47 {
48  this->name = name;
49  this->type = ParameterInfo::TYPE_DOUBLE;
50  this->writeable = writeable;
51  this->value.doubleVal = value;
52  this->min.doubleVal = min;
53  this->max.doubleVal = max;
54  this->inc.doubleVal = inc;
55 }
56 template<> void ParameterInfo::Pimpl::set(const std::string& name, bool writeable, bool value, bool min, bool max, bool inc)
57 {
58  this->name = name;
59  this->type = ParameterInfo::TYPE_BOOL;
60  this->writeable = writeable;
61  this->value.boolVal = value;
62  this->min.boolVal = min;
63  this->max.boolVal = max;
64  this->inc.boolVal = inc;
65 }
66 
67 ParameterInfo ParameterInfo::fromInt(const std::string& name, bool writeable,
68  int value, int min, int max, int inc) {
69  ParameterInfo pi;
70  pi.pimpl->set<int>(name, writeable, value, min, max, inc);
71  return pi;
72 }
73 
74 ParameterInfo ParameterInfo::fromDouble(const std::string& name, bool writeable,
75  double value, double min, double max, double inc) {
76  ParameterInfo pi;
77  pi.pimpl->set<double>(name, writeable, value, min, max, inc);
78  return pi;
79 }
80 
81 ParameterInfo ParameterInfo::fromBool(const std::string& name, bool writeable, bool value) {
82  ParameterInfo pi;
83  pi.pimpl->set<bool>(name, writeable, value, 0, 1, 1);
84  return pi;
85 }
86 
87 #ifndef DOXYGEN_SHOULD_SKIP_THIS
88 template<> int ParameterInfo::Pimpl::getTypedValue(const ParameterInfo::ParameterValue& val) const {
89  switch (type) {
90  case ParameterInfo::TYPE_INT: {
91  return val.intVal;
92  }
93  case ParameterInfo::TYPE_BOOL: {
94  return (int) val.boolVal;
95  }
96  case ParameterInfo::TYPE_DOUBLE: {
97  return (int) val.doubleVal;
98  }
99  }
100  throw ParameterException("Unexpected parameter type");
101 }
102 
103 template<> double ParameterInfo::Pimpl::getTypedValue(const ParameterInfo::ParameterValue& val) const {
104  switch (type) {
105  case ParameterInfo::TYPE_DOUBLE: {
106  return val.doubleVal;
107  }
108  case ParameterInfo::TYPE_INT: {
109  return (double) val.intVal;
110  }
111  case ParameterInfo::TYPE_BOOL: {
112  return val.boolVal?1.0:0.0;
113  }
114  }
115  throw ParameterException("Unexpected parameter type");
116 }
117 
118 template<> bool ParameterInfo::Pimpl::getTypedValue(const ParameterInfo::ParameterValue& val) const {
119  switch (type) {
120  case ParameterInfo::TYPE_BOOL: {
121  return val.boolVal;
122  }
123  case ParameterInfo::TYPE_DOUBLE: {
124  return val.doubleVal != 0.0;
125  }
126  case ParameterInfo::TYPE_INT: {
127  return val.intVal != 0;
128  }
129  }
130  throw ParameterException("Unexpected parameter type");
131 }
132 #endif // DOXYGEN_SHOULD_SKIP_THIS
133 
134 std::string ParameterInfo::getName() const { return pimpl->getName(); }
135 ParameterInfo::ParameterType ParameterInfo::getType() const { return pimpl->getType(); }
136 bool ParameterInfo::isWriteable() const { return pimpl->isWriteable(); }
137 
138 #ifndef DOXYGEN_SHOULD_SKIP_THIS
139 template<> VT_EXPORT int ParameterInfo::getValue() const { return pimpl->getValue<int>(); }
140 template<> VT_EXPORT double ParameterInfo::getValue() const { return pimpl->getValue<double>(); }
141 template<> VT_EXPORT bool ParameterInfo::getValue() const { return pimpl->getValue<bool>(); }
142 template<> VT_EXPORT int ParameterInfo::getMin() const { return pimpl->getMin<int>(); }
143 template<> VT_EXPORT double ParameterInfo::getMin() const { return pimpl->getMin<double>(); }
144 template<> VT_EXPORT bool ParameterInfo::getMin() const { return pimpl->getMin<bool>(); }
145 template<> VT_EXPORT int ParameterInfo::getMax() const { return pimpl->getMax<int>(); }
146 template<> VT_EXPORT double ParameterInfo::getMax() const { return pimpl->getMax<double>(); }
147 template<> VT_EXPORT bool ParameterInfo::getMax() const { return pimpl->getMax<bool>(); }
148 template<> VT_EXPORT int ParameterInfo::getInc() const { return pimpl->getInc<int>(); }
149 template<> VT_EXPORT double ParameterInfo::getInc() const { return pimpl->getInc<double>(); }
150 template<> VT_EXPORT bool ParameterInfo::getInc() const { return pimpl->getInc<bool>(); }
151 #endif // DOXYGEN_SHOULD_SKIP_THIS
152 
153 } // namespace
154 
visiontransfer::ParameterInfo::getMin
T getMin() const
Returns the minimum parameter value, cast to the desired type (int, double or bool)
visiontransfer::ParameterInfo::getValue
T getValue() const
Returns the current parameter value, cast to the desired type (int, double or bool)
visiontransfer::ParameterInfo::getName
std::string getName() const
Returns the string representation of the parameter name.
Definition: parameterinfo.cpp:134
visiontransfer::ParameterInfo::isWriteable
bool isWriteable() const
Returns whether the parameter is writeable (or read-only)
Definition: parameterinfo.cpp:136
visiontransfer::ParameterInfo::getMax
T getMax() const
Returns the maximum parameter value, cast to the desired type (int, double or bool)
visiontransfer::ParameterInfo::getType
ParameterType getType() const
Returns the type of the parameter.
Definition: parameterinfo.cpp:135
visiontransfer::ParameterInfo::getInc
T getInc() const
Returns the increment of the parameter (i.e. increment for raising / lowering the value),...
Allied Vision