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  clear();
25  }
26 
27  bool allocate(size_t newSize);
28 
29  void clear()
30  {
31  free(values);
32  values = nullptr;
33  size = 0;
34  }
35 
36  bool insert(unsigned index, T value)
37  {
38  memmove(&values[index + 1], &values[index], size - index - 1);
39  values[index] = value;
40  return true;
41  }
42 
43  void remove(unsigned index)
44  {
45  memmove(&values[index], &values[index + 1], (size - index - 1) * sizeof(T));
46  }
47 
48  void trim(size_t newSize, bool reallocate)
49  {
50  if(!reallocate) {
51  return;
52  }
53 
54  auto newmem = realloc(values, sizeof(T) * newSize);
55  if(newmem == nullptr) {
56  return;
57  }
58 
59  values = static_cast<T*>(newmem);
60  size = newSize;
61  }
62 
63  T& operator[](unsigned index)
64  {
65  return values[index];
66  }
67 
68  const T& operator[](unsigned index) const
69  {
70  return const_cast<ScalarList&>(*this)[index];
71  }
72 };
73 
77 template <typename T> struct ObjectList : public ScalarList<T*> {
78  struct Element {
79  T*& value;
80 
82  {
83  delete value;
84  value = v;
85  return *this;
86  }
87 
88  template <typename U = T>
89  typename std::enable_if<std::is_copy_constructible<U>::value, Element&>::type operator=(const U& v)
90  {
91  delete value;
92  value = new U{v};
93  return *this;
94  }
95 
96  operator T&()
97  {
98  return *value;
99  }
100  };
101 
103  {
104  clear();
105  }
106 
107  bool allocate(size_t newSize);
108 
109  void clear()
110  {
111  while(this->size != 0) {
112  delete this->values[--this->size];
113  }
115  }
116 
117  bool insert(unsigned index, const T& value)
118  {
119  auto el = new T(value);
120  if(el == nullptr) {
121  return false;
122  }
123  return ScalarList<T*>::insert(index, el);
124  }
125 
126  void remove(unsigned index)
127  {
128  delete this->values[index];
129  ScalarList<T*>::remove(index);
130  this->values[this->size - 1] = nullptr;
131  }
132 
133  void trim(size_t newSize, bool reallocate)
134  {
135  for(unsigned i = this->size; i > newSize; --i) {
136  delete this->values[i - 1];
137  this->values[i - 1] = nullptr;
138  }
139 
140  ScalarList<T*>::trim(newSize, reallocate);
141  }
142 
143  Element operator[](unsigned index)
144  {
145  return Element{this->values[index]};
146  }
147 
148  const T& operator[](unsigned index) const
149  {
150  return *this->values[index];
151  }
152 };
153 
154 template <typename T> bool ScalarList<T>::allocate(size_t newSize)
155 {
156  if(newSize <= size) {
157  return true;
158  }
159 
160  auto newmem = realloc(values, sizeof(T) * newSize);
161  if(newmem == nullptr) {
162  return false;
163  }
164 
165  values = static_cast<T*>(newmem);
166  size = newSize;
167  return true;
168 }
169 
170 template <typename T> bool ObjectList<T>::allocate(size_t newSize)
171 {
172  auto curSize = this->size;
173  if(!ScalarList<T*>::allocate(newSize)) {
174  return false;
175  }
176 
177  std::fill_n(&this->values[curSize], newSize - curSize, nullptr);
178  return true;
179 }
180 
181 template <typename T>
182 using List = typename std::conditional<std::is_scalar<T>::value, ScalarList<T>, ObjectList<T>>::type;
183 
184 } // namespace wiring_private
Definition: WiringList.h:14
typename std::conditional< std::is_scalar< T >::value, ScalarList< T >, ObjectList< T > >::type List
Definition: WiringList.h:182
Definition: WiringList.h:78
std::enable_if< std::is_copy_constructible< U >::value, Element & >::type operator=(const U &v)
Definition: WiringList.h:89
Element & operator=(T *v)
Definition: WiringList.h:81
T *& value
Definition: WiringList.h:79
List of object pointers.
Definition: WiringList.h:77
Element operator[](unsigned index)
Definition: WiringList.h:143
void trim(size_t newSize, bool reallocate)
Definition: WiringList.h:133
void remove(unsigned index)
Definition: WiringList.h:126
~ObjectList()
Definition: WiringList.h:102
bool allocate(size_t newSize)
Definition: WiringList.h:170
const T & operator[](unsigned index) const
Definition: WiringList.h:148
bool insert(unsigned index, const T &value)
Definition: WiringList.h:117
void clear()
Definition: WiringList.h:109
List of scalar values.
Definition: WiringList.h:18
bool allocate(size_t newSize)
Definition: WiringList.h:154
const T & operator[](unsigned index) const
Definition: WiringList.h:68
void remove(unsigned index)
Definition: WiringList.h:43
void trim(size_t newSize, bool reallocate)
Definition: WiringList.h:48
bool insert(unsigned index, T value)
Definition: WiringList.h:36
~ScalarList()
Definition: WiringList.h:22
void clear()
Definition: WiringList.h:29
size_t size
Definition: WiringList.h:20
T * values
Definition: WiringList.h:19
T & operator[](unsigned index)
Definition: WiringList.h:63