WiringList.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * WiringList.h - Private class templates used by HashMap
8  *
9  ****/
10 
11 #pragma once
12 
13 namespace wiring_private
14 {
18 template <typename T> struct ScalarList {
19  T* values{nullptr};
20  size_t size{0};
21 
23  {
24  }
25 
26  ScalarList(const ScalarList&) = delete;
27  ScalarList(ScalarList&&) = default;
28  ScalarList& operator=(const ScalarList&) = delete;
30 
32  {
33  clear();
34  }
35 
36  bool allocate(size_t newSize);
37 
38  void clear()
39  {
40  free(values);
41  values = nullptr;
42  size = 0;
43  }
44 
45  bool insert(unsigned index, T value)
46  {
47  memmove(&values[index + 1], &values[index], size - index - 1);
48  values[index] = value;
49  return true;
50  }
51 
52  void remove(unsigned index)
53  {
54  memmove(&values[index], &values[index + 1], (size - index - 1) * sizeof(T));
55  }
56 
57  void trim(size_t newSize, bool reallocate)
58  {
59  if(!reallocate) {
60  return;
61  }
62 
63  auto newmem = realloc(values, sizeof(T) * newSize);
64  if(newmem == nullptr) {
65  return;
66  }
67 
68  values = static_cast<T*>(newmem);
69  size = newSize;
70  }
71 
72  T& operator[](unsigned index)
73  {
74  return values[index];
75  }
76 
77  const T& operator[](unsigned index) const
78  {
79  return values[index];
80  }
81 };
82 
86 template <typename T> struct ObjectList : public ScalarList<T*> {
87  struct Element {
88  T*& value;
89 
91  {
92  delete value;
93  value = v;
94  return *this;
95  }
96 
97  template <typename U = T>
98  typename std::enable_if<std::is_copy_constructible<U>::value, Element&>::type operator=(const U& v)
99  {
100  delete value;
101  value = new U{v};
102  return *this;
103  }
104 
105  operator T&()
106  {
107  return *value;
108  }
109  };
110 
111  ObjectList() = default;
112  ObjectList(const ObjectList&) = delete;
113  ObjectList(ObjectList&&) = default;
114  ObjectList& operator=(const ObjectList&) = delete;
116 
118  {
119  clear();
120  }
121 
122  bool allocate(size_t newSize);
123 
124  void clear()
125  {
126  while(this->size != 0) {
127  delete this->values[--this->size];
128  }
130  }
131 
132  bool insert(unsigned index, const T& value)
133  {
134  auto el = new T(value);
135  if(el == nullptr) {
136  return false;
137  }
138  return ScalarList<T*>::insert(index, el);
139  }
140 
141  void remove(unsigned index)
142  {
143  delete this->values[index];
144  ScalarList<T*>::remove(index);
145  this->values[this->size - 1] = nullptr;
146  }
147 
148  void trim(size_t newSize, bool reallocate)
149  {
150  for(unsigned i = this->size; i > newSize; --i) {
151  delete this->values[i - 1];
152  this->values[i - 1] = nullptr;
153  }
154 
155  ScalarList<T*>::trim(newSize, reallocate);
156  }
157 
158  Element operator[](unsigned index)
159  {
160  return Element{this->values[index]};
161  }
162 
163  const T& operator[](unsigned index) const
164  {
165  return *this->values[index];
166  }
167 };
168 
169 template <typename T> bool ScalarList<T>::allocate(size_t newSize)
170 {
171  if(newSize <= size) {
172  return true;
173  }
174 
175  auto newmem = realloc(values, sizeof(T) * newSize);
176  if(newmem == nullptr) {
177  return false;
178  }
179 
180  values = static_cast<T*>(newmem);
181  size = newSize;
182  return true;
183 }
184 
185 template <typename T> bool ObjectList<T>::allocate(size_t newSize)
186 {
187  auto curSize = this->size;
188  if(!ScalarList<T*>::allocate(newSize)) {
189  return false;
190  }
191 
192  std::fill_n(&this->values[curSize], newSize - curSize, nullptr);
193  return true;
194 }
195 
196 template <typename T>
197 using List = typename std::conditional<std::is_scalar<T>::value, ScalarList<T>, ObjectList<T>>::type;
198 
199 } // namespace wiring_private
Definition: WiringList.h:14
typename std::conditional< std::is_scalar< T >::value, ScalarList< T >, ObjectList< T > >::type List
Definition: WiringList.h:197
Definition: WiringList.h:87
std::enable_if< std::is_copy_constructible< U >::value, Element & >::type operator=(const U &v)
Definition: WiringList.h:98
Element & operator=(T *v)
Definition: WiringList.h:90
T *& value
Definition: WiringList.h:88
List of object pointers.
Definition: WiringList.h:86
Element operator[](unsigned index)
Definition: WiringList.h:158
ObjectList & operator=(ObjectList &&)=default
void trim(size_t newSize, bool reallocate)
Definition: WiringList.h:148
ObjectList(const ObjectList &)=delete
ObjectList & operator=(const ObjectList &)=delete
ObjectList(ObjectList &&)=default
void remove(unsigned index)
Definition: WiringList.h:141
~ObjectList()
Definition: WiringList.h:117
bool allocate(size_t newSize)
Definition: WiringList.h:185
const T & operator[](unsigned index) const
Definition: WiringList.h:163
bool insert(unsigned index, const T &value)
Definition: WiringList.h:132
void clear()
Definition: WiringList.h:124
List of scalar values.
Definition: WiringList.h:18
bool allocate(size_t newSize)
Definition: WiringList.h:169
ScalarList & operator=(const ScalarList &)=delete
const T & operator[](unsigned index) const
Definition: WiringList.h:77
void remove(unsigned index)
Definition: WiringList.h:52
ScalarList & operator=(ScalarList &&)=default
void trim(size_t newSize, bool reallocate)
Definition: WiringList.h:57
bool insert(unsigned index, T value)
Definition: WiringList.h:45
ScalarList(const ScalarList &)=delete
ScalarList(ScalarList &&)=default
~ScalarList()
Definition: WiringList.h:31
ScalarList()
Definition: WiringList.h:22
void clear()
Definition: WiringList.h:38
size_t size
Definition: WiringList.h:20
T * values
Definition: WiringList.h:19
T & operator[](unsigned index)
Definition: WiringList.h:72