XORDE  v1.0
eXtensible Operational Robotic Development Environment
onbobject.h
1 #ifndef ONBOBJECT_H
2 #define ONBOBJECT_H
3 
4 #include "ObjectInfo.h"
5 #include <chrono>
6 #include <functional>
7 #include <QStringList>
8 
9 using namespace std::chrono_literals;
10 
28 template<typename T>
29 class ONBObject : public ObjectInfo
30 {
31 public:
37  explicit ONBObject()
38  {
39  mDesc.extFlags |= ExtInfo;
40  }
41 
46  ONBObject<T> &min(const T &value)
47  {
48  delete m_min;
49  m_min = new T(value);
50  mDesc.extFlags |= Minimum;
51  return *this;
52  }
53 
58  ONBObject<T> &max(const T &value)
59  {
60  delete m_max;
61  m_max = new T(value);
62  mDesc.extFlags |= Maximum;
63  return *this;
64  }
65 
70  ONBObject<T> &def(const T &value)
71  {
72  delete m_def;
73  m_def = new T(value);
74  mDesc.extFlags |= Default;
75  return *this;
76  }
77 
83  ONBObject<T> &step(const T &value)
84  {
85  delete m_step;
86  m_step = new T(value);
87  mDesc.extFlags |= Step;
88  return *this;
89  }
90 
99  ONBObject<T> &MMD(const T &minValue, const T &maxValue, const T &defValue)
100  {
101  return min(minValue).max(maxValue).def(defValue);
102  }
103 
114  ONBObject<T> &mime(const QString &value)
115  {
116  m_mimeType = value;
117  mDesc.extFlags |= MimeType;
118  return *this;
119  }
120 
131  ONBObject<T> &hint(const QString &value)
132  {
133  m_hint = value;
134  mDesc.extFlags |= Hint;
135  return *this;
136  }
137 
146  ONBObject<T> &unit(const QString &value)
147  {
148  m_unit = value;
149  mDesc.extFlags |= Unit;
150  return *this;
151  }
152 
157  ONBObject<T> &opt(const QString &value)
158  {
159  m_options = value;
160  mDesc.extFlags |= Options;
161  return *this;
162  }
163 
172  ONBObject<T> &sampling(std::chrono::milliseconds value)
173  {
174  m_extInfo.RMIP = value.count();
175  m_extInfo.needTimestamp = m_extInfo.RMIP > 0;
176  return *this;
177  }
178 
191  template<typename... Args>
192  ONBObject<T> &enumeration(QString s, Args... a)
193  {
194  m_enum << s;
195  return enumeration(a...);
196  }
197 
203  template<typename... Args>
204  ONBObject<T> &enumeration(QStringList list)
205  {
206  for (auto item: list)
207  m_enum << item;
208  return enumeration();
209  }
210 
211 protected:
213  virtual bool readMeta(QByteArray &ba, MetaValue id) const override
214  {
215  switch (id)
216  {
217  case MV_Min: return doRead(ba, m_min);
218  case MV_Max: return doRead(ba, m_max);
219  case MV_Def: return doRead(ba, m_def);
220  case MV_Step: return doRead(ba, m_step);
221  case MV_Mime: ba.append(m_mimeType.toUtf8()); return true;
222  case MV_Hint: ba.append(m_hint.toUtf8()); return true;
223  case MV_Unit: ba.append(m_unit.toUtf8()); return true;
224  case MV_Options: ba.append(m_options.toUtf8()); return true;
225  case MV_ExtInfo: ba.append(reinterpret_cast<const char*>(&m_extInfo), sizeof(ExtendedInfo)); return true;
226  case MV_Enum: ba.append(m_enum.join(",").toUtf8()); return true;
227  }
228  return false;
229  }
230 
231 private:
232  T *m_min = nullptr;
233  T *m_max = nullptr;
234  T *m_def = nullptr;
235  T *m_step = nullptr;
236  QString m_mimeType;
237  QString m_hint;
238  QString m_unit;
239  QString m_options;
240  QStringList m_enum;
241 
242  // TODO: do something with it:
243 // std::function<void(void)> m_onRequest, m_onReceive, m_onValueChange;
244 
245  ONBObject<T> &enumeration()
246  {
247  min(static_cast<T>(0));
248  max(static_cast<T>(m_enum.count() - 1));
249  mDesc.extFlags |= Enum;
250  return *this;
251  }
252 };
253 
254 #endif // ONBOBJECT_H
ONBObject< T > & sampling(std::chrono::milliseconds value)
Set the default sampling interval of the object.
Definition: onbobject.h:172
ONBObject< T > & enumeration(QString s, Args...a)
Specify the list of the enumeration object values.
Definition: onbobject.h:192
ONBObject< T > & def(const T &value)
Set the default value of the object.
Definition: onbobject.h:70
Component-side ONB Object.
Definition: ObjectInfo.h:9
ONBObject< T > & mime(const QString &value)
Set the mime-type of the object.
Definition: onbobject.h:114
ONBObject< T > & min(const T &value)
Set the minimum value of the object.
Definition: onbobject.h:46
ONB Object base interface.
Definition: ObjectInfo.h:16
ONBObject< T > & enumeration(QStringList list)
Specify the names of the enumeration object with given string list.
Definition: onbobject.h:204
ONBObject()
Default constructor.
Definition: onbobject.h:37
ONBObject< T > & max(const T &value)
Set the maximum value of the object.
Definition: onbobject.h:58
ONBObject< T > & step(const T &value)
Set the step value of the object.
Definition: onbobject.h:83
ONBObject< T > & hint(const QString &value)
Set the human-readable description of the object.
Definition: onbobject.h:131
ONBObject< T > & MMD(const T &minValue, const T &maxValue, const T &defValue)
Set the minimum, maximum and default value of the object.
Definition: onbobject.h:99
ONBObject< T > & unit(const QString &value)
Set the unit of the object.
Definition: onbobject.h:146
ONBObject< T > & opt(const QString &value)
Set the optional text associated to the object.
Definition: onbobject.h:157