Array.h
Go to the documentation of this file.
1 /****
2  * ConfigDB/Array.h
3  *
4  * Copyright 2024 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the ConfigDB Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "ArrayBase.h"
23 #include "ArrayIterator.h"
24 
25 namespace ConfigDB
26 {
30 class Array : public ArrayBase
31 {
32 public:
33  using ArrayBase::ArrayBase;
34 
35  unsigned getPropertyCount() const
36  {
37  return getItemCount();
38  }
39 
40  Property getProperty(unsigned index)
41  {
42  return makeProperty(getArray()[index]);
43  }
44 
46  {
47  return makeProperty(getArray().add());
48  }
49 
50  Property insertItem(unsigned index)
51  {
52  return makeProperty(getArray().insert(index));
53  }
54 
55  PropertyConst getProperty(unsigned index) const
56  {
57  return makeProperty(getArray()[index]);
58  }
59 
60  const PropertyInfo& getItemType() const
61  {
62  assert(typeinfo().propertyCount == 1);
63  return typeinfo().propinfo[0];
64  }
65 
66 protected:
67  friend class Object;
68 
69  void loadDefaults();
70 
71  StringId getStringId(const String& value)
72  {
73  return ArrayBase::getStringId(getItemType(), value);
74  }
75 
76  template <typename T> void addItem(T value)
77  {
78  PropertyData dst{};
79  dst.setValue(getItemType(), value);
80  this->getArray().add(&dst);
81  }
82 
83  template <typename T> void insertItem(unsigned index, T value)
84  {
85  PropertyData dst{};
86  dst.setValue(getItemType(), value);
87  this->getArray().insert(index, &dst);
88  }
89 
90  template <typename T> void setItem(unsigned index, T value)
91  {
92  auto dst = static_cast<PropertyData*>(ArrayBase::getItem(index));
93  dst->setValue(getItemType(), value);
94  }
95 
96  int indexOf(const void* value) const
97  {
98  auto itemSize = getItemType().getSize();
99  auto& array = getArray();
100  for(unsigned i = 0; i < array.getCount(); ++i) {
101  if(memcmp(array[i], value, itemSize) == 0) {
102  return i;
103  }
104  }
105  return -1;
106  }
107 
108 private:
109  Property makeProperty(void* data)
110  {
111  return {getStore(), getItemType(), static_cast<PropertyData*>(data), nullptr};
112  }
113 
114  PropertyConst makeProperty(const void* data) const
115  {
116  return {getStore(), getItemType(), static_cast<const PropertyData*>(data)};
117  }
118 };
119 
125 template <class ClassType, typename ItemType> class ArrayTemplate : public Array
126 {
127 public:
129 
130  using Array::Array;
131 
132  ItemType getItem(unsigned index) const
133  {
134  return *static_cast<const ItemType*>(getArray()[index]);
135  }
136 
137  const ItemType operator[](unsigned index) const
138  {
139  return static_cast<const ClassType*>(this)->getItem(index);
140  }
141 
142  int indexOf(ItemType item) const
143  {
144  return Array::indexOf(&item);
145  }
146 
147  bool contains(ItemType item) const
148  {
149  return indexOf(item) >= 0;
150  }
151 
152  Iterator begin() const
153  {
154  return Iterator(*this, 0);
155  }
156 
157  Iterator end() const
158  {
159  return Iterator(*this, this->getItemCount());
160  }
161 };
162 
171 template <class UpdaterType, class ClassType, typename ItemType, typename ItemSetType, typename ItemCastType>
173 {
174 public:
175  struct ItemRef {
177  unsigned index;
178 
179  operator ItemType() const
180  {
181  return static_cast<UpdaterType&>(array).getItem(index);
182  }
183 
184  ItemRef& operator=(ItemSetType value)
185  {
186  static_cast<UpdaterType&>(array).setItem(index, ItemCastType(value));
187  return *this;
188  }
189  };
190 
192 
193  using ClassType::ClassType;
194 
195  void setItem(unsigned index, ItemSetType value)
196  {
197  Array::setItem(index, ItemCastType(value));
198  }
199 
200  void addItem(ItemSetType value)
201  {
202  Array::addItem(ItemCastType(value));
203  }
204 
205  void insertItem(unsigned index, ItemSetType value)
206  {
207  Array::insertItem(index, ItemCastType(value));
208  }
209 
210  ItemRef operator[](unsigned index)
211  {
212  return {*this, index};
213  }
214 
216  {
217  return Iterator(*this, 0);
218  }
219 
221  {
222  return Iterator(*this, this->getItemCount());
223  }
224 };
225 
231 template <class ClassType, typename ItemType> class StringArrayTemplate : public ArrayTemplate<ClassType, ItemType>
232 {
233 public:
235 
236  String getItem(unsigned index) const
237  {
238  auto id = *static_cast<const StringId*>(this->getArray()[index]);
239  return this->getPropertyString(0, id);
240  }
241 
242  int indexOf(const String& item) const
243  {
244  int stringId = this->findStringId(item.c_str(), item.length());
245  return Array::indexOf(&stringId);
246  }
247 
248  bool contains(const String& item) const
249  {
250  return indexOf(item) >= 0;
251  }
252 };
253 
260 template <class UpdaterType, class ClassType, typename ItemType, typename ItemSetType, typename ItemCastType>
262  : public ArrayUpdaterTemplate<UpdaterType, ClassType, ItemType, ItemSetType, ItemCastType>
263 {
264 public:
266 
267  void setItem(unsigned index, ItemType value)
268  {
269  Array::setItem(index, this->getStringId(value));
270  }
271 
272  void addItem(ItemSetType value)
273  {
274  Array::addItem(this->getStringId(value));
275  }
276 
277  void insertItem(unsigned index, ItemSetType value)
278  {
279  Array::insertItem(index, this->getStringId(value));
280  }
281 };
282 
283 } // namespace ConfigDB
Base class to provide array of properties.
Definition: ArrayBase.h:31
unsigned getItemCount() const
Definition: ArrayBase.h:35
void * getItem(unsigned index)
Definition: ArrayBase.h:67
ArrayData & getArray()
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:231
Array iterator support.
Definition: ArrayIterator.h:28
Used by code generator for integral-typed arrays.
Definition: Array.h:126
Iterator end() const
Definition: Array.h:157
bool contains(ItemType item) const
Definition: Array.h:147
ItemType getItem(unsigned index) const
Definition: Array.h:132
const ArrayIterator< const ArrayTemplate, const ItemType > Iterator
Definition: Array.h:128
Iterator begin() const
Definition: Array.h:152
int indexOf(ItemType item) const
Definition: Array.h:142
const ItemType operator[](unsigned index) const
Definition: Array.h:137
Used by code generator for integral-typed arrays.
Definition: Array.h:173
Iterator end()
Definition: Array.h:220
Iterator begin()
Definition: Array.h:215
void insertItem(unsigned index, ItemSetType value)
Definition: Array.h:205
void setItem(unsigned index, ItemSetType value)
Definition: Array.h:195
ItemRef operator[](unsigned index)
Definition: Array.h:210
void addItem(ItemSetType value)
Definition: Array.h:200
ArrayIterator< ArrayUpdaterTemplate, ItemRef > Iterator
Definition: Array.h:191
Base class to provide array of properties.
Definition: Array.h:31
Property getProperty(unsigned index)
Definition: Array.h:40
const PropertyInfo & getItemType() const
Definition: Array.h:60
StringId getStringId(const String &value)
Definition: Array.h:71
Property addItem()
Definition: Array.h:45
int indexOf(const void *value) const
Definition: Array.h:96
void insertItem(unsigned index, T value)
Definition: Array.h:83
Property insertItem(unsigned index)
Definition: Array.h:50
void setItem(unsigned index, T value)
Definition: Array.h:90
unsigned getPropertyCount() const
Definition: Array.h:35
void addItem(T value)
Definition: Array.h:76
PropertyConst getProperty(unsigned index) const
Definition: Array.h:55
An object can contain other objects, properties and arrays.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:37
const ObjectInfo & typeinfo() const
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:232
Store & getStore()
StringId getStringId(const PropertyInfo &prop, const char *value, uint16_t valueLength)
String getPropertyString(unsigned index, StringId id) const
int findStringId(const char *value, uint16_t valueLength) const
Read-only access to a key/value pair stored in an object, or a simple array value.
Definition: Property.h:32
Adds write access for a key/value pair stored in an object, or a simple array value.
Definition: Property.h:80
Used by code generator for String-typed arrays.
Definition: Array.h:232
String getItem(unsigned index) const
Definition: Array.h:236
int indexOf(const String &item) const
Definition: Array.h:242
bool contains(const String &item) const
Definition: Array.h:248
Used by code generator for String-typed arrays.
Definition: Array.h:263
void addItem(ItemSetType value)
Definition: Array.h:272
void setItem(unsigned index, ItemType value)
Definition: Array.h:267
void insertItem(unsigned index, ItemSetType value)
Definition: Array.h:277
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
uint16_t StringId
Defines contained string data using index into string pool.
Definition: PropertyInfo.h:59
FSTR::Array< Ssl::CipherSuite > Array
Definition: CipherSuite.h:182
Array & array
Definition: Array.h:176
ItemRef & operator=(ItemSetType value)
Definition: Array.h:184
unsigned index
Definition: Array.h:177
const PropertyInfo propinfo[]
Definition: ObjectInfo.h:45
Property metadata.
Definition: PropertyInfo.h:126
uint8_t getSize() const
Get number of bytes required to store this property value within a structure.
Definition: PropertyInfo.h:177
Definition: PropertyData.h:44
bool setValue(const PropertyInfo &prop, int64_t value)