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 std::uint32_t() const { return value_->unsignedValue(); }
25 EitherType::operator std::uint64_t() const {
26  return value_->unsignedlongintValue();
27 }
28 EitherType::operator std::int32_t() const { return value_->intValue(); }
29 EitherType::operator std::int64_t() 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 std::uint32_t *)value_;
46  break;
47  case UNSIGNEDLONGINT:
48  delete (const std::uint64_t *)value_;
49  break;
50  case INT:
51  delete (const std::int32_t *)value_;
52  break;
53  case LONGINT:
54  delete (const std::int64_t *)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 
86 
87 Value::Value(const bool &value) : type_(BOOL), value_(new bool(value)) {}
88 Value::Value(const std::uint32_t &value)
89  : type_(UNSIGNED), value_(new std::uint32_t(value)) {}
90 Value::Value(const std::uint64_t &value)
91  : type_(UNSIGNEDLONGINT), value_(new std::uint64_t(value)) {}
92 Value::Value(const std::int32_t &value)
93  : type_(INT), value_(new std::int32_t(value)) {}
94 Value::Value(const std::int64_t &value)
95  : type_(LONGINT), value_(new int64_t(value)) {}
96 Value::Value(const float &value) : type_(FLOAT), value_(new float(value)) {}
97 Value::Value(const double &value) : type_(DOUBLE), value_(new double(value)) {}
98 Value::Value(const std::string &value)
99  : type_(STRING), value_(new std::string(value)) {}
100 Value::Value(const Vector &value) : type_(VECTOR), value_(new Vector(value)) {}
101 Value::Value(const Eigen::MatrixXd &value)
102  : type_(MATRIX), value_(new Eigen::MatrixXd(value)) {}
103 Value::Value(const Eigen::Matrix4d &value)
104  : type_(MATRIX4D), value_(new Eigen::Matrix4d(value)) {}
105 Value::Value(const Values &value) : type_(VALUES), value_(new Values(value)) {}
106 
107 Value::Value(const Value &value)
108  : type_(value.type_), value_(copyValue(value)) {}
109 
110 void *copyValue(const Value &value) {
111  void *copy;
112  switch (value.type()) {
113  case Value::NONE:
114  copy = NULL;
115  break;
116  case Value::BOOL:
117  copy = new bool(value.boolValue());
118  break;
119  case Value::UNSIGNED:
120  copy = new unsigned(value.unsignedValue());
121  break;
123  copy = new unsigned long int(value.unsignedlongintValue());
124  break;
125  case Value::INT:
126  copy = new int(value.intValue());
127  break;
128  case Value::LONGINT:
129  copy = new long int(value.longintValue());
130  break;
131  case Value::FLOAT:
132  copy = new float(value.floatValue());
133  break;
134  case Value::DOUBLE:
135  copy = new double(value.doubleValue());
136  break;
137  case Value::STRING:
138  copy = new std::string(value.stringValue());
139  break;
140  case Value::VECTOR:
141  copy = new Vector(value.vectorValue());
142  break;
143  case Value::MATRIX:
144  copy = new Eigen::MatrixXd(value.matrixXdValue());
145  break;
146  case Value::MATRIX4D:
147  copy = new Eigen::Matrix4d(value.matrix4dValue());
148  break;
149  case Value::VALUES:
150  copy = new Values(value.valuesValue());
151  break;
152  default:
153  abort();
154  }
155  return copy;
156 }
157 
158 Value::Value() : type_(NONE), value_(NULL) {}
159 
160 Value Value::operator=(const Value &value) {
161  if (&value != this) {
162  if (value_ != 0x0) deleteValue();
163  type_ = value.type_;
164  void **ptValue = const_cast<void **>(&value_);
165  *ptValue = copyValue(value);
166  }
167  return *this;
168 }
169 
170 bool Value::operator==(const Value &other) const {
171  if (type_ != other.type_) return false;
172  switch (type_) {
173  case Value::BOOL:
174  return boolValue() == other.boolValue();
175  case Value::UNSIGNED:
176  return unsignedValue() == other.unsignedValue();
178  return unsignedlongintValue() == other.unsignedlongintValue();
179  case Value::INT:
180  return intValue() == other.intValue();
181  case Value::DOUBLE:
182  return doubleValue() == other.doubleValue();
183  case Value::FLOAT:
184  return floatValue() == other.floatValue();
185  case Value::STRING:
186  return stringValue() == other.stringValue();
187  case Value::VECTOR:
188  return vectorValue() == other.vectorValue();
189  case Value::MATRIX:
190  return matrixXdValue() == other.matrixXdValue();
191  case Value::MATRIX4D:
192  return matrix4dValue() == other.matrix4dValue();
193  case Value::VALUES:
194  return constValuesValue() == other.constValuesValue();
195  case Value::NONE:
196  break;
197  default:
198  break;
199  }
200  return false;
201 }
202 
203 const EitherType Value::value() const { return EitherType(*this); }
204 
205 Value::Type Value::type() const { return type_; }
206 
207 bool Value::boolValue() const {
208  if (type_ == BOOL) return *((const bool *)value_);
209  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an bool");
210 }
211 
212 std::uint32_t Value::unsignedValue() const {
213  if (type_ == UNSIGNED) return *((const std::uint32_t *)value_);
215  "value is not an unsigned int");
216 }
217 
218 std::uint64_t Value::unsignedlongintValue() const {
219  if (type_ == UNSIGNEDLONGINT) return *((const std::uint64_t *)value_);
221  "value is not an unsigned long int");
222 }
223 
224 std::int64_t Value::longintValue() const {
225  if (type_ == LONGINT) return *((const std::int64_t *)value_);
226  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an long int");
227 }
228 
229 std::int32_t Value::intValue() const {
230  if (type_ == INT) return *((const std::int32_t *)value_);
231  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an int");
232 }
233 
234 float Value::floatValue() const {
235  float result;
236  if (FLOAT != type_)
237  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a float");
238  result = *((const float *)value_);
239  return result;
240 }
241 
242 double Value::doubleValue() const {
243  double result;
244  if (DOUBLE != type_)
245  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a double");
246  result = *((const double *)value_);
247  return result;
248 }
249 
250 std::string Value::stringValue() const {
251  if (type_ == STRING) return *((const std::string *)value_);
252  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an string");
253 }
254 
256  if (type_ == VECTOR) return *((const Vector *)value_);
257  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an vector");
258 }
259 
260 Eigen::MatrixXd Value::matrixXdValue() const {
261  if (type_ == MATRIX) return *((const Eigen::MatrixXd *)value_);
263  "value is not a Eigen matrixXd");
264 }
265 
266 Eigen::Matrix4d Value::matrix4dValue() const {
267  if (type_ == MATRIX4D) return *((const Eigen::Matrix4d *)value_);
269  "value is not a Eigen matrix4d");
270 }
271 
273  if (type_ == VALUES) return *((const Values *)value_);
275  "value is not a vector of Value");
276 }
277 
279  if (type_ == VALUES) return *((const Values *)value_);
281  "value is not a vector of Value");
282 }
283 
284 std::string Value::typeName(Type type) {
285  switch (type) {
286  case BOOL:
287  return std::string("bool");
288  case UNSIGNED:
289  return std::string("unsigned int");
290  case UNSIGNEDLONGINT:
291  return std::string("unsigned long int");
292  case INT:
293  return std::string("int");
294  case FLOAT:
295  return std::string("float");
296  case DOUBLE:
297  return std::string("double");
298  case STRING:
299  return std::string("string");
300  case VECTOR:
301  return std::string("vector");
302  case MATRIX:
303  return std::string("matrixXd");
304  case MATRIX4D:
305  return std::string("matrix4d");
306  case VALUES:
307  return std::string("values");
308  default:
309  return std::string("unknown");
310  }
311 }
312 
313 std::ostream &operator<<(std::ostream &os, const Value &value) {
314  os << "Type=" << Value::typeName(value.type_) << ", value=";
315  switch (value.type_) {
316  case Value::BOOL:
317  os << value.boolValue();
318  break;
319  case Value::UNSIGNED:
320  os << value.unsignedValue();
321  break;
323  os << value.unsignedlongintValue();
324  break;
325  case Value::INT:
326  os << value.intValue();
327  break;
328  case Value::DOUBLE:
329  os << value.doubleValue();
330  break;
331  case Value::FLOAT:
332  os << value.floatValue();
333  break;
334  case Value::STRING:
335  os << value.stringValue();
336  break;
337  case Value::VECTOR:
338  os << value.vectorValue();
339  break;
340  case Value::MATRIX:
341  os << value.matrixXdValue();
342  break;
343  case Value::MATRIX4D:
344  os << value.matrix4dValue();
345  break;
346  case Value::VALUES: {
347  const std::vector<Value> &vals = value.constValuesValue();
348  os << "[ ";
349  for (std::size_t i = 0; i < vals.size(); ++i)
350  os << "Value(" << vals[i] << "), ";
351  os << "]";
352  } break;
353  default:
354  return os;
355  }
356  return os;
357 }
358 
359 template <>
361 template <>
363 template <>
365 template <>
367 template <>
369 template <>
371 template <>
373 template <>
375 template <>
377 
378 template <>
380 template <>
382 template <>
384 
385 } // namespace command
386 } // namespace dynamicgraph
dynamicgraph::command::Value::type_
Type type_
Definition: value.h:128
Eigen
Definition: eigen-io.h:27
dynamicgraph::command::Value::longintValue
std::int64_t longintValue() const
Definition: src/command/value.cpp:224
dynamicgraph::command::ValueHelper
Definition: value.h:137
dynamicgraph::command::Value::MATRIX4D
@ MATRIX4D
Definition: value.h:65
dynamicgraph
dynamicgraph::command::Values
std::vector< Value > Values
Definition: value.h:22
dynamicgraph::command::Value::INT
@ INT
Definition: value.h:58
dynamicgraph::command::Value::~Value
~Value()
Definition: src/command/value.cpp:85
dynamicgraph::command::Value::matrix4dValue
Eigen::Matrix4d matrix4dValue() const
Definition: src/command/value.cpp:266
dynamicgraph::command::EitherType
Definition: value.h:25
dynamicgraph::command::Value::boolValue
bool boolValue() const
Definition: src/command/value.cpp:207
dynamicgraph::command::operator<<
std::ostream & operator<<(std::ostream &os, const Value &value)
Definition: src/command/value.cpp:313
dynamicgraph::command::Value::constValuesValue
const Values & constValuesValue() const
Definition: src/command/value.cpp:278
dynamicgraph::command::Value::unsignedlongintValue
std::uint64_t unsignedlongintValue() const
Definition: src/command/value.cpp:218
dynamicgraph::command::Value::VALUES
@ VALUES
Definition: value.h:66
dynamicgraph::command::Value::operator=
Value operator=(const Value &value)
Definition: src/command/value.cpp:160
dynamicgraph::command::Value::value_
const void *const value_
Definition: value.h:129
dynamicgraph::command::Value::STRING
@ STRING
Definition: value.h:62
dynamicgraph::command::Value::UNSIGNED
@ UNSIGNED
Definition: value.h:56
dynamicgraph::ExceptionAbstract::TOOLS
@ TOOLS
Definition: exception-abstract.h:72
dynamicgraph::ExceptionAbstract
Abstract root class for all dynamic-graph exceptions.
Definition: exception-abstract.h:31
dynamicgraph::command::Value::NONE
@ NONE
Definition: value.h:54
dynamicgraph::command::Value::vectorValue
Vector vectorValue() const
Definition: src/command/value.cpp:255
dynamicgraph::command::Value::BOOL
@ BOOL
Definition: value.h:55
dynamicgraph::command::Value::DOUBLE
@ DOUBLE
Definition: value.h:61
dynamicgraph::Vector
Eigen::VectorXd Vector
Definition: linear-algebra.h:14
dynamicgraph::command::Value::unsignedValue
std::uint32_t unsignedValue() const
Definition: src/command/value.cpp:212
dynamicgraph::command::Value::Value
Value()
Definition: src/command/value.cpp:158
dynamicgraph::command::Value::operator==
bool operator==(const Value &other) const
Definition: src/command/value.cpp:170
dynamicgraph::command::EitherType::EitherType
EitherType(const Value &value)
Definition: src/command/value.cpp:16
dynamicgraph::command::Value::stringValue
std::string stringValue() const
Definition: src/command/value.cpp:250
dynamicgraph::command::Value::floatValue
float floatValue() const
Definition: src/command/value.cpp:234
dynamicgraph::command::Value::value
const EitherType value() const
Definition: src/command/value.cpp:203
dynamicgraph::command::Value::VECTOR
@ VECTOR
Definition: value.h:63
dynamicgraph::command::Value::doubleValue
double doubleValue() const
Definition: src/command/value.cpp:242
dynamicgraph::command::EitherType::value_
const Value * value_
Definition: value.h:43
dynamicgraph::command::Value::MATRIX
@ MATRIX
Definition: value.h:64
dynamicgraph::command::Value::deleteValue
void deleteValue()
Definition: src/command/value.cpp:39
dynamicgraph::command::Value::EitherType
friend class EitherType
Definition: value.h:114
dynamicgraph::command::Value::LONGINT
@ LONGINT
Definition: value.h:59
dynamicgraph::command::Value::Type
Type
Definition: value.h:53
dynamicgraph::command::Value::valuesValue
Values valuesValue() const
Definition: src/command/value.cpp:272
dynamicgraph::command::Value::type
Type type() const
Return the type of the value.
Definition: src/command/value.cpp:205
dynamicgraph::command::Value::typeName
static std::string typeName(Type type)
Return the name of the type.
Definition: src/command/value.cpp:284
dynamicgraph::command::Value::FLOAT
@ FLOAT
Definition: value.h:60
dynamicgraph::command::Value::UNSIGNEDLONGINT
@ UNSIGNEDLONGINT
Definition: value.h:57
dynamicgraph::command::copyValue
static void * copyValue(const Value &value)
Definition: src/command/value.cpp:110
dynamicgraph::command::Value::intValue
std::int32_t intValue() const
Definition: src/command/value.cpp:229
dynamicgraph::command::Value
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:51
value.h
dynamicgraph::command::EitherType::~EitherType
~EitherType()
Definition: src/command/value.cpp:18
dynamicgraph::command::Value::matrixXdValue
Eigen::MatrixXd matrixXdValue() const
Definition: src/command/value.cpp:260
exception-abstract.h


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Oct 22 2023 02:27:08