Array.h
Go to the documentation of this file.
1 
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  void addItem(const void* value)
77  {
78  PropertyData dst{};
79  dst.setValue(getItemType(), *static_cast<const PropertyData*>(value));
80  this->getArray().add(&dst);
81  }
82 
83  void insertItem(unsigned index, const void* value)
84  {
85  PropertyData dst{};
86  dst.setValue(getItemType(), *static_cast<const PropertyData*>(value));
87  this->getArray().insert(index, &dst);
88  }
89 
90  void setItem(unsigned index, const void* value)
91  {
92  auto dst = static_cast<PropertyData*>(ArrayBase::getItem(index));
93  dst->setValue(getItemType(), *static_cast<const PropertyData*>(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), nullptr};
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 
169 template <class UpdaterType, class ClassType, typename ItemType> class ArrayUpdaterTemplate : public ClassType
170 {
171 public:
172  struct ItemRef {
174  unsigned index;
175 
176  operator ItemType() const
177  {
178  return static_cast<UpdaterType&>(array).getItem(index);
179  }
180 
181  ItemRef& operator=(const ItemType& value)
182  {
183  static_cast<UpdaterType&>(array).setItem(index, value);
184  return *this;
185  }
186  };
187 
189 
190  using ClassType::ClassType;
191 
192  void setItem(unsigned index, ItemType value)
193  {
194  Array::setItem(index, &value);
195  }
196 
197  void addItem(ItemType value)
198  {
199  Array::addItem(&value);
200  }
201 
202  void insertItem(unsigned index, ItemType value)
203  {
204  Array::insertItem(index, &value);
205  }
206 
207  ItemRef operator[](unsigned index)
208  {
209  return {*this, index};
210  }
211 
213  {
214  return Iterator(*this, 0);
215  }
216 
218  {
219  return Iterator(*this, this->getItemCount());
220  }
221 };
222 
228 template <class ClassType, typename ItemType> class StringArrayTemplate : public ArrayTemplate<ClassType, ItemType>
229 {
230 public:
232 
233  String getItem(unsigned index) const
234  {
235  auto id = *static_cast<const StringId*>(this->getArray()[index]);
236  return this->getPropertyString(0, id);
237  }
238 
239  int indexOf(const String& item) const
240  {
241  int stringId = this->findStringId(item.c_str(), item.length());
242  return Array::indexOf(&stringId);
243  }
244 
245  bool contains(const String& item) const
246  {
247  return indexOf(item) >= 0;
248  }
249 };
250 
257 template <class UpdaterType, class ClassType, typename ItemType>
258 class StringArrayUpdaterTemplate : public ArrayUpdaterTemplate<UpdaterType, ClassType, ItemType>
259 {
260 public:
262 
263  void setItem(unsigned index, const ItemType& value)
264  {
265  auto stringId = this->getStringId(value);
266  Array::setItem(index, &stringId);
267  }
268 
269  void addItem(const ItemType& value)
270  {
271  auto stringId = this->getStringId(value);
272  Array::addItem(&stringId);
273  }
274 
275  void insertItem(unsigned index, const ItemType& value)
276  {
277  auto stringId = this->getStringId(value);
278  Array::insertItem(index, &stringId);
279  }
280 };
281 
282 } // 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:45
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:228
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:170
Iterator begin()
Definition: Array.h:212
void addItem(ItemType value)
Definition: Array.h:197
ArrayIterator< ArrayUpdaterTemplate, ItemRef > Iterator
Definition: Array.h:188
ItemRef operator[](unsigned index)
Definition: Array.h:207
void setItem(unsigned index, ItemType value)
Definition: Array.h:192
void insertItem(unsigned index, ItemType value)
Definition: Array.h:202
Iterator end()
Definition: Array.h:217
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
void addItem(const void *value)
Definition: Array.h:76
int indexOf(const void *value) const
Definition: Array.h:96
Property insertItem(unsigned index)
Definition: Array.h:50
unsigned getPropertyCount() const
Definition: Array.h:35
PropertyConst getProperty(unsigned index) const
Definition: Array.h:55
void insertItem(unsigned index, const void *value)
Definition: Array.h:83
void setItem(unsigned index, const void *value)
Definition: Array.h:90
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
Manages a key/value pair stored in an object, or a simple array value.
Definition: Property.h:32
Definition: Property.h:69
Used by code generator for String-typed arrays.
Definition: Array.h:229
String getItem(unsigned index) const
Definition: Array.h:233
int indexOf(const String &item) const
Definition: Array.h:239
bool contains(const String &item) const
Definition: Array.h:245
Used by code generator for String-typed arrays.
Definition: Array.h:259
void setItem(unsigned index, const ItemType &value)
Definition: Array.h:263
void insertItem(unsigned index, const ItemType &value)
Definition: Array.h:275
void addItem(const ItemType &value)
Definition: Array.h:269
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:60
FSTR::Array< Ssl::CipherSuite > Array
Definition: CipherSuite.h:182
unsigned index
Definition: Array.h:174
Array & array
Definition: Array.h:173
ItemRef & operator=(const ItemType &value)
Definition: Array.h:181
const PropertyInfo propinfo[]
Definition: ObjectInfo.h:45
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
Definition: PropertyData.h:44
void setValue(const PropertyInfo &prop, const PropertyData &src)
Range-check raw binary value. Do not use with Strings.