src/command/value.cpp
Go to the documentation of this file.
1 //
2 // Copyright 2010 CNRS
3 //
4 // Author: Florent Lamiraux
5 //
6 
7 #include "dynamic-graph/value.h"
8 
10 
11 namespace dynamicgraph {
12 namespace command {
13 
14 static void *copyValue(const Value &value);
15 
16 EitherType::EitherType(const Value &value) : value_(new Value(value)) {}
17 
19  delete value_;
20  value_ = NULL;
21 }
22 
23 EitherType::operator bool() const { return value_->boolValue(); }
24 EitherType::operator unsigned() const { return value_->unsignedValue(); }
25 EitherType::operator unsigned long int() const {
26  return value_->unsignedlongintValue();
27 }
28 EitherType::operator int() const { return value_->intValue(); }
29 EitherType::operator long int() const { return value_->longintValue(); }
30 EitherType::operator float() const { return value_->floatValue(); }
31 EitherType::operator double() const { return value_->doubleValue(); }
32 EitherType::operator std::string() const { return value_->stringValue(); }
33 EitherType::operator Vector() const { return value_->vectorValue(); }
34 EitherType::operator Eigen::MatrixXd() const { return value_->matrixXdValue(); }
35 
36 EitherType::operator Eigen::Matrix4d() const { return value_->matrix4dValue(); }
37 EitherType::operator Values() const { return value_->valuesValue(); }
38 
40  switch (type_) {
41  case BOOL:
42  delete (const bool *)value_;
43  break;
44  case UNSIGNED:
45  delete (const unsigned *)value_;
46  break;
47  case UNSIGNEDLONGINT:
48  delete (const unsigned long int *)value_;
49  break;
50  case INT:
51  delete (const int *)value_;
52  break;
53  case LONGINT:
54  delete (const long int *)value_;
55  break;
56  case FLOAT:
57  delete (const float *)value_;
58  break;
59  case DOUBLE:
60  delete (const double *)value_;
61  break;
62  case STRING:
63  delete (const std::string *)value_;
64  break;
65  case VECTOR:
66  delete (const Vector *)value_;
67  break;
68  case MATRIX:
69  delete (const Eigen::MatrixXd *)value_;
70  break;
71  case MATRIX4D:
72  delete (const Eigen::Matrix4d *)value_;
73  break;
74  case VALUES:
75  delete (const Values *)value_;
76  break;
77  case NONE: /* Equivalent to void */
78  break;
79  default:
80  throw "Value::deleteValue : Undefined type";
81  ;
82  }
83 }
84 
85 Value::~Value() { deleteValue(); }
86 
87 Value::Value(const bool &value) : type_(BOOL), value_(new bool(value)) {}
88 Value::Value(const unsigned &value)
89  : type_(UNSIGNED), value_(new unsigned(value)) {}
90 Value::Value(const unsigned long int &value)
91  : type_(UNSIGNEDLONGINT), value_(new unsigned long int(value)) {}
92 Value::Value(const int &value) : type_(INT), value_(new int(value)) {}
93 Value::Value(const float &value) : type_(FLOAT), value_(new float(value)) {}
94 Value::Value(const double &value) : type_(DOUBLE), value_(new double(value)) {}
95 Value::Value(const std::string &value)
96  : type_(STRING), value_(new std::string(value)) {}
97 Value::Value(const Vector &value) : type_(VECTOR), value_(new Vector(value)) {}
98 Value::Value(const Eigen::MatrixXd &value)
99  : type_(MATRIX), value_(new Eigen::MatrixXd(value)) {}
100 Value::Value(const Eigen::Matrix4d &value)
101  : type_(MATRIX4D), value_(new Eigen::Matrix4d(value)) {}
102 Value::Value(const Values &value) : type_(VALUES), value_(new Values(value)) {}
103 
105  : type_(value.type_), value_(copyValue(value)) {}
106 
107 void *copyValue(const Value &value) {
108  void *copy;
109  switch (value.type()) {
110  case Value::NONE:
111  copy = NULL;
112  break;
113  case Value::BOOL:
114  copy = new bool(value.boolValue());
115  break;
116  case Value::UNSIGNED:
117  copy = new unsigned(value.unsignedValue());
118  break;
120  copy = new unsigned long int(value.unsignedlongintValue());
121  break;
122  case Value::INT:
123  copy = new int(value.intValue());
124  break;
125  case Value::LONGINT:
126  copy = new long int(value.longintValue());
127  break;
128  case Value::FLOAT:
129  copy = new float(value.floatValue());
130  break;
131  case Value::DOUBLE:
132  copy = new double(value.doubleValue());
133  break;
134  case Value::STRING:
135  copy = new std::string(value.stringValue());
136  break;
137  case Value::VECTOR:
138  copy = new Vector(value.vectorValue());
139  break;
140  case Value::MATRIX:
141  copy = new Eigen::MatrixXd(value.matrixXdValue());
142  break;
143  case Value::MATRIX4D:
144  copy = new Eigen::Matrix4d(value.matrix4dValue());
145  break;
146  case Value::VALUES:
147  copy = new Values(value.valuesValue());
148  break;
149  default:
150  abort();
151  }
152  return copy;
153 }
154 
156 
158  if (&value != this) {
159  if (value_ != 0x0) deleteValue();
160  type_ = value.type_;
161  void **ptValue = const_cast<void **>(&value_);
162  *ptValue = copyValue(value);
163  }
164  return *this;
165 }
166 
167 bool Value::operator==(const Value &other) const {
168  if (type_ != other.type_) return false;
169  switch (type_) {
170  case Value::BOOL:
171  return boolValue() == other.boolValue();
172  case Value::UNSIGNED:
173  return unsignedValue() == other.unsignedValue();
175  return unsignedlongintValue() == other.unsignedlongintValue();
176  case Value::INT:
177  return intValue() == other.intValue();
178  case Value::DOUBLE:
179  return doubleValue() == other.doubleValue();
180  case Value::FLOAT:
181  return floatValue() == other.floatValue();
182  case Value::STRING:
183  return stringValue() == other.stringValue();
184  case Value::VECTOR:
185  return vectorValue() == other.vectorValue();
186  case Value::MATRIX:
187  return matrixXdValue() == other.matrixXdValue();
188  case Value::MATRIX4D:
189  return matrix4dValue() == other.matrix4dValue();
190  case Value::VALUES:
191  return constValuesValue() == other.constValuesValue();
192  case Value::NONE:
193  break;
194  default:
195  break;
196  }
197  return false;
198 }
199 
200 const EitherType Value::value() const { return EitherType(*this); }
201 
202 Value::Type Value::type() const { return type_; }
203 
204 bool Value::boolValue() const {
205  if (type_ == BOOL) return *((const bool *)value_);
206  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an bool");
207 }
208 
209 unsigned Value::unsignedValue() const {
210  if (type_ == UNSIGNED) return *((const unsigned *)value_);
212  "value is not an unsigned int");
213 }
214 
215 unsigned long int Value::unsignedlongintValue() const {
216  if (type_ == UNSIGNEDLONGINT) return *((const unsigned long int *)value_);
218  "value is not an unsigned long int");
219 }
220 
221 long int Value::longintValue() const {
222  if (type_ == LONGINT) return *((const long int *)value_);
223  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an long int");
224 }
225 
226 int Value::intValue() const {
227  if (type_ == INT) return *((const int *)value_);
228  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an int");
229 }
230 
231 float Value::floatValue() const {
232  float result;
233  if (FLOAT != type_)
234  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a float");
235  result = *((const float *)value_);
236  return result;
237 }
238 
239 double Value::doubleValue() const {
240  double result;
241  if (DOUBLE != type_)
242  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a double");
243  result = *((const double *)value_);
244  return result;
245 }
246 
247 std::string Value::stringValue() const {
248  if (type_ == STRING) return *((const std::string *)value_);
249  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an string");
250 }
251 
253  if (type_ == VECTOR) return *((const Vector *)value_);
254  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an vector");
255 }
256 
257 Eigen::MatrixXd Value::matrixXdValue() const {
258  if (type_ == MATRIX) return *((const Eigen::MatrixXd *)value_);
260  "value is not a Eigen matrixXd");
261 }
262 
263 Eigen::Matrix4d Value::matrix4dValue() const {
264  if (type_ == MATRIX4D) return *((const Eigen::Matrix4d *)value_);
266  "value is not a Eigen matrix4d");
267 }
268 
270  if (type_ == VALUES) return *((const Values *)value_);
272  "value is not a vector of Value");
273 }
274 
276  if (type_ == VALUES) return *((const Values *)value_);
278  "value is not a vector of Value");
279 }
280 
281 std::string Value::typeName(Type type) {
282  switch (type) {
283  case BOOL:
284  return std::string("bool");
285  case UNSIGNED:
286  return std::string("unsigned int");
287  case UNSIGNEDLONGINT:
288  return std::string("unsigned long int");
289  case INT:
290  return std::string("int");
291  case FLOAT:
292  return std::string("float");
293  case DOUBLE:
294  return std::string("double");
295  case STRING:
296  return std::string("string");
297  case VECTOR:
298  return std::string("vector");
299  case MATRIX:
300  return std::string("matrixXd");
301  case MATRIX4D:
302  return std::string("matrix4d");
303  case VALUES:
304  return std::string("values");
305  default:
306  return std::string("unknown");
307  }
308 }
309 
310 std::ostream &operator<<(std::ostream &os, const Value &value) {
311  os << "Type=" << Value::typeName(value.type_) << ", value=";
312  switch (value.type_) {
313  case Value::BOOL:
314  os << value.boolValue();
315  break;
316  case Value::UNSIGNED:
317  os << value.unsignedValue();
318  break;
320  os << value.unsignedlongintValue();
321  break;
322  case Value::INT:
323  os << value.intValue();
324  break;
325  case Value::DOUBLE:
326  os << value.doubleValue();
327  break;
328  case Value::FLOAT:
329  os << value.floatValue();
330  break;
331  case Value::STRING:
332  os << value.stringValue();
333  break;
334  case Value::VECTOR:
335  os << value.vectorValue();
336  break;
337  case Value::MATRIX:
338  os << value.matrixXdValue();
339  break;
340  case Value::MATRIX4D:
341  os << value.matrix4dValue();
342  break;
343  case Value::VALUES: {
344  const std::vector<Value> &vals = value.constValuesValue();
345  os << "[ ";
346  for (std::size_t i = 0; i < vals.size(); ++i)
347  os << "Value(" << vals[i] << "), ";
348  os << "]";
349  } break;
350  default:
351  return os;
352  }
353  return os;
354 }
355 
356 template <>
358 template <>
360 template <>
363 template <>
365 template <>
367 template <>
369 template <>
371 template <>
373 
374 template <>
376 template <>
378 template <>
380 
381 } // namespace command
382 } // namespace dynamicgraph
Eigen::VectorXd Vector
const void *const value_
Definition: value.h:128
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:50
const Values & constValuesValue() const
Definition: eigen-io.h:27
Eigen::Matrix4d matrix4dValue() const
unsigned long int unsignedlongintValue() const
Value operator=(const Value &value)
friend class EitherType
Definition: value.h:113
Abstract root class for all dynamic-graph exceptions.
DYNAMIC_GRAPH_DLLAPI friend std::ostream & operator<<(std::ostream &os, const Value &value)
Output in a stream.
bool operator==(const Value &other) const
static void * copyValue(const Value &value)
Type type() const
Return the type of the value.
std::vector< Value > Values
Definition: value.h:21
const EitherType value() const
Eigen::MatrixXd matrixXdValue() const
static std::string typeName(Type type)
Return the name of the type.


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03