XORDE  v1.0
eXtensible Operational Robotic Development Environment
ObjectInfo.h
1 #ifndef OBJECTINFO_H
2 #define OBJECTINFO_H
3 
4 #include "tronostools_global.h"
5 #include "objectdescription.h"
6 #include <QJsonObject>
7 
8 template<typename T>
9 class ONBObject;
10 
16 class TRONOSTOOLSSHARED_EXPORT ObjectInfo
17 {
18 public:
20  typedef enum
21  {
22  Volatile= 0x01,
23  Read = 0x02,
24  Write = 0x04,
25  Save = 0x08,
26  Hidden = 0x10,
27  Dual = 0x20,
28  Function= 0x40,
29  Array = 0x80,
30 
31  ReadOnly = Read,
32  WriteOnly = Write,
33  ReadWrite = Read | Write,
34 
35  Constant = Read,
36  Input = Write | Volatile,
37  Output = Read | Volatile,
38  Variable = Read | Write,
39  Setting = Variable | Save,
40 
41  Exchange = Dual | ReadWrite | Volatile,
42  SecretVar = ReadWrite | Hidden,
43  SecretSetting = Setting | Hidden
44  } Flags;
45 
48  typedef enum
49  {
50  Void = 43,
51  Bool = 1,
52  Int = 2,
53  UInt = 3,
54  LongLong = 4,
55  ULongLong = 5,
56  Double = 6,
57  Long = 32,
58  Short = 33,
59  Char = 34,
60  ULong = 35,
61  UShort = 36,
62  UChar = 37,
63  Float = 38,
64  SChar = 40,
65 
66  String = 10,
67  Common = 12,
68  Variant = 41,
69  } Type;
70 
71 protected:
73  typedef enum
74  {
75  Minimum = 0x0001,
76  Maximum = 0x0002,
77  Default = 0x0004,
78  MMD = Minimum | Maximum | Default,
79  Step = 0x0008,
80  MimeType= 0x0010,
81  Hint = 0x0020,
82  Unit = 0x0040,
83  Options = 0x0080,
84  ExtInfo = 0x0100,
85  Enum = 0x0200,
86  } ExtFlags;
87 
88  typedef enum
89  {
90  MV_Min = 1,
91  MV_Max = 2,
92  MV_Def = 3,
93  MV_Step = 4,
94  MV_Mime = 5,
95  MV_Hint = 6,
96  MV_Unit = 7,
97  MV_Options = 8,
98  MV_ExtInfo = 9,
99  MV_Enum = 10
100  } MetaValue;
101 
102  typedef bool Bool_t;
103  typedef int Int_t;
104  typedef unsigned int UInt_t;
105  typedef long long LongLong_t;
106  typedef unsigned long long ULongLong_t;
107  typedef double Double_t;
108  typedef long Long_t;
109  typedef short Short_t;
110  typedef char Char_t;
111  typedef unsigned long ULong_t;
112  typedef unsigned short UShort_t;
113  typedef unsigned char UChar_t;
114  typedef float Float_t;
115  typedef signed char SChar_t;
116  typedef QString String_t;
117 
118  template<typename T> static Type typeOfVar(T &var) {(void)var; return Common;}
119 #define DeclareTypeOfVar(Tp) static Type typeOfVar(Tp##_t &) {return Tp;}
120  //DeclareTypeOfVar(Void)
121  DeclareTypeOfVar(Bool)
122  DeclareTypeOfVar(Int)
123  DeclareTypeOfVar(UInt)
124  DeclareTypeOfVar(LongLong)
125  DeclareTypeOfVar(ULongLong)
126  DeclareTypeOfVar(Double)
127  DeclareTypeOfVar(Long)
128  DeclareTypeOfVar(Short)
129  DeclareTypeOfVar(Char)
130  DeclareTypeOfVar(ULong)
131  DeclareTypeOfVar(UShort)
132  DeclareTypeOfVar(UChar)
133  DeclareTypeOfVar(Float)
134  DeclareTypeOfVar(SChar)
135  DeclareTypeOfVar(String)
136 // static Type typeOfVar(String_t &) {return String;}
137 // template<> ObjectInfo::Type typeOfVar<_String>(_String &var) {(void)var; return ObjectInfo::String;}
138 // DeclareTypeOfVar(QVector3D)
139 // DeclareTypeOfVar(QQuaternion)
140 
141  class ExtendedInfo
142  {
143  public:
144  int RMIP; // РМИП
145  int autoPeriodMs; // period of automatic output generation
146 // bool sendOnlyIfChanged;
147  bool needTimestamp; // The need of timestamp transmission
148 
149  ExtendedInfo() : RMIP(10), autoPeriodMs(0), needTimestamp(false) {}
150  };
151 
152  const void *mReadPtr = nullptr;
153  void *mWritePtr = nullptr;
154  ObjectDescription mDesc;
155  ExtendedInfo m_extInfo;
156  bool m_changed = false;
157  unsigned long m_timestamp = 0;
158 
159  int sizeofType(Type type) const;
160 
161  bool doRead(QByteArray &ba, const void *ptr) const;
162  bool doWrite(const QByteArray &ba, void *ptr);
163 
164  virtual void requestEvent() const {}
165  virtual void receiveEvent() {}
166  virtual void valueChangeEvent() {}
167 
168  friend class ComponentBase;
169  friend class ComponentProxyONB;
170 
185  template<typename T>
186  static ONBObject<T> &create(QString name, T &var, Flags flags=ReadWrite)
187  {
188  ONBObject<T> *obj = new ONBObject<T>;
189  unsigned short sz = (unsigned short)sizeof(T);
190  Type t = typeOfVar(var);
191  if (flags & Read)
192  {
193  obj->mReadPtr = &var;
194  obj->mDesc.readSize = sz;
195  obj->mDesc.rType = t;
196  }
197  if (flags & Write)
198  {
199  obj->mWritePtr = &var;
200  obj->mDesc.writeSize = sz;
201  obj->mDesc.wType = t;
202  }
203  obj->mDesc.flags = flags;
204  obj->mDesc.name = name;
205  return *obj;
206  }
207 
210  template<> static ONBObject<QByteArray> &create(QString name, QByteArray &var, Flags flags);
211 
215  template<typename T, int N>
216  static ONBObject<T> &create(QString name, T (&var)[N], Flags flags=ReadWrite)
217  {
218  ONBObject<T> *obj = new ONBObject<T>;
219  unsigned short sz = static_cast<unsigned short>(sizeof(T)) * N;
220  Type t = typeOfVar(var[0]);
221  if (flags & Read)
222  {
223  obj->mReadPtr = &var;
224  obj->mDesc.readSize = sz;
225  obj->mDesc.rType = t;
226  }
227  if (flags & Write)
228  {
229  obj->mWritePtr = &var;
230  obj->mDesc.writeSize = sz;
231  obj->mDesc.wType = t;
232  }
233  obj->mDesc.flags = flags | Array;
234  obj->mDesc.name = name;
235  return *obj;
236  }
237 
238  template<typename T>
239  bool rebind(T &var)
240  {
241  bool descChanged = false;
242  unsigned short sz = (unsigned short)sizeof(T);
243  Type t = typeOfVar(var);
244  if (mDesc.flags & Read)
245  {
246  if (t != mDesc.rType || sz != mDesc.readSize)
247  descChanged = true;
248  mDesc.rType = t;
249  mDesc.readSize = sz;
250  mReadPtr = &var;
251  }
252  if (mDesc.flags & Write)
253  {
254  if (t != mDesc.wType || sz != mDesc.writeSize)
255  descChanged = true;
256  mDesc.wType = t;
257  mDesc.writeSize = sz;
258  mWritePtr = &var;
259  }
260  return descChanged;
261  }
262 
263  void setId(unsigned char oid, unsigned char group = 0) {mDesc.id = oid; mDesc.group = group;}
264 
267  QByteArray read() const;
268 
272  bool read(QByteArray &ba) const;
273 
277  virtual bool readMeta(QByteArray &ba, MetaValue id) const {(void)id; return read(ba);}
278 
279  bool write(const QByteArray &ba);
280 
281  virtual bool writeMeta(const QByteArray &ba, MetaValue id) {(void)ba; (void)id; return false;}
282 
283 public:
286  ObjectInfo();
287 
290  ObjectInfo(const ObjectDescription &desc);
291 
293  ObjectInfo(const ObjectInfo &other) = delete;
294 
295 // virtual ~ObjectInfo() {}
296 
299  const ObjectDescription &description() const {return mDesc;}
300 
303  QString name() const {return mDesc.name;}
304 
306  Flags flags() const {return static_cast<Flags>(mDesc.flags);}
307 
309  int RMIP() const {return m_extInfo.RMIP;}
310 
312  unsigned long timestamp() const {return m_timestamp;}
313 
316  inline bool isVolatile() const {return mDesc.flags & Volatile;}
317  inline bool isReadable() const {return mDesc.flags & Read;}
318  inline bool isWritable() const {return mDesc.flags & Write;}
319  inline bool isStorable() const {return mDesc.flags & Save;}
320  inline bool isDual() const {return mDesc.flags & Dual;}
321  inline bool isHidden() const {return mDesc.flags & Hidden;}
322  inline bool isFunction() const {return mDesc.flags & Function;}
323  inline bool isArray() const {return mDesc.flags & Array;}
325 
328  bool hasMinimum() const {return mDesc.extFlags & Minimum;}
329  bool hasMaximum() const {return mDesc.extFlags & Maximum;}
330  bool hasDefault() const {return mDesc.extFlags & Default;}
331  bool hasStep() const {return mDesc.extFlags & Step;}
332  bool hasMimeType() const {return mDesc.extFlags & MimeType;}
333  bool hasHint() const {return mDesc.extFlags & Hint;}
334  bool hasUnit() const {return mDesc.extFlags & Unit;}
335  bool hasOptions() const {return mDesc.extFlags & Options;}
336  bool hasExtInfo() const {return mDesc.extFlags & ExtInfo;}
337  bool isEnum() const {return mDesc.extFlags & Enum;}
339 
342  QJsonObject createJsonInfo() const;
343 };
344 
345 #endif // OBJECTINFO_H
QString name() const
Get name of the object.
Definition: ObjectInfo.h:303
unsigned char wType
Type of the object to write (Type enum)
Definition: objectdescription.h:35
bool isWritable() const
Test an access flag.
Definition: ObjectInfo.h:318
bool hasMinimum() const
Test a meta-value flag.
Definition: ObjectInfo.h:328
bool isHidden() const
Test an access flag.
Definition: ObjectInfo.h:321
ONB Object description.
Definition: objectdescription.h:44
unsigned long timestamp() const
Get timestamp of the last received value.
Definition: ObjectInfo.h:312
The base for the ONB component.
Definition: ComponentBase.h:97
unsigned char group
Object ID category.
Definition: objectdescription.h:30
bool isStorable() const
Test an access flag.
Definition: ObjectInfo.h:319
bool hasMimeType() const
Test a meta-value flag.
Definition: ObjectInfo.h:332
const ObjectDescription & description() const
Get description of the object.
Definition: ObjectInfo.h:299
unsigned char flags
Access flags from Flags enum.
Definition: objectdescription.h:32
bool hasMaximum() const
Test a meta-value flag.
Definition: ObjectInfo.h:329
Component-side ONB Object.
Definition: ObjectInfo.h:9
unsigned short extFlags
Advanced flags (min max def) from ExtFlags enum.
Definition: objectdescription.h:33
bool isFunction() const
Test an access flag.
Definition: ObjectInfo.h:322
bool isArray() const
Test an access flag.
Definition: ObjectInfo.h:323
ONB Object base interface.
Definition: ObjectInfo.h:16
Flags
Access flags of the Object.
Definition: ObjectInfo.h:20
unsigned char rType
Type of the object to read (Type enum)
Definition: objectdescription.h:34
unsigned short writeSize
Size of the object to write in bytes.
Definition: objectdescription.h:37
unsigned short readSize
Size of the object to read in bytes.
Definition: objectdescription.h:36
bool isDual() const
Test an access flag.
Definition: ObjectInfo.h:320
QString name
Name of the object.
Definition: objectdescription.h:55
bool hasExtInfo() const
Test a meta-value flag.
Definition: ObjectInfo.h:336
bool hasDefault() const
Test a meta-value flag.
Definition: ObjectInfo.h:330
Flags flags() const
Get access flags.
Definition: ObjectInfo.h:306
bool hasUnit() const
Test a meta-value flag.
Definition: ObjectInfo.h:334
bool hasHint() const
Test a meta-value flag.
Definition: ObjectInfo.h:333
bool isVolatile() const
Test an access flag.
Definition: ObjectInfo.h:316
bool hasStep() const
Test a meta-value flag.
Definition: ObjectInfo.h:331
int RMIP() const
Get default sampling interval.
Definition: ObjectInfo.h:309
unsigned char id
Object ID.
Definition: objectdescription.h:27
bool isEnum() const
Test a meta-value flag.
Definition: ObjectInfo.h:337
bool hasOptions() const
Test a meta-value flag.
Definition: ObjectInfo.h:335
bool isReadable() const
Test an access flag.
Definition: ObjectInfo.h:317