GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/command/value.cpp Lines: 237 282 84.0 %
Date: 2023-03-15 12:04:10 Branches: 144 249 57.8 %

Line Branch Exec Source
1
//
2
// Copyright 2010 CNRS
3
//
4
// Author: Florent Lamiraux
5
//
6
7
#include "dynamic-graph/value.h"
8
9
#include "dynamic-graph/exception-abstract.h"
10
11
namespace dynamicgraph {
12
namespace command {
13
14
static void *copyValue(const Value &value);
15
16
15
EitherType::EitherType(const Value &value) : value_(new Value(value)) {}
17
18
30
EitherType::~EitherType() {
19
15
  delete value_;
20
15
  value_ = NULL;
21
15
}
22
23
1
EitherType::operator bool() const { return value_->boolValue(); }
24
1
EitherType::operator unsigned() const { return value_->unsignedValue(); }
25
EitherType::operator unsigned long int() const {
26
  return value_->unsignedlongintValue();
27
}
28
14
EitherType::operator int() const { return value_->intValue(); }
29
EitherType::operator long int() const { return value_->longintValue(); }
30
1
EitherType::operator float() const { return value_->floatValue(); }
31
2
EitherType::operator double() const { return value_->doubleValue(); }
32
1
EitherType::operator std::string() const { return value_->stringValue(); }
33
1
EitherType::operator Vector() const { return value_->vectorValue(); }
34
1
EitherType::operator Eigen::MatrixXd() const { return value_->matrixXdValue(); }
35
36
1
EitherType::operator Eigen::Matrix4d() const { return value_->matrix4dValue(); }
37
EitherType::operator Values() const { return value_->valuesValue(); }
38
39
91
void Value::deleteValue() {
40



91
  switch (type_) {
41
2
    case BOOL:
42
2
      delete (const bool *)value_;
43
2
      break;
44
2
    case UNSIGNED:
45
2
      delete (const unsigned *)value_;
46
2
      break;
47
    case UNSIGNEDLONGINT:
48
      delete (const unsigned long int *)value_;
49
      break;
50
41
    case INT:
51
41
      delete (const int *)value_;
52
41
      break;
53
    case LONGINT:
54
      delete (const long int *)value_;
55
      break;
56
2
    case FLOAT:
57
2
      delete (const float *)value_;
58
2
      break;
59
13
    case DOUBLE:
60
13
      delete (const double *)value_;
61
13
      break;
62
11
    case STRING:
63
11
      delete (const std::string *)value_;
64
11
      break;
65
2
    case VECTOR:
66
2
      delete (const Vector *)value_;
67
2
      break;
68
2
    case MATRIX:
69
2
      delete (const Eigen::MatrixXd *)value_;
70
2
      break;
71
2
    case MATRIX4D:
72
2
      delete (const Eigen::Matrix4d *)value_;
73
2
      break;
74
2
    case VALUES:
75
2
      delete (const Values *)value_;
76
2
      break;
77
12
    case NONE: /* Equivalent to void */
78
12
      break;
79
    default:
80
      throw "Value::deleteValue : Undefined type";
81
      ;
82
  }
83
91
}
84
85
90
Value::~Value() { deleteValue(); }
86
87
1
Value::Value(const bool &value) : type_(BOOL), value_(new bool(value)) {}
88
1
Value::Value(const unsigned &value)
89
1
    : type_(UNSIGNED), value_(new unsigned(value)) {}
90
Value::Value(const unsigned long int &value)
91
    : type_(UNSIGNEDLONGINT), value_(new unsigned long int(value)) {}
92
3
Value::Value(const int &value) : type_(INT), value_(new int(value)) {}
93
1
Value::Value(const float &value) : type_(FLOAT), value_(new float(value)) {}
94
5
Value::Value(const double &value) : type_(DOUBLE), value_(new double(value)) {}
95
4
Value::Value(const std::string &value)
96
4
    : type_(STRING), value_(new std::string(value)) {}
97
1
Value::Value(const Vector &value) : type_(VECTOR), value_(new Vector(value)) {}
98
1
Value::Value(const Eigen::MatrixXd &value)
99
1
    : type_(MATRIX), value_(new Eigen::MatrixXd(value)) {}
100
1
Value::Value(const Eigen::Matrix4d &value)
101
1
    : type_(MATRIX4D), value_(new Eigen::Matrix4d(value)) {}
102
1
Value::Value(const Values &value) : type_(VALUES), value_(new Values(value)) {}
103
104
61
Value::Value(const Value &value)
105
61
    : type_(value.type_), value_(copyValue(value)) {}
106
107
62
void *copyValue(const Value &value) {
108
  void *copy;
109



62
  switch (value.type()) {
110
2
    case Value::NONE:
111
2
      copy = NULL;
112
2
      break;
113
1
    case Value::BOOL:
114
1
      copy = new bool(value.boolValue());
115
1
      break;
116
1
    case Value::UNSIGNED:
117
1
      copy = new unsigned(value.unsignedValue());
118
1
      break;
119
    case Value::UNSIGNEDLONGINT:
120
      copy = new unsigned long int(value.unsignedlongintValue());
121
      break;
122
38
    case Value::INT:
123
38
      copy = new int(value.intValue());
124
38
      break;
125
    case Value::LONGINT:
126
      copy = new long int(value.longintValue());
127
      break;
128
1
    case Value::FLOAT:
129
1
      copy = new float(value.floatValue());
130
1
      break;
131
8
    case Value::DOUBLE:
132
8
      copy = new double(value.doubleValue());
133
8
      break;
134
7
    case Value::STRING:
135
7
      copy = new std::string(value.stringValue());
136
7
      break;
137
1
    case Value::VECTOR:
138
1
      copy = new Vector(value.vectorValue());
139
1
      break;
140
1
    case Value::MATRIX:
141
1
      copy = new Eigen::MatrixXd(value.matrixXdValue());
142
1
      break;
143
1
    case Value::MATRIX4D:
144
1
      copy = new Eigen::Matrix4d(value.matrix4dValue());
145
1
      break;
146
1
    case Value::VALUES:
147
1
      copy = new Values(value.valuesValue());
148
1
      break;
149
    default:
150
      abort();
151
  }
152
62
  return copy;
153
}
154
155
10
Value::Value() : type_(NONE), value_(NULL) {}
156
157
1
Value Value::operator=(const Value &value) {
158
1
  if (&value != this) {
159
1
    if (value_ != 0x0) deleteValue();
160
1
    type_ = value.type_;
161
1
    void **ptValue = const_cast<void **>(&value_);
162
1
    *ptValue = copyValue(value);
163
  }
164
1
  return *this;
165
}
166
167
15
bool Value::operator==(const Value &other) const {
168
15
  if (type_ != other.type_) return false;
169



15
  switch (type_) {
170
1
    case Value::BOOL:
171
1
      return boolValue() == other.boolValue();
172
1
    case Value::UNSIGNED:
173
1
      return unsignedValue() == other.unsignedValue();
174
    case Value::UNSIGNEDLONGINT:
175
      return unsignedlongintValue() == other.unsignedlongintValue();
176
1
    case Value::INT:
177
1
      return intValue() == other.intValue();
178
3
    case Value::DOUBLE:
179
3
      return doubleValue() == other.doubleValue();
180
1
    case Value::FLOAT:
181
1
      return floatValue() == other.floatValue();
182
3
    case Value::STRING:
183
3
      return stringValue() == other.stringValue();
184
1
    case Value::VECTOR:
185

1
      return vectorValue() == other.vectorValue();
186
1
    case Value::MATRIX:
187

1
      return matrixXdValue() == other.matrixXdValue();
188
1
    case Value::MATRIX4D:
189

1
      return matrix4dValue() == other.matrix4dValue();
190
1
    case Value::VALUES:
191
1
      return constValuesValue() == other.constValuesValue();
192
1
    case Value::NONE:
193
1
      break;
194
    default:
195
      break;
196
  }
197
1
  return false;
198
}
199
200
14
const EitherType Value::value() const { return EitherType(*this); }
201
202
80
Value::Type Value::type() const { return type_; }
203
204
6
bool Value::boolValue() const {
205
6
  if (type_ == BOOL) return *((const bool *)value_);
206

1
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an bool");
207
}
208
209
6
unsigned Value::unsignedValue() const {
210
6
  if (type_ == UNSIGNED) return *((const unsigned *)value_);
211
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
212

1
                          "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_);
217
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
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
56
int Value::intValue() const {
227
56
  if (type_ == INT) return *((const int *)value_);
228

1
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an int");
229
}
230
231
6
float Value::floatValue() const {
232
  float result;
233
6
  if (FLOAT != type_)
234

1
    throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a float");
235
5
  result = *((const float *)value_);
236
5
  return result;
237
}
238
239
19
double Value::doubleValue() const {
240
  double result;
241
19
  if (DOUBLE != type_)
242

1
    throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a double");
243
18
  result = *((const double *)value_);
244
18
  return result;
245
}
246
247
19
std::string Value::stringValue() const {
248
19
  if (type_ == STRING) return *((const std::string *)value_);
249

1
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an string");
250
}
251
252
6
Vector Value::vectorValue() const {
253
6
  if (type_ == VECTOR) return *((const Vector *)value_);
254

1
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an vector");
255
}
256
257
6
Eigen::MatrixXd Value::matrixXdValue() const {
258
6
  if (type_ == MATRIX) return *((const Eigen::MatrixXd *)value_);
259
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
260

1
                          "value is not a Eigen matrixXd");
261
}
262
263
6
Eigen::Matrix4d Value::matrix4dValue() const {
264
6
  if (type_ == MATRIX4D) return *((const Eigen::Matrix4d *)value_);
265
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
266

1
                          "value is not a Eigen matrix4d");
267
}
268
269
1
Values Value::valuesValue() const {
270
1
  if (type_ == VALUES) return *((const Values *)value_);
271
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
272
                          "value is not a vector of Value");
273
}
274
275
4
const Values &Value::constValuesValue() const {
276
4
  if (type_ == VALUES) return *((const Values *)value_);
277
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
278
                          "value is not a vector of Value");
279
}
280
281
26
std::string Value::typeName(Type type) {
282



26
  switch (type) {
283
2
    case BOOL:
284
2
      return std::string("bool");
285
2
    case UNSIGNED:
286
2
      return std::string("unsigned int");
287
    case UNSIGNEDLONGINT:
288
      return std::string("unsigned long int");
289
3
    case INT:
290
3
      return std::string("int");
291
2
    case FLOAT:
292
2
      return std::string("float");
293
4
    case DOUBLE:
294
4
      return std::string("double");
295
5
    case STRING:
296
5
      return std::string("string");
297
2
    case VECTOR:
298
2
      return std::string("vector");
299
2
    case MATRIX:
300
2
      return std::string("matrixXd");
301
2
    case MATRIX4D:
302
2
      return std::string("matrix4d");
303
1
    case VALUES:
304
1
      return std::string("values");
305
1
    default:
306
1
      return std::string("unknown");
307
  }
308
}
309
310
24
std::ostream &operator<<(std::ostream &os, const Value &value) {
311

24
  os << "Type=" << Value::typeName(value.type_) << ", value=";
312



24
  switch (value.type_) {
313
2
    case Value::BOOL:
314
2
      os << value.boolValue();
315
2
      break;
316
2
    case Value::UNSIGNED:
317
2
      os << value.unsignedValue();
318
2
      break;
319
    case Value::UNSIGNEDLONGINT:
320
      os << value.unsignedlongintValue();
321
      break;
322
2
    case Value::INT:
323
2
      os << value.intValue();
324
2
      break;
325
3
    case Value::DOUBLE:
326
3
      os << value.doubleValue();
327
3
      break;
328
2
    case Value::FLOAT:
329
2
      os << value.floatValue();
330
2
      break;
331
5
    case Value::STRING:
332
5
      os << value.stringValue();
333
5
      break;
334
2
    case Value::VECTOR:
335
2
      os << value.vectorValue();
336
2
      break;
337
2
    case Value::MATRIX:
338
2
      os << value.matrixXdValue();
339
2
      break;
340
2
    case Value::MATRIX4D:
341
2
      os << value.matrix4dValue();
342
2
      break;
343
1
    case Value::VALUES: {
344
1
      const std::vector<Value> &vals = value.constValuesValue();
345
1
      os << "[ ";
346
3
      for (std::size_t i = 0; i < vals.size(); ++i)
347
2
        os << "Value(" << vals[i] << "), ";
348
1
      os << "]";
349
1
    } break;
350
1
    default:
351
1
      return os;
352
  }
353
23
  return os;
354
}
355
356
template <>
357
const Value::Type ValueHelper<bool>::TypeID = Value::BOOL;
358
template <>
359
const Value::Type ValueHelper<unsigned>::TypeID = Value::UNSIGNED;
360
template <>
361
const Value::Type ValueHelper<unsigned long int>::TypeID =
362
    Value::UNSIGNEDLONGINT;
363
template <>
364
const Value::Type ValueHelper<int>::TypeID = Value::INT;
365
template <>
366
const Value::Type ValueHelper<float>::TypeID = Value::FLOAT;
367
template <>
368
const Value::Type ValueHelper<double>::TypeID = Value::DOUBLE;
369
template <>
370
const Value::Type ValueHelper<std::string>::TypeID = Value::STRING;
371
template <>
372
const Value::Type ValueHelper<Vector>::TypeID = Value::VECTOR;
373
374
template <>
375
const Value::Type ValueHelper<Eigen::MatrixXd>::TypeID = Value::MATRIX;
376
template <>
377
const Value::Type ValueHelper<Eigen::Matrix4d>::TypeID = Value::MATRIX4D;
378
template <>
379
const Value::Type ValueHelper<Values>::TypeID = Value::VALUES;
380
381
}  // namespace command
382
}  // namespace dynamicgraph