Libraries/ConfigDB/src/include/ConfigDB/Object.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "Property.h"
23 #include "ObjectInfo.h"
24 #include "StoreRef.h"
25 #include "Format.h"
26 
27 namespace ConfigDB
28 {
29 class Database;
30 class Store;
31 
36 class Object
37 {
38 public:
40  {
41  }
42 
43  Object(const Object& other)
44  {
45  *this = other;
46  }
47 
48  Object(Object&&) = delete;
49 
50  Object& operator=(const Object& other);
51 
53  {
54  }
55 
56  Object(Object& parent, const PropertyInfo& prop, uint16_t dataRef)
58  {
59  }
60 
61  Object(const Object& parent, const PropertyInfo& prop, uint16_t dataRef)
62  : Object(const_cast<Object&>(parent), prop, dataRef)
63  {
64  }
65 
66  Object(Object& parent, unsigned propIndex, uint16_t dataRef = 0)
67  : Object(parent, parent.typeinfo().getObject(propIndex), dataRef)
68  {
69  }
70 
71  Object(const Object& parent, unsigned propIndex, uint16_t dataRef = 0)
72  : Object(const_cast<Object&>(parent), propIndex, dataRef)
73  {
74  }
75 
76  explicit operator bool() const
77  {
78  return propinfoPtr->type == PropertyType::Object;
79  }
80 
81  bool typeIs(ObjectType type) const
82  {
83  return *this && typeinfo().type == type;
84  }
85 
86  bool isArray() const
87  {
88  return typeinfo().isArray();
89  }
90 
94  bool isStore() const
95  {
96  return !parent;
97  }
98 
100 
101  const Store& getStore() const
102  {
103  return const_cast<Object*>(this)->getStore();
104  }
105 
107 
108  const Database& getDatabase() const
109  {
110  return const_cast<Object*>(this)->getDatabase();
111  }
112 
117  unsigned getObjectCount() const;
118 
122  Object getObject(unsigned index);
123 
124  const Object getObject(unsigned index) const
125  {
126  return const_cast<Object*>(this)->getObject(index);
127  }
128 
134  Object findObject(const char* name, size_t length);
135 
140  unsigned getPropertyCount() const;
141 
146  Property getProperty(unsigned index);
147 
148  PropertyConst getProperty(unsigned index) const;
149 
153  Property findProperty(const char* name, size_t length);
154 
159  void clear();
160 
165 
170 
174  bool commit();
175 
180  void clearDirty();
181 
182  String getName() const;
183 
184  String getPath() const;
185 
186  size_t printTo(Print& p) const;
187 
188  bool exportToStream(const Format& format, Print& output) const
189  {
190  return format.exportToStream(*this, output);
191  }
192 
193  bool exportToFile(const Format& format, const String& filename) const;
194 
196  {
197  return format.importFromStream(*this, source);
198  }
199 
200  Status importFromFile(const Format& format, const String& filename);
201 
202  const PropertyInfo& propinfo() const
203  {
204  return *propinfoPtr;
205  }
206 
207  const ObjectInfo& typeinfo() const
208  {
209  return *this ? *propinfoPtr->variant.object : ObjectInfo::empty;
210  }
211 
212  PropertyData* getPropertyData(unsigned index)
213  {
215  }
216 
217  const PropertyData* getPropertyData(unsigned index) const
218  {
220  }
221 
228  using UpdateCallback = Delegate<void(Store& store)>;
229 
233  void queueUpdate(UpdateCallback callback);
234 
235 protected:
236  StoreRef openStore(Database& db, unsigned storeIndex);
237  StoreUpdateRef openStoreForUpdate(Database& db, unsigned storeIndex);
238 
240  void initArrays();
241 
242  bool isWriteable() const;
243 
245 
246  bool writeCheck() const;
247 
248  void* getDataPtr();
249 
250  const void* getDataPtr() const;
251 
252  String getPropertyString(unsigned index, StringId id) const;
253 
254  String getPropertyString(unsigned index) const;
255 
256  StringId getStringId(const PropertyInfo& prop, const char* value, uint16_t valueLength);
257 
258  StringId getStringId(const PropertyInfo& prop, const String& value)
259  {
260  return value ? getStringId(prop, value.c_str(), value.length()) : 0;
261  }
262 
263  template <typename T> StringId getStringId(const PropertyInfo& prop, const T& value)
264  {
265  return getStringId(prop, toString(value));
266  }
267 
268  int findStringId(const char* value, uint16_t valueLength) const;
269 
270  void setPropertyValue(unsigned index, const void* value);
271  void setPropertyValue(unsigned index, const String& value);
272 
275  uint16_t dataRef{}; //< Relative to parent
276 
277 public:
278  uint16_t streamPos{}; //< Used during streaming
279 };
280 
285 template <class ClassType> class ObjectTemplate : public Object
286 {
287 public:
288  using Object::Object;
289 };
290 
296 template <class UpdaterType, class ClassType> class ObjectUpdaterTemplate : public ClassType
297 {
298 public:
299  using ClassType::ClassType;
300 
301  explicit operator bool() const
302  {
303  return Object::operator bool() && this->isWriteable();
304  }
305 };
306 
316 template <class UpdaterType, class DatabaseClassType, unsigned storeIndex, class ParentClassType, unsigned propIndex,
317  unsigned offset>
319 {
320 public:
322  : UpdaterType(*store, ParentClassType::typeinfo.getObject(propIndex), offset), store(store)
323  {
324  }
325 
326  explicit OuterObjectUpdaterTemplate(DatabaseClassType& db)
327  : OuterObjectUpdaterTemplate(this->openStoreForUpdate(db, storeIndex))
328  {
329  }
330 
331  std::unique_ptr<ImportStream> createImportStream(const Format& format)
332  {
333  return format.createImportStream(store, *this);
334  }
335 
336  explicit operator bool() const
337  {
338  return store && UpdaterType::operator bool();
339  }
340 
341 private:
342  StoreUpdateRef store;
343 };
344 
355 template <class ContainedClassType, class UpdaterType, class DatabaseClassType, unsigned storeIndex,
356  class ParentClassType, unsigned propIndex, unsigned offset>
358 {
359 public:
361  : ContainedClassType(*store, ParentClassType::typeinfo.getObject(propIndex), offset), store(store)
362  {
363  }
364 
365  OuterObjectTemplate(DatabaseClassType& db) : OuterObjectTemplate(this->openStore(db, storeIndex))
366  {
367  }
368 
369  std::unique_ptr<ExportStream> createExportStream(const Format& format) const
370  {
371  return format.createExportStream(store, *this);
372  }
373 
374  using OuterUpdater =
376 
390  {
391  return OuterUpdater(this->lockStore(store));
392  }
393 
399  bool update(Delegate<void(UpdaterType)> callback)
400  {
401  if(auto upd = update()) {
402  callback(upd);
403  return true;
404  }
405  this->queueUpdate([callback](Store& store) {
406  callback(UpdaterType(store, ParentClassType::typeinfo.getObject(propIndex), offset));
407  });
408  return false;
409  }
410 
411 private:
412  StoreRef store;
413 };
414 
415 } // namespace ConfigDB
std::enable_if< std::is_integral< T >::value, String >::type toString(T value)
Definition: BitSet.h:481
Definition: Database.h:30
std::unique_ptr< ExportStream > createExportStream(Database &db) const override
Create a stream to serialize the entire database This is used for streaming asychronously to a web cl...
size_t exportToStream(const Object &object, Print &output) const override
Print object.
std::unique_ptr< ImportStream > createImportStream(Database &db) const override
Create a stream for de-serialising (writing) into the database Used when updating a database from a r...
Status importFromStream(Object &object, Stream &source) const override
De-serialise content from stream into object (RAM)
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:286
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:297
An object can contain other objects, properties and arrays.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:37
const Store & getStore() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:101
Object & operator=(const Object &other)
const Database & getDatabase() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:108
StringId getStringId(const PropertyInfo &prop, const String &value)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:258
PropertyConst getProperty(unsigned index) const
Status importFromFile(const Format &format, const String &filename)
bool typeIs(ObjectType type) const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:81
bool commit()
Commit changes to the store.
Object()
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:39
Object * parent
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:274
bool isArray() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:86
Object(const Object &parent, unsigned propIndex, uint16_t dataRef=0)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:71
void setPropertyValue(unsigned index, const void *value)
String getPath() const
Object(const Object &other)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:43
StoreUpdateRef lockStore(StoreRef &store)
unsigned getObjectCount() const
Get number of child objects.
bool isWriteable() const
bool exportToFile(const Format &format, const String &filename) const
void queueUpdate(UpdateCallback callback)
Called from OuterObjectTemplate::update to queue an update.
const ObjectInfo & typeinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:207
bool exportToStream(const Format &format, Print &output) const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:188
void setPropertyValue(unsigned index, const String &value)
const PropertyInfo * propinfoPtr
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:273
const PropertyInfo & propinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:202
void loadArrayDefaults()
Clear and load all contained arrays with defaults from schema.
Object(Object &parent, unsigned propIndex, uint16_t dataRef=0)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:66
uint16_t streamPos
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:278
size_t printTo(Print &p) const
Object(const PropertyInfo &propinfo)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:52
String getName() const
const PropertyData * getPropertyData(unsigned index) const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:217
Database & getDatabase()
PropertyData * getPropertyData(unsigned index)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:212
Object(Object &parent, const PropertyInfo &prop, uint16_t dataRef)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:56
void clearDirty()
Clear store dirty flag so changes don't get committed.
Store & getStore()
Property findProperty(const char *name, size_t length)
Find property by name.
bool isStore() const
Determine if this object is a store (not just a reference to it)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:94
StoreRef openStore(Database &db, unsigned storeIndex)
String getPropertyString(unsigned index) const
StringId getStringId(const PropertyInfo &prop, const T &value)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:263
StoreUpdateRef openStoreForUpdate(Database &db, unsigned storeIndex)
const Object getObject(unsigned index) const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:124
uint16_t dataRef
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:275
Object findObject(const char *name, size_t length)
Find child object by name.
Object(const Object &parent, const PropertyInfo &prop, uint16_t dataRef)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:61
StringId getStringId(const PropertyInfo &prop, const char *value, uint16_t valueLength)
unsigned getPropertyCount() const
Get number of properties.
Property getProperty(unsigned index)
Get properties.
void resetToDefaults()
Does a 'clear' followed by 'loadArrayDefaults'.
void * getDataPtr()
Object(Object &&)=delete
const void * getDataPtr() const
void clear()
Reset contents to defaults (except arrays, which are cleared)
String getPropertyString(unsigned index, StringId id) const
Object getObject(unsigned index)
Get child object by index.
Status importFromStream(const Format &format, Stream &source)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:195
int findStringId(const char *value, uint16_t valueLength) const
bool writeCheck() const
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:358
OuterObjectTemplate(DatabaseClassType &db)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:365
OuterObjectUpdaterTemplate< UpdaterType, DatabaseClassType, storeIndex, ParentClassType, propIndex, offset > OuterUpdater
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:375
bool update(Delegate< void(UpdaterType)> callback)
Run an update asynchronously.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:399
OuterUpdater update()
Create an update object.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:389
std::unique_ptr< ExportStream > createExportStream(const Format &format) const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:369
OuterObjectTemplate(StoreRef store)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:360
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:319
OuterObjectUpdaterTemplate(StoreUpdateRef store)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:321
OuterObjectUpdaterTemplate(DatabaseClassType &db)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:326
std::unique_ptr< ImportStream > createImportStream(const Format &format)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:331
Manages a key/value pair stored in an object, or a simple array value.
Definition: Property.h:32
Definition: Property.h:69
Definition: StoreRef.h:29
Definition: StoreRef.h:46
Manages access to an object store, typically one file.
Definition: ConfigDB/src/include/ConfigDB/Store.h:43
Provides formatted output to stream.
Definition: Print.h:37
Base Stream class.
Definition: Wiring/Stream.h:33
The String class.
Definition: WString.h:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:238
Definition: Array.h:26
ObjectType
Definition: ObjectInfo.h:32
uint16_t StringId
Defines contained string data using index into string pool.
Definition: PropertyInfo.h:60
Definition: Formatter.h:20
Definition: ObjectInfo.h:38
static const ObjectInfo empty
Definition: ObjectInfo.h:47
ObjectType type
Definition: ObjectInfo.h:39
bool isArray() const
Definition: ObjectInfo.h:49
Property metadata.
Definition: PropertyInfo.h:112
Variant variant
Definition: PropertyInfo.h:149
PropertyType type
Definition: PropertyInfo.h:146
Definition: Status.h:54
Definition: PropertyData.h:44
static PropertyData * fromStruct(const PropertyInfo &prop, void *data)
Definition: PropertyData.h:68
const ObjectInfo * object
Definition: PropertyInfo.h:137