ConfigDB/src/include/ConfigDB/Store.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "Object.h"
23 #include "Array.h"
24 #include "ObjectArray.h"
25 #include "Union.h"
26 #include "Pool.h"
27 
28 #if DEBUG_VERBOSE_LEVEL == DBG
29 #include <debug_progmem.h>
30 #define CFGDB_DEBUG(fmt, ...) debug_e("[CFGDB] %s() %s %p" fmt, __FUNCTION__, getName().c_str(), this, ##__VA_ARGS__);
31 #else
32 #define CFGDB_DEBUG(...)
33 #endif
34 
35 namespace ConfigDB
36 {
37 class Database;
38 
42 class Store : public Object
43 {
44 public:
50  Store(Database& db) : Object(), db(db)
51  {
52  }
53 
60 
64  explicit Store(const Store& store);
65 
66  Store(Store&&) = delete;
67 
68  ~Store();
69 
71  {
72  String name = getName();
73  return name.length() ? name : F("_root");
74  }
75 
77 
79  {
80  return db;
81  }
82 
83  uint8_t* getRootData()
84  {
85  if(!writeCheck()) {
86  return nullptr;
87  }
88  dirty = true;
89  return rootData.get();
90  }
91 
92  const uint8_t* getRootData() const
93  {
94  return rootData.get();
95  }
96 
101  void clear();
102 
103  String getValueString(const PropertyInfo& info, const void* data) const;
104  bool parseString(const PropertyInfo& prop, PropertyData& dst, const PropertyData* defaultData, const char* value,
105  uint16_t valueLength);
106 
107  const StringPool& getStringPool() const
108  {
109  return stringPool;
110  }
111 
112  const ArrayPool& getArrayPool() const
113  {
114  return arrayPool;
115  }
116 
117  bool isLocked() const
118  {
119  return updaterCount != 0;
120  }
121 
125  static uint8_t getInstanceCount()
126  {
127  return instanceCount;
128  }
129 
130  bool writeCheck() const;
131 
132  using Object::exportToFile;
133 
134  bool exportToFile(const Format& format) const
135  {
136  String filename = getFilePath() + format.getFileExtension();
137  return exportToFile(format, filename);
138  }
139 
141 
143  {
144  String filename = getFilePath() + format.getFileExtension();
145  return importFromFile(format, filename);
146  }
147 
148  bool isDirty() const
149  {
150  return dirty;
151  }
152 
153  bool commit();
154 
155  static StoreUpdateRef lock(StoreRef& store);
156 
157 protected:
158  friend class Object;
159  friend class ArrayBase;
160  friend class Database;
161  friend class StoreRef;
162  friend class StoreUpdateRef;
163 
164  void checkRef(const StoreRef& ref);
165 
167 
168  void clearDirty()
169  {
170  dirty = false;
171  }
172 
173  void incUpdate();
174 
175  void decUpdate();
176 
179 
180 private:
181  Database& db;
182  std::unique_ptr<uint8_t[]> rootData;
183  uint8_t updaterCount{};
184  bool dirty{};
185  static uint8_t instanceCount;
186 };
187 
188 } // namespace ConfigDB
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:109
Base class to provide array of properties.
Definition: ArrayBase.h:31
This pool stores array data.
Definition: Pool.h:251
Definition: Database.h:30
String getFileExtension() const override
Get the standard file extension for the reader implementation.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Json/Format.h:43
An object can contain other objects, properties and arrays.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:37
Status importFromFile(const Format &format, const String &filename)
bool exportToFile(const Format &format, const String &filename) const
const PropertyInfo & propinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:202
String getName() const
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
void checkRef(const StoreRef &ref)
static uint8_t getInstanceCount()
Get number of valid (non-empty) Store instances in existence.
Definition: ConfigDB/src/include/ConfigDB/Store.h:125
Store(Store &&)=delete
Store(const Store &store)
Copy constructor.
ArrayPool arrayPool
Definition: ConfigDB/src/include/ConfigDB/Store.h:177
Status importFromFile(const Format &format, const String &filename)
bool isDirty() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:148
const ArrayPool & getArrayPool() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:112
String getFilePath() const
bool exportToFile(const Format &format, const String &filename) const
Store(Database &db, const PropertyInfo &propinfo)
Storage instance.
Store(Database &db)
This is an empty Store instance Object chain is built via references so need a valid Store instance w...
Definition: ConfigDB/src/include/ConfigDB/Store.h:50
String getValueString(const PropertyInfo &info, const void *data) const
bool exportToFile(const Format &format) const
Definition: ConfigDB/src/include/ConfigDB/Store.h:134
bool writeCheck() const
bool isLocked() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:117
Status importFromFile(const Format &format)
Definition: ConfigDB/src/include/ConfigDB/Store.h:142
void clear()
Reset store contents to defaults.
void queueUpdate(Object::UpdateCallback &&callback)
const StringPool & getStringPool() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:107
static StoreUpdateRef lock(StoreRef &store)
Database & getDatabase() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:78
uint8_t * getRootData()
Definition: ConfigDB/src/include/ConfigDB/Store.h:83
StringPool stringPool
Definition: ConfigDB/src/include/ConfigDB/Store.h:178
void clearDirty()
Definition: ConfigDB/src/include/ConfigDB/Store.h:168
const uint8_t * getRootData() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:92
String getFileName() const
Definition: ConfigDB/src/include/ConfigDB/Store.h:70
bool parseString(const PropertyInfo &prop, PropertyData &dst, const PropertyData *defaultData, const char *value, uint16_t valueLength)
Pool for string data.
Definition: Pool.h:176
The String class.
Definition: WString.h:133
Definition: Array.h:26
Definition: Formatter.h:20
Property metadata.
Definition: PropertyInfo.h:112
Definition: Status.h:54
Definition: PropertyData.h:44