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 
189  size_t printTo(Print& p) const;
190 
197  bool exportToStream(const Format& format, Print& output, const ExportOptions& options = {}) const
198  {
199  return format.exportToStream(*this, output, options);
200  }
201 
208  bool exportToFile(const Format& format, const String& filename, const ExportOptions& options = {}) const;
209 
216  {
217  return format.importFromStream(*this, source);
218  }
219 
225  Status importFromFile(const Format& format, const String& filename);
226 
227  const PropertyInfo& propinfo() const
228  {
229  return *propinfoPtr;
230  }
231 
232  const ObjectInfo& typeinfo() const
233  {
234  return *this ? *propinfoPtr->variant.object : ObjectInfo::empty;
235  }
236 
237  PropertyData* getPropertyData(unsigned index)
238  {
240  }
241 
242  const PropertyData* getPropertyData(unsigned index) const
243  {
245  }
246 
253  using UpdateCallback = Delegate<void(Store& store)>;
254 
258  void queueUpdate(UpdateCallback callback);
259 
260 protected:
261  StoreRef openStore(Database& db, unsigned storeIndex);
262  StoreUpdateRef openStoreForUpdate(Database& db, unsigned storeIndex);
263 
265  void initArrays();
266 
267  bool isWriteable() const;
268 
270 
271  bool writeCheck() const;
272 
273  void* getDataPtr();
274 
275  const void* getDataPtr() const;
276 
277  String getPropertyString(unsigned index, StringId id) const;
278 
279  String getPropertyString(unsigned index) const;
280 
281  StringId getStringId(const PropertyInfo& prop, const char* value, uint16_t valueLength);
282 
283  StringId getStringId(const PropertyInfo& prop, const String& value)
284  {
285  return value ? getStringId(prop, value.c_str(), value.length()) : 0;
286  }
287 
288  template <typename T> StringId getStringId(const PropertyInfo& prop, const T& value)
289  {
290  return getStringId(prop, toString(value));
291  }
292 
293  int findStringId(const char* value, uint16_t valueLength) const;
294 
295  void setPropertyValue(unsigned index, const void* value);
296  void setPropertyValue(unsigned index, const String& value);
297 
300  uint16_t dataRef{}; //< Relative to parent
301 
302 public:
303  uint16_t streamPos{}; //< Used during streaming
304 };
305 
310 template <class ClassType> class ObjectTemplate : public Object
311 {
312 public:
313  using Object::Object;
314 };
315 
321 template <class UpdaterType, class ClassType> class ObjectUpdaterTemplate : public ClassType
322 {
323 public:
324  using ClassType::ClassType;
325 
326  explicit operator bool() const
327  {
328  return Object::operator bool() && this->isWriteable();
329  }
330 };
331 
341 template <class UpdaterType, class DatabaseClassType, unsigned storeIndex, class ParentClassType, unsigned propIndex,
342  unsigned offset>
344 {
345 public:
347  : UpdaterType(*store, ParentClassType::typeinfo.getObject(propIndex), offset), store(store)
348  {
349  }
350 
351  explicit OuterObjectUpdaterTemplate(DatabaseClassType& db)
352  : OuterObjectUpdaterTemplate(this->openStoreForUpdate(db, storeIndex))
353  {
354  }
355 
360  std::unique_ptr<ImportStream> createImportStream(const Format& format)
361  {
362  return format.createImportStream(store, *this);
363  }
364 
365  explicit operator bool() const
366  {
367  return store && UpdaterType::operator bool();
368  }
369 
370 private:
371  StoreUpdateRef store;
372 };
373 
384 template <class ContainedClassType, class UpdaterType, class DatabaseClassType, unsigned storeIndex,
385  class ParentClassType, unsigned propIndex, unsigned offset>
387 {
388 public:
390  : ContainedClassType(*store, ParentClassType::typeinfo.getObject(propIndex), offset), store(store)
391  {
392  }
393 
394  OuterObjectTemplate(DatabaseClassType& db) : OuterObjectTemplate(this->openStore(db, storeIndex))
395  {
396  }
397 
404  std::unique_ptr<ExportStream> createExportStream(const Format& format, const ExportOptions& options = {}) const
405  {
406  return format.createExportStream(store, *this, options);
407  }
408 
409  using OuterUpdater =
411 
425  {
426  return OuterUpdater(this->lockStore(store));
427  }
428 
434  bool update(Delegate<void(UpdaterType)> callback)
435  {
436  if(auto upd = update()) {
437  callback(upd);
438  return true;
439  }
440  this->queueUpdate([callback](Store& store) {
441  callback(UpdaterType(store, ParentClassType::typeinfo.getObject(propIndex), offset));
442  });
443  return false;
444  }
445 
446 private:
447  StoreRef store;
448 };
449 
450 } // namespace ConfigDB
std::enable_if< std::is_integral< T >::value, String >::type toString(T value)
Definition: BitSet.h:481
Definition: Database.h:30
size_t exportToStream(const Object &object, Print &output, const ExportOptions &options) const override
Print object.
std::unique_ptr< ExportStream > createExportStream(Database &db, const ExportOptions &options) const override
Create a stream to serialize the entire database.
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:311
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:322
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:283
PropertyConst getProperty(unsigned index) const
Status importFromFile(const Format &format, const String &filename)
Import content to this object.
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:299
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
void queueUpdate(UpdateCallback callback)
Called from OuterObjectTemplate::update to queue an update.
const ObjectInfo & typeinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:232
void setPropertyValue(unsigned index, const String &value)
const PropertyInfo * propinfoPtr
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:298
const PropertyInfo & propinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:227
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:303
size_t printTo(Print &p) const
Support standard streaming output of this object's content in prettified JSON.
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:242
bool exportToFile(const Format &format, const String &filename, const ExportOptions &options={}) const
Export object to an output stream.
Database & getDatabase()
PropertyData * getPropertyData(unsigned index)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:237
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
bool exportToStream(const Format &format, Print &output, const ExportOptions &options={}) const
Export object to an output stream.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:197
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:288
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:300
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)
Import content to this object.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:215
int findStringId(const char *value, uint16_t valueLength) const
bool writeCheck() const
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:387
std::unique_ptr< ExportStream > createExportStream(const Format &format, const ExportOptions &options={}) const
Create a read-only stream for serializing object contents.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:404
OuterObjectTemplate(DatabaseClassType &db)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:394
OuterObjectUpdaterTemplate< UpdaterType, DatabaseClassType, storeIndex, ParentClassType, propIndex, offset > OuterUpdater
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:410
bool update(Delegate< void(UpdaterType)> callback)
Run an update asynchronously.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:434
OuterUpdater update()
Create an update object.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:424
OuterObjectTemplate(StoreRef store)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:389
Used by code generator.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:344
OuterObjectUpdaterTemplate(StoreUpdateRef store)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:346
OuterObjectUpdaterTemplate(DatabaseClassType &db)
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:351
std::unique_ptr< ImportStream > createImportStream(const Format &format)
Create a write-only stream for importing data to this object.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:360
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
Options for streaming object output.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Format.h:41
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