Pool.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "Object.h"
23 
24 namespace ConfigDB
25 {
29 class PoolData
30 {
31 public:
33  {
34  assert(itemSize > 0 && itemSize <= 255);
35  }
36 
37  PoolData(const PoolData& other)
38  {
39  *this = other;
40  }
41 
42  PoolData(PoolData&& other);
43 
44  PoolData& operator=(const PoolData& other);
45 
46  size_t getCount() const
47  {
48  return count;
49  }
50 
51  size_t getCapacity() const
52  {
53  return count + space;
54  }
55 
56  size_t getItemSize() const
57  {
58  return itemSize;
59  }
60 
61  size_t getItemSize(size_t count) const
62  {
63  return count * itemSize;
64  }
65 
66  void* operator[](unsigned index)
67  {
68  return getItemPtr(index);
69  }
70 
71  const void* operator[](unsigned index) const
72  {
73  return getItemPtr(index);
74  }
75 
76  void clear()
77  {
78  free(buffer);
79  buffer = nullptr;
80  count = space = 0;
81  }
82 
83  bool ensureCapacity(size_t capacity);
84 
85  size_t usage() const
86  {
87  return getCapacity() * itemSize;
88  }
89 
90 protected:
91  void* allocate(size_t itemCount);
92  void deallocate(size_t itemCount);
93 
94  void* getItemPtr(unsigned index)
95  {
96  assert(index < count);
97  return static_cast<uint8_t*>(buffer) + getItemSize(index);
98  }
99 
100  const void* getItemPtr(unsigned index) const
101  {
102  assert(index < count);
103  return static_cast<const uint8_t*>(buffer) + getItemSize(index);
104  }
105 
106  void* buffer{};
107  uint16_t count{0};
108  uint8_t space{0};
109  uint8_t itemSize;
110 };
111 
116 public:
117  const char* value{};
118  uint16_t length{};
119 
120  CountedString() = default;
121 
122  CountedString(const char* value, uint16_t length) : value(value), length(length)
123  {
124  }
125 
126  explicit CountedString(const String& s) : value(s.c_str()), length(s.length())
127  {
128  }
129 
130  bool operator==(const CountedString& other) const
131  {
132  return length == other.length && memcmp(value, other.value, length) == 0;
133  }
134 
135  explicit operator bool() const
136  {
137  return value && length;
138  }
139 
140  explicit operator String() const
141  {
142  return String(value, length);
143  }
144 
148  uint16_t getStorageSize() const
149  {
150  return 1 + (length > 0x80) + length;
151  }
152 };
153 
175 class StringPool : public PoolData
176 {
177 public:
179  {
180  }
181 
183  {
184  clear();
185  }
186 
191  StringId find(const CountedString& string) const;
192 
193  StringId add(const CountedString& string);
194 
196  {
197  return find(string) ?: add(string);
198  }
199 
201  {
202  unsigned offset = ref - 1;
203  return offset < count ? getString(offset) : CountedString{};
204  }
205 
206  const char* getBuffer() const
207  {
208  return static_cast<const char*>(buffer);
209  }
210 
211 private:
212  CountedString getString(unsigned offset) const;
213 };
214 
221 class ArrayData : public PoolData
222 {
223 public:
224  using PoolData::PoolData;
225 
226  void* insert(unsigned index, const void* data = nullptr, size_t itemCount = 1);
227 
228  void* add(const void* data = nullptr, size_t itemCount = 1)
229  {
230  return insert(getCount(), data, itemCount);
231  }
232 
233  bool remove(unsigned index);
234 
238  void dispose()
239  {
240  clear();
241  itemSize = 0;
242  }
243 };
244 
250 class ArrayPool : public PoolData
251 {
252 public:
254  {
255  }
256 
257  ArrayPool(const ArrayPool& other);
258 
260  {
261  clear();
262  }
263 
264  ArrayId add(const ObjectInfo& object)
265  {
266  return add(object.structSize);
267  }
268 
269  ArrayId add(const PropertyInfo& prop)
270  {
271  return add(prop.getSize());
272  }
273 
275  {
276  return *static_cast<ArrayData*>(getItemPtr(id - 1));
277  }
278 
279  const ArrayData& operator[](ArrayId id) const
280  {
281  return *static_cast<const ArrayData*>(getItemPtr(id - 1));
282  }
283 
284  void clear();
285 
286  size_t usage() const
287  {
288  size_t n = PoolData::usage();
289  auto data = static_cast<const ArrayData*>(buffer);
290  for(unsigned i = 0; i < count; ++i) {
291  n += data->usage();
292  ++data;
293  }
294  return n;
295  }
296 
297 private:
298  ArrayId add(size_t itemSize);
299 };
300 
301 } // namespace ConfigDB
An array of fixed-sized items.
Definition: Pool.h:222
void dispose()
Clear and mark this array as 'not in use' so it can be re-used.
Definition: Pool.h:238
void * insert(unsigned index, const void *data=nullptr, size_t itemCount=1)
void * add(const void *data=nullptr, size_t itemCount=1)
Definition: Pool.h:228
bool remove(unsigned index)
This pool stores array data.
Definition: Pool.h:251
ArrayId add(const PropertyInfo &prop)
Definition: Pool.h:269
ArrayPool()
Definition: Pool.h:253
~ArrayPool()
Definition: Pool.h:259
ArrayId add(const ObjectInfo &object)
Definition: Pool.h:264
const ArrayData & operator[](ArrayId id) const
Definition: Pool.h:279
size_t usage() const
Definition: Pool.h:286
ArrayPool(const ArrayPool &other)
ArrayData & operator[](ArrayId id)
Definition: Pool.h:274
Base allocator, designed for good reallocation performance and low RAM footprint.
Definition: Pool.h:30
uint16_t count
Definition: Pool.h:107
size_t getCount() const
Definition: Pool.h:46
PoolData(const PoolData &other)
Definition: Pool.h:37
bool ensureCapacity(size_t capacity)
void * allocate(size_t itemCount)
void * getItemPtr(unsigned index)
Definition: Pool.h:94
void * buffer
Definition: Pool.h:106
PoolData(PoolData &&other)
PoolData & operator=(const PoolData &other)
uint8_t space
Definition: Pool.h:108
void clear()
Definition: Pool.h:76
size_t getCapacity() const
Definition: Pool.h:51
size_t usage() const
Definition: Pool.h:85
const void * operator[](unsigned index) const
Definition: Pool.h:71
PoolData(size_t itemSize)
Definition: Pool.h:32
size_t getItemSize(size_t count) const
Definition: Pool.h:61
void deallocate(size_t itemCount)
void * operator[](unsigned index)
Definition: Pool.h:66
uint8_t itemSize
Definition: Pool.h:109
size_t getItemSize() const
Definition: Pool.h:56
const void * getItemPtr(unsigned index) const
Definition: Pool.h:100
Pool for string data.
Definition: Pool.h:176
StringPool()
Definition: Pool.h:178
const char * getBuffer() const
Definition: Pool.h:206
~StringPool()
Definition: Pool.h:182
StringId add(const CountedString &string)
StringId find(const CountedString &string) const
Search for a string.
CountedString operator[](StringId ref) const
Definition: Pool.h:200
StringId findOrAdd(const CountedString &string)
Definition: Pool.h:195
The String class.
Definition: WString.h:133
Definition: Array.h:26
uint16_t StringId
Defines contained string data using index into string pool.
Definition: PropertyInfo.h:60
Identifies array storage within array pool.
Definition: PropertyData.h:31
Used by StringPool.
Definition: Pool.h:115
CountedString(const char *value, uint16_t length)
Definition: Pool.h:122
bool operator==(const CountedString &other) const
Definition: Pool.h:130
uint16_t length
Definition: Pool.h:118
CountedString(const String &s)
Definition: Pool.h:126
const char * value
Definition: Pool.h:117
uint16_t getStorageSize() const
Get buffer size required to store this string.
Definition: Pool.h:148
Definition: ObjectInfo.h:38
Property metadata.
Definition: PropertyInfo.h:112
uint8_t getSize() const
Get number of bytes required to store this property value within a structure.
Definition: PropertyInfo.h:172