ArrayBase.h
Go to the documentation of this file.
1 /****
2  * ConfigDB/ArrayBase.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 "Object.h"
23 #include "Pool.h"
24 
25 namespace ConfigDB
26 {
30 class ArrayBase : public Object
31 {
32 public:
33  using Object::Object;
34 
35  unsigned getItemCount() const
36  {
37  return getId() ? getArray().getCount() : 0;
38  }
39 
40  /*
41  * @brief Remove an item from the array.
42  * @note Be very careful dealing with `ObjectArray` item removal as all
43  * items after this one will be shifted and their references no longer valid.
44  */
45  bool removeItem(unsigned index)
46  {
47  return getArray().remove(index);
48  }
49 
50  void clear();
51 
52  void dispose();
53 
54 protected:
55  friend class Object;
56 
58  {
59  return static_cast<PropertyData*>(getDataPtr())->array;
60  }
61 
62  ArrayId getId() const
63  {
64  return static_cast<const PropertyData*>(getDataPtr())->array;
65  }
66 
67  void* getItem(unsigned index)
68  {
69  return getArray()[index];
70  }
71 
72  const void* getItem(unsigned index) const
73  {
74  return getArray()[index];
75  }
76 
78  const ArrayData& getArray() const;
79 };
80 
81 } // namespace ConfigDB
Base class to provide array of properties.
Definition: ArrayBase.h:31
unsigned getItemCount() const
Definition: ArrayBase.h:35
bool removeItem(unsigned index)
Definition: ArrayBase.h:45
ArrayId getId() const
Definition: ArrayBase.h:62
ArrayId & getId()
Definition: ArrayBase.h:57
void * getItem(unsigned index)
Definition: ArrayBase.h:67
const ArrayData & getArray() const
const void * getItem(unsigned index) const
Definition: ArrayBase.h:72
ArrayData & getArray()
An array of fixed-sized items.
Definition: Pool.h:225
bool remove(unsigned index)
An object can contain other objects, properties and arrays.
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:37
Object()
Definition: Libraries/ConfigDB/src/include/ConfigDB/Object.h:39
void * getDataPtr()
size_t getCount() const
Definition: Pool.h:46
Definition: Array.h:26
Identifies array storage within array pool.
Definition: PropertyData.h:31
Definition: PropertyData.h:44