dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
value.h
1 //
2 // Copyright 2010 CNRS
3 //
4 // Author: Florent Lamiraux
5 //
6 
7 #ifndef DYNAMIC_GRAPH_VALUE_H
8 #define DYNAMIC_GRAPH_VALUE_H
9 
10 #include "dynamic-graph/dynamic-graph-api.h"
11 #include <cassert>
12 #include <dynamic-graph/linear-algebra.h>
13 #include <string>
14 #include <typeinfo>
15 #include <vector>
16 
17 namespace dynamicgraph {
18 namespace command {
19 class Value;
20 typedef std::vector<Value> Values;
21 
22 class DYNAMIC_GRAPH_DLLAPI EitherType {
23 public:
24  EitherType(const Value &value);
25  ~EitherType();
26  operator bool() const;
27  operator unsigned() const;
28  operator int() const;
29  operator float() const;
30  operator double() const;
31  operator std::string() const;
32  operator Vector() const;
33  operator Eigen::MatrixXd() const;
34  operator Eigen::Matrix4d() const;
35  operator Values() const;
36 
37 private:
38  const Value *value_;
39 };
40 
46 class DYNAMIC_GRAPH_DLLAPI Value {
47 public:
48  enum Type {
49  NONE,
50  BOOL,
51  UNSIGNED,
52  INT,
53  FLOAT,
54  DOUBLE,
55  STRING,
56  VECTOR,
57  MATRIX,
58  MATRIX4D,
59  VALUES,
60  NB_TYPES
61  };
62  ~Value();
63  void deleteValue();
64  explicit Value(const bool &value);
65  explicit Value(const unsigned &value);
66  explicit Value(const int &value);
67  explicit Value(const float &value);
68  explicit Value(const double &value);
69  explicit Value(const std::string &value);
70  explicit Value(const Vector &value);
71  explicit Value(const Eigen::MatrixXd &value);
72  explicit Value(const Eigen::Matrix4d &value);
73  explicit Value(const Values &value);
75  Value(const Value &value);
76  // Construct an empty value (None)
77  explicit Value();
78  // operator assignement
79  Value operator=(const Value &value);
80  // Equality operator
81  bool operator==(const Value &other) const;
83  Type type() const;
84 
96  const EitherType value() const;
98  static std::string typeName(Type type);
99 
101  DYNAMIC_GRAPH_DLLAPI friend std::ostream &operator<<(std::ostream &os,
102  const Value &value);
103 
104 public:
105  friend class EitherType;
106  bool boolValue() const;
107  unsigned unsignedValue() const;
108  int intValue() const;
109  float floatValue() const;
110  double doubleValue() const;
111  std::string stringValue() const;
112  Vector vectorValue() const;
113  Eigen::MatrixXd matrixXdValue() const;
114  Eigen::Matrix4d matrix4dValue() const;
115  Values valuesValue() const;
116  const Values &constValuesValue() const;
117  Type type_;
118  const void *const value_;
119 };
120 
121 /* ---- HELPER ---------------------------------------------------------- */
122 // Note: to ensure the WIN32 compatibility, it is necessary to export
123 // the template specialization. Also, it is forbidden to do the template
124 // specialization declaration in the header file, for the same reason.
125 template <typename T> struct DYNAMIC_GRAPH_DLLAPI ValueHelper {
126  static const Value::Type TypeID;
127 };
128 } // namespace command
129 } // namespace dynamicgraph
130 
131 #endif // DYNAMIC_GRAPH_VALUE_H
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:46