OpenDaVINCI  4.15.3
Message.h
Go to the documentation of this file.
1 
20 #ifndef OPENDAVINCI_CORE_REFLECTION_MESSAGE_H_
21 #define OPENDAVINCI_CORE_REFLECTION_MESSAGE_H_
22 
23 #include <memory>
24 #include <vector>
25 
30 #include "opendavinci/generated/odcore/data/reflection/AbstractField.h"
31 
32 namespace odcore { namespace base { class Visitor; } }
33 
34 namespace odcore {
35  namespace reflection {
36 
37  using namespace std;
38 
43  public:
44  Message();
45 
51  Message(const Message &obj);
52 
53  virtual ~Message();
54 
61  Message& operator=(const Message &obj);
62 
63  private:
64  // Implementation of serialization of a Message's content is by design disabled.
65  virtual ostream& operator<<(ostream &out) const;
66 
67  // Implementation of serialization of a Message's content is by design disabled.
68  virtual istream& operator>>(istream &in);
69 
70  public:
71  virtual void accept(odcore::base::Visitor &v);
72 
73  virtual int32_t getID() const;
74 
75  virtual const string getShortName() const;
76 
77  virtual const string getLongName() const;
78 
79  virtual const string toString() const;
80 
81  public:
82  void setID(const int32_t &id);
83  void setShortName(const string &sn);
84  void setLongName(const string &ln);
85 
86  private:
95  template<typename T>
96  void visitPrimitiveDataType(odcore::base::Visitor &v, std::shared_ptr<odcore::data::reflection::AbstractField> &f) {
97  // Read value.
98  T value = dynamic_cast<odcore::reflection::Field<T>*>(f.operator->())->getValue();
99  // Visit value.
100  v.visit(f->getFieldIdentifier(), f->getLongFieldName(), f->getShortFieldName(), value);
101  // Update value.
102  dynamic_cast<odcore::reflection::Field<T>*>(f.operator->())->setValue(value);
103  }
104 
105  public:
111  void insertField(const std::shared_ptr<odcore::data::reflection::AbstractField> &f);
112 
118  void addField(const std::shared_ptr<odcore::data::reflection::AbstractField> &f);
119 
125  uint32_t getNumberOfFields() const;
126 
134  std::shared_ptr<odcore::data::reflection::AbstractField> getFieldByIdentifier(const uint32_t &id, bool &found);
135 
144  template<typename T>
145  T getValueFromScalarField(const uint32_t &id, bool &found, bool &extracted) {
146  T value = 0;
147  extracted = false;
148  std::shared_ptr<odcore::data::reflection::AbstractField> af = getFieldByIdentifier(id, found);
149  if (found) {
150  extracted = true;
151  switch(af->getFieldDataType()) {
152  case odcore::data::reflection::AbstractField::BOOL_T:
153  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<bool>*>(af.get())->getValue());
154  break;
156  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<uint8_t>*>(af.get())->getValue());
157  break;
159  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<int8_t>*>(af.get())->getValue());
160  break;
161  case odcore::data::reflection::AbstractField::UCHAR_T:
162  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<unsigned char>*>(af.get())->getValue());
163  break;
164  case odcore::data::reflection::AbstractField::CHAR_T:
165  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<char>*>(af.get())->getValue());
166  break;
168  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<uint16_t>*>(af.get())->getValue());
169  break;
171  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<int16_t>*>(af.get())->getValue());
172  break;
174  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<uint32_t>*>(af.get())->getValue());
175  break;
177  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<int32_t>*>(af.get())->getValue());
178  break;
180  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<uint64_t>*>(af.get())->getValue());
181  break;
183  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<int64_t>*>(af.get())->getValue());
184  break;
185  case odcore::data::reflection::AbstractField::FLOAT_T:
186  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<float>*>(af.get())->getValue());
187  break;
188  case odcore::data::reflection::AbstractField::DOUBLE_T:
189  value = static_cast<T>(dynamic_cast<odcore::reflection::Field<double>*>(af.get())->getValue());
190  break;
191  default:
192  extracted = false;
193  break;
194  }
195  }
196  return value;
197  }
198 
199  private:
200  int32_t m_ID;
201  string m_shortName;
202  string m_longName;
203  vector<std::shared_ptr<odcore::data::reflection::AbstractField> > m_fields;
204  };
205 
206  }
207 } // odcore::reflection
208 
209 #endif /*OPENDAVINCI_CORE_REFLECTION_MESSAGE_H_*/
Definition: opendavinci.h:105
Definition: opendavinci.h:106
Definition: Visitable.h:34
Definition: opendavinci.h:102
Definition: Message.h:42
#define OPENDAVINCI_API
Definition: opendavinci.h:67
T getValueFromScalarField(const uint32_t &id, bool &found, bool &extracted)
Definition: Message.h:145
Definition: SerializableData.h:36
Definition: opendavinci.h:101
Definition: opendavinci.h:100
Definition: Visitor.h:37
virtual void visit(const uint32_t &id, const string &longName, const string &shortName, odcore::serialization::Serializable &v)=0
Definition: opendavinci.h:99
Definition: opendavinci.h:104
Definition: Field.h:36
Definition: opendavinci.h:103