LinkedObjectList.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  * LinkedObjectList.h
8  *
9  ****/
10 #pragma once
11 
12 #include "LinkedObject.h"
13 
19 {
20 public:
21  LinkedObjectList() = default;
22 
23  LinkedObjectList(LinkedObject* object) : mHead(object)
24  {
25  }
26 
27  bool add(LinkedObject* object);
28 
29  bool add(const LinkedObject* object)
30  {
31  return add(const_cast<LinkedObject*>(object));
32  }
33 
34  bool insert(LinkedObject* object)
35  {
36  if(object == nullptr) {
37  return false;
38  }
39 
40  object->mNext = mHead;
41  mHead = object;
42  return true;
43  }
44 
45  bool insert(const LinkedObject* object)
46  {
47  return insert(const_cast<LinkedObject*>(object));
48  }
49 
50  bool remove(LinkedObject* object);
51 
53  {
54  if(mHead == nullptr) {
55  return nullptr;
56  }
57  auto obj = mHead;
58  mHead = mHead->mNext;
59  obj->mNext = nullptr;
60  return obj;
61  }
62 
63  void clear()
64  {
65  mHead = nullptr;
66  }
67 
69  {
70  return mHead;
71  }
72 
73  const LinkedObject* head() const
74  {
75  return mHead;
76  }
77 
78  bool isEmpty() const
79  {
80  return mHead == nullptr;
81  }
82 
83 protected:
84  LinkedObject* mHead{nullptr};
85 };
86 
87 template <typename ObjectType> class LinkedObjectListTemplate : public LinkedObjectList
88 {
89 public:
90  using Iterator =
91  typename LinkedObjectTemplate<ObjectType>::template IteratorTemplate<ObjectType, ObjectType*, ObjectType&>;
92  using ConstIterator =
93  typename LinkedObjectTemplate<ObjectType>::template IteratorTemplate<const ObjectType, const ObjectType*,
94  const ObjectType&>;
95 
97 
99  {
100  }
101 
103  {
104  return static_cast<ObjectType*>(mHead);
105  }
106 
107  const ObjectType* head() const
108  {
109  return static_cast<const ObjectType*>(mHead);
110  }
111 
113  {
114  return head();
115  }
116 
118  {
119  return nullptr;
120  }
121 
123  {
124  return head();
125  }
126 
128  {
129  return nullptr;
130  }
131 
132  bool add(ObjectType* object)
133  {
134  return LinkedObjectList::add(object);
135  }
136 
137  bool add(const ObjectType* object)
138  {
139  return LinkedObjectList::add(object);
140  }
141 
142  bool insert(ObjectType* object)
143  {
144  return LinkedObjectList::insert(object);
145  }
146 
147  bool insert(const ObjectType* object)
148  {
149  return LinkedObjectList::insert(object);
150  }
151 
153  {
154  return static_cast<ObjectType*>(LinkedObjectList::pop());
155  }
156 
157  size_t count() const
158  {
159  return std::count_if(begin(), end(), [](const ObjectType&) { return true; });
160  }
161 
162  bool contains(const ObjectType& object) const
163  {
164  return std::find(begin(), end(), object);
165  }
166 };
167 
172 template <typename ObjectType> class OwnedLinkedObjectListTemplate : public LinkedObjectListTemplate<ObjectType>
173 {
174 public:
176 
179 
181  {
182  clear();
183  }
184 
185  bool remove(ObjectType* object)
186  {
187  bool res = LinkedObjectList::remove(object);
188  delete object;
189  return res;
190  }
191 
192  void clear()
193  {
194  while(remove(this->head())) {
195  //
196  }
197  }
198 };
Definition: LinkedObjectList.h:88
bool add(const ObjectType *object)
Definition: LinkedObjectList.h:137
bool insert(const ObjectType *object)
Definition: LinkedObjectList.h:147
ObjectType * pop()
Definition: LinkedObjectList.h:152
ObjectType * head()
Definition: LinkedObjectList.h:102
LinkedObjectListTemplate()=default
ConstIterator begin() const
Definition: LinkedObjectList.h:122
LinkedObjectListTemplate(ObjectType *object)
Definition: LinkedObjectList.h:98
Iterator begin()
Definition: LinkedObjectList.h:112
bool insert(ObjectType *object)
Definition: LinkedObjectList.h:142
bool add(ObjectType *object)
Definition: LinkedObjectList.h:132
typename LinkedObjectTemplate< ObjectType >::template IteratorTemplate< const ObjectType, const ObjectType *, const ObjectType & > ConstIterator
Definition: LinkedObjectList.h:94
bool contains(const ObjectType &object) const
Definition: LinkedObjectList.h:162
size_t count() const
Definition: LinkedObjectList.h:157
typename LinkedObjectTemplate< ObjectType >::template IteratorTemplate< ObjectType, ObjectType *, ObjectType & > Iterator
Definition: LinkedObjectList.h:91
const ObjectType * head() const
Definition: LinkedObjectList.h:107
Iterator end()
Definition: LinkedObjectList.h:117
ConstIterator end() const
Definition: LinkedObjectList.h:127
Singly-linked list of objects.
Definition: LinkedObjectList.h:19
LinkedObjectList()=default
LinkedObject * mHead
Definition: LinkedObjectList.h:84
const LinkedObject * head() const
Definition: LinkedObjectList.h:73
LinkedObjectList(LinkedObject *object)
Definition: LinkedObjectList.h:23
bool insert(LinkedObject *object)
Definition: LinkedObjectList.h:34
void clear()
Definition: LinkedObjectList.h:63
LinkedObject * head()
Definition: LinkedObjectList.h:68
bool add(LinkedObject *object)
LinkedObject * pop()
Definition: LinkedObjectList.h:52
bool insert(const LinkedObject *object)
Definition: LinkedObjectList.h:45
bool add(const LinkedObject *object)
Definition: LinkedObjectList.h:29
bool isEmpty() const
Definition: LinkedObjectList.h:78
bool remove(LinkedObject *object)
Base class template for linked items with type casting.
Definition: LinkedObject.h:60
Base virtual class to allow objects to be linked together.
Definition: LinkedObject.h:22
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:173
OwnedLinkedObjectListTemplate(const OwnedLinkedObjectListTemplate &other)=delete
bool remove(ObjectType *object)
Definition: LinkedObjectList.h:185
OwnedLinkedObjectListTemplate & operator=(const OwnedLinkedObjectListTemplate &other)=delete
~OwnedLinkedObjectListTemplate()
Definition: LinkedObjectList.h:180
void clear()
Definition: LinkedObjectList.h:192
ObjectType
Definition: ObjectInfo.h:32