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