Choreonoid  1.8
MprVariable.h
Go to the documentation of this file.
1 #ifndef CNOID_MANIPULATOR_PLUGIN_MPR_VARIBLE_H
2 #define CNOID_MANIPULATOR_PLUGIN_MPR_VARIBLE_H
3 
4 #include <cnoid/Referenced>
5 #include <cnoid/GeneralId>
6 #include <cnoid/Signal>
7 #include <cnoid/stdx/variant>
8 #include <string>
9 #include "exportdecl.h"
10 
11 namespace cnoid {
12 
13 class MprVariableList;
14 
15 class CNOID_EXPORT MprVariable : public Referenced
16 {
17 public:
18  typedef stdx::variant<int, double, bool, std::string> Value;
19  enum TypeId { Int, Double, Bool, String };
20 
21  MprVariable();
22  MprVariable(const GeneralId& id, Value value = 0);
23  MprVariable(const MprVariable& org);
24  MprVariable& operator=(const MprVariable&) = delete;
25 
26  static int valueType(const Value& value) { return stdx::get_variant_index(value); }
27  static int intValue(const Value& value) { return stdx::get<int>(value); }
28  static double doubleValue(const Value& value) { return stdx::get<double>(value); }
29  static bool boolValue(const Value& value) { return stdx::get<bool>(value); }
30  static const std::string& stringValue(const Value& value) { return stdx::get<std::string>(value); }
31  static bool toBool(const Value& value);
32 
33  const GeneralId& id() const { return id_; }
34  bool resetId(const GeneralId& id);
35 
36  int valueType() const { return stdx::get_variant_index(value_); }
37  bool changeValueType(TypeId type);
38 
39  bool isInt() const { return valueType() == Int; }
40  bool isDouble() const { return valueType() == Double; }
41  bool isBool() const { return valueType() == Bool; }
42  bool isString() const { return valueType() == String; }
43 
44  template<class T> T value() const { return stdx::get<T>(value_); }
45  Value value() const { return value_; }
46  int intValue() const { return stdx::get<int>(value_); }
47  double doubleValue() const { return stdx::get<double>(value_); }
48  bool boolValue() const { return stdx::get<bool>(value_); }
49  const std::string& stringValue() const { return stdx::get<std::string>(value_); }
50 
51  bool setValue(int value);
52  bool setValue(double value);
53  bool setValue(bool value);
54  bool setValue(const std::string& value);
55  bool setValue(const Value& value);
56 
57  bool toBool() const { return toBool(value_); }
58  std::string toString() const;
59 
60  const std::string& note() const { return note_; }
61  void setNote(const std::string& note) { note_ = note; }
62 
63  MprVariableList* ownerVariableList() const;
64 
65  bool read(const Mapping& archive);
66  bool write(Mapping& archive) const;
67 
68  enum UpdateFlag {
69  IdUpdate = 1 << 0,
70  ValueUpdate = 1 << 1,
71  NoteUpdate = 1 << 2
72  };
73  SignalProxy<void(int flags)> sigUpdated() { return sigUpdated_; }
74  void notifyUpdate(int flags = ValueUpdate);
75 
76 private:
77  Value value_;
78  GeneralId id_;
79  std::string note_;
80  weak_ref_ptr<MprVariableList> ownerVariableList_;
81  Signal<void(int flags)> sigUpdated_;
82 
83  friend class MprVariableList;
84 
85  template<class ValueType>
86  bool setScalarValue(ValueType value);
87 };
88 
90 
91 }
92 
93 #endif
cnoid::MprVariable
Definition: MprVariable.h:15
cnoid::MprVariable::sigUpdated
SignalProxy< void(int flags)> sigUpdated()
Definition: MprVariable.h:73
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::GeneralId
Definition: GeneralId.h:11
cnoid::MprVariable::note
const std::string & note() const
Definition: MprVariable.h:60
cnoid::write
Listing * write(Mapping *mapping, const std::string &key, const Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:145
cnoid::weak_ref_ptr
Definition: Referenced.h:276
cnoid::MprVariable::valueType
int valueType() const
Definition: MprVariable.h:36
cnoid::MprVariable::setNote
void setNote(const std::string &note)
Definition: MprVariable.h:61
cnoid::read
bool read(const Mapping *mapping, const std::string &key, Eigen::MatrixBase< Derived > &x)
Definition: EigenArchive.h:43
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::MprVariablePtr
ref_ptr< MprVariable > MprVariablePtr
Definition: MprVariable.h:89
cnoid::MprVariable::isDouble
bool isDouble() const
Definition: MprVariable.h:40
cnoid::MprVariable::Value
stdx::variant< int, double, bool, std::string > Value
Definition: MprVariable.h:18
cnoid::MprVariable::intValue
int intValue() const
Definition: MprVariable.h:46
cnoid::MprVariable::doubleValue
double doubleValue() const
Definition: MprVariable.h:47
cnoid::MprVariable::isBool
bool isBool() const
Definition: MprVariable.h:41
cnoid::MprVariable::UpdateFlag
UpdateFlag
Definition: MprVariable.h:68
cnoid::MprVariable::stringValue
const std::string & stringValue() const
Definition: MprVariable.h:49
cnoid::MprVariable::stringValue
static const std::string & stringValue(const Value &value)
Definition: MprVariable.h:30
cnoid::MprVariable::intValue
static int intValue(const Value &value)
Definition: MprVariable.h:27
cnoid::MprVariable::valueType
static int valueType(const Value &value)
Definition: MprVariable.h:26
cnoid::MprVariable::doubleValue
static double doubleValue(const Value &value)
Definition: MprVariable.h:28
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::MprVariable::isString
bool isString() const
Definition: MprVariable.h:42
cnoid::MprVariable::value
T value() const
Definition: MprVariable.h:44
cnoid::MprVariable::TypeId
TypeId
Definition: MprVariable.h:19
cnoid::MprVariableList
Definition: MprVariableList.h:12
cnoid::Referenced
Definition: Referenced.h:54
cnoid::MprVariable::isInt
bool isInt() const
Definition: MprVariable.h:39
cnoid::MprVariable::boolValue
static bool boolValue(const Value &value)
Definition: MprVariable.h:29
cnoid::MprVariable::toBool
bool toBool() const
Definition: MprVariable.h:57
cnoid::Signal
Definition: Signal.h:165
cnoid::MprVariable::id
const GeneralId & id() const
Definition: MprVariable.h:33
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::MprVariable::value
Value value() const
Definition: MprVariable.h:45
cnoid::MprVariable::boolValue
bool boolValue() const
Definition: MprVariable.h:48