value.cpp 9.48 KB
Newer Older
florent's avatar
florent committed
1 2 3 4 5
//
// Copyright 2010 CNRS
//
// Author: Florent Lamiraux
//
6

florent's avatar
florent committed
7 8 9
#include "dynamic-graph/value.h"
#include "dynamic-graph/exception-abstract.h"

10 11
namespace dynamicgraph {
namespace command {
florent's avatar
florent committed
12

13
static void *copyValue(const Value &value);
14

15
EitherType::EitherType(const Value &value) : value_(new Value(value)) {}
16

17 18 19 20
EitherType::~EitherType() {
  delete value_;
  value_ = NULL;
}
21

22 23 24 25 26 27 28 29
EitherType::operator bool() const { return value_->boolValue(); }
EitherType::operator unsigned() const { return value_->unsignedValue(); }
EitherType::operator int() const { return value_->intValue(); }
EitherType::operator float() const { return value_->floatValue(); }
EitherType::operator double() const { return value_->doubleValue(); }
EitherType::operator std::string() const { return value_->stringValue(); }
EitherType::operator Vector() const { return value_->vectorValue(); }
EitherType::operator Eigen::MatrixXd() const { return value_->matrixXdValue(); }
30

31
EitherType::operator Eigen::Matrix4d() const { return value_->matrix4dValue(); }
32
EitherType::operator Values() const { return value_->valuesValue(); }
florent's avatar
florent committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
void Value::deleteValue() {
  switch (type_) {
  case BOOL:
    delete (const bool *)value_;
    break;
  case UNSIGNED:
    delete (const unsigned *)value_;
    break;
  case INT:
    delete (const int *)value_;
    break;
  case FLOAT:
    delete (const float *)value_;
    break;
  case DOUBLE:
    delete (const double *)value_;
    break;
  case STRING:
    delete (const std::string *)value_;
    break;
  case VECTOR:
    delete (const Vector *)value_;
    break;
  case MATRIX:
    delete (const Eigen::MatrixXd *)value_;
    break;
  case MATRIX4D:
    delete (const Eigen::Matrix4d *)value_;
    break;
63 64 65
  case VALUES:
    delete (const Values *)value_;
    break;
66 67 68
  default:;
  }
}
69

70
Value::~Value() { deleteValue(); }
71

72 73 74 75 76 77 78 79 80 81 82 83 84
Value::Value(const bool &value) : type_(BOOL), value_(new bool(value)) {}
Value::Value(const unsigned &value)
    : type_(UNSIGNED), value_(new unsigned(value)) {}
Value::Value(const int &value) : type_(INT), value_(new int(value)) {}
Value::Value(const float &value) : type_(FLOAT), value_(new float(value)) {}
Value::Value(const double &value) : type_(DOUBLE), value_(new double(value)) {}
Value::Value(const std::string &value)
    : type_(STRING), value_(new std::string(value)) {}
Value::Value(const Vector &value) : type_(VECTOR), value_(new Vector(value)) {}
Value::Value(const Eigen::MatrixXd &value)
    : type_(MATRIX), value_(new Eigen::MatrixXd(value)) {}
Value::Value(const Eigen::Matrix4d &value)
    : type_(MATRIX4D), value_(new Eigen::Matrix4d(value)) {}
Olivier Stasse's avatar
Olivier Stasse committed
85
Value::Value(const Values &value) : type_(VALUES), value_(new Values(value)) {}
86

87 88
Value::Value(const Value &value)
    : type_(value.type_), value_(copyValue(value)) {}
florent's avatar
florent committed
89

90 91 92
void *copyValue(const Value &value) {
  void *copy;
  switch (value.type()) {
florent's avatar
florent committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  case Value::NONE:
    copy = NULL;
    break;
  case Value::BOOL:
    copy = new bool(value.boolValue());
    break;
  case Value::UNSIGNED:
    copy = new unsigned(value.unsignedValue());
    break;
  case Value::INT:
    copy = new int(value.intValue());
    break;
  case Value::FLOAT:
    copy = new float(value.floatValue());
    break;
  case Value::DOUBLE:
    copy = new double(value.doubleValue());
    break;
  case Value::STRING:
    copy = new std::string(value.stringValue());
    break;
  case Value::VECTOR:
    copy = new Vector(value.vectorValue());
    break;
  case Value::MATRIX:
    copy = new Eigen::MatrixXd(value.matrixXdValue());
    break;
  case Value::MATRIX4D:
    copy = new Eigen::Matrix4d(value.matrix4dValue());
    break;
124 125 126
  case Value::VALUES:
    copy = new Values(value.valuesValue());
    break;
127 128 129 130 131
  default:
    abort();
  }
  return copy;
}
florent's avatar
florent committed
132

133
Value::Value() : type_(NONE), value_(NULL) {}
florent's avatar
florent committed
134

135 136 137 138 139 140 141 142 143 144
Value Value::operator=(const Value &value) {
  if (&value != this) {
    if (value_ != 0x0)
      deleteValue();
    type_ = value.type_;
    void **ptValue = const_cast<void **>(&value_);
    *ptValue = copyValue(value);
  }
  return *this;
}
145

Joseph Mirabel's avatar
Joseph Mirabel committed
146
bool Value::operator==(const Value &other) const {
Olivier Stasse's avatar
Olivier Stasse committed
147 148
  if (type_ != other.type_)
    return false;
Joseph Mirabel's avatar
Joseph Mirabel committed
149
  switch (type_) {
Olivier Stasse's avatar
Olivier Stasse committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  case Value::BOOL:
    return boolValue() == other.boolValue();
  case Value::UNSIGNED:
    return unsignedValue() == other.unsignedValue();
  case Value::INT:
    return intValue() == other.intValue();
  case Value::DOUBLE:
    return doubleValue() == other.doubleValue();
  case Value::FLOAT:
    return floatValue() == other.floatValue();
  case Value::STRING:
    return stringValue() == other.stringValue();
  case Value::VECTOR:
    return vectorValue() == other.vectorValue();
  case Value::MATRIX:
    return matrixXdValue() == other.matrixXdValue();
  case Value::MATRIX4D:
    return matrix4dValue() == other.matrix4dValue();
  case Value::VALUES:
    return constValuesValue() == other.constValuesValue();
  case Value::NONE:
    break;
  default:
    break;
Joseph Mirabel's avatar
Joseph Mirabel committed
174 175 176 177
  }
  return false;
}

178
const EitherType Value::value() const { return EitherType(*this); }
florent's avatar
florent committed
179

180
Value::Type Value::type() const { return type_; }
181

182 183 184 185 186
bool Value::boolValue() const {
  if (type_ == BOOL)
    return *((const bool *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an bool");
}
florent's avatar
florent committed
187

188 189 190 191 192 193
unsigned Value::unsignedValue() const {
  if (type_ == UNSIGNED)
    return *((const unsigned *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
                          "value is not an unsigned int");
}
194

195 196 197 198 199
int Value::intValue() const {
  if (type_ == INT)
    return *((const int *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an int int");
}
200

201 202 203 204 205 206 207
float Value::floatValue() const {
  float result;
  if (FLOAT != type_)
    throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a float");
  result = *((const float *)value_);
  return result;
}
florent's avatar
florent committed
208

209 210 211 212 213 214 215
double Value::doubleValue() const {
  double result;
  if (DOUBLE != type_)
    throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not a double");
  result = *((const double *)value_);
  return result;
}
florent's avatar
florent committed
216

217 218 219 220 221
std::string Value::stringValue() const {
  if (type_ == STRING)
    return *((const std::string *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an string");
}
florent's avatar
florent committed
222

223 224 225 226 227
Vector Value::vectorValue() const {
  if (type_ == VECTOR)
    return *((const Vector *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS, "value is not an vector");
}
florent's avatar
florent committed
228

229 230 231 232 233 234
Eigen::MatrixXd Value::matrixXdValue() const {
  if (type_ == MATRIX)
    return *((const Eigen::MatrixXd *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
                          "value is not a Eigen matrixXd");
}
235

236 237 238 239 240 241 242
Eigen::Matrix4d Value::matrix4dValue() const {
  if (type_ == MATRIX4D)
    return *((const Eigen::Matrix4d *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
                          "value is not a Eigen matrix4d");
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256
Values Value::valuesValue() const {
  if (type_ == VALUES)
    return *((const Values *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
                          "value is not a vector of Value");
}

const Values &Value::constValuesValue() const {
  if (type_ == VALUES)
    return *((const Values *)value_);
  throw ExceptionAbstract(ExceptionAbstract::TOOLS,
                          "value is not a vector of Value");
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
std::string Value::typeName(Type type) {
  switch (type) {
  case BOOL:
    return std::string("bool");
  case UNSIGNED:
    return std::string("unsigned int");
  case INT:
    return std::string("int");
  case FLOAT:
    return std::string("float");
  case DOUBLE:
    return std::string("double");
  case STRING:
    return std::string("string");
  case VECTOR:
    return std::string("vector");
  case MATRIX:
    return std::string("matrixXd");
  case MATRIX4D:
    return std::string("matrix4d");
277 278
  case VALUES:
    return std::string("values");
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  default:
    return std::string("unknown");
  }
}

std::ostream &operator<<(std::ostream &os, const Value &value) {
  os << "Type=" << Value::typeName(value.type_) << ", value=";
  switch (value.type_) {
  case Value::BOOL:
    os << value.boolValue();
    break;
  case Value::UNSIGNED:
    os << value.unsignedValue();
    break;
  case Value::INT:
    os << value.intValue();
    break;
  case Value::DOUBLE:
    os << value.doubleValue();
    break;
  case Value::FLOAT:
    os << value.floatValue();
    break;
  case Value::STRING:
    os << value.stringValue();
    break;
  case Value::VECTOR:
    os << value.vectorValue();
    break;
  case Value::MATRIX:
    os << value.matrixXdValue();
    break;
  case Value::MATRIX4D:
    os << value.matrix4dValue();
    break;
Olivier Stasse's avatar
Olivier Stasse committed
314 315 316 317 318 319 320
  case Value::VALUES: {
    const std::vector<Value> &vals = value.constValuesValue();
    os << "[ ";
    for (std::size_t i = 0; i < vals.size(); ++i)
      os << "Value(" << vals[i] << "), ";
    os << "]";
  } break;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  default:
    return os;
  }
  return os;
}

template <> const Value::Type ValueHelper<bool>::TypeID = Value::BOOL;
template <> const Value::Type ValueHelper<unsigned>::TypeID = Value::UNSIGNED;
template <> const Value::Type ValueHelper<int>::TypeID = Value::INT;
template <> const Value::Type ValueHelper<float>::TypeID = Value::FLOAT;
template <> const Value::Type ValueHelper<double>::TypeID = Value::DOUBLE;
template <> const Value::Type ValueHelper<std::string>::TypeID = Value::STRING;
template <> const Value::Type ValueHelper<Vector>::TypeID = Value::VECTOR;
template <>
const Value::Type ValueHelper<Eigen::MatrixXd>::TypeID = Value::MATRIX;
template <>
const Value::Type ValueHelper<Eigen::Matrix4d>::TypeID = Value::MATRIX4D;
338
template <> const Value::Type ValueHelper<Values>::TypeID = Value::VALUES;
339 340 341

} // namespace command
} // namespace dynamicgraph