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 remove(LinkedObject* object);
37 
38  void clear()
39  {
40  mHead = nullptr;
41  }
42 
44  {
45  return mHead;
46  }
47 
48  const LinkedObject* head() const
49  {
50  return mHead;
51  }
52 
53  bool isEmpty() const
54  {
55  return mHead == nullptr;
56  }
57 
58 protected:
59  LinkedObject* mHead{nullptr};
60 };
61 
62 template <typename ObjectType> class LinkedObjectListTemplate : public LinkedObjectList
63 {
64 public:
65  LinkedObjectListTemplate() = default;
66 
67  LinkedObjectListTemplate(ObjectType* object) : LinkedObjectList(object)
68  {
69  }
70 
71  ObjectType* head()
72  {
73  return reinterpret_cast<ObjectType*>(mHead);
74  }
75 
76  const ObjectType* head() const
77  {
78  return reinterpret_cast<const ObjectType*>(mHead);
79  }
80 
81  typename ObjectType::Iterator begin()
82  {
83  return head();
84  }
85 
86  typename ObjectType::Iterator end()
87  {
88  return nullptr;
89  }
90 
91  typename ObjectType::ConstIterator begin() const
92  {
93  return head();
94  }
95 
96  typename ObjectType::ConstIterator end() const
97  {
98  return nullptr;
99  }
100 
101  size_t count() const
102  {
103  size_t n{0};
104  for(auto p = mHead; p != nullptr; ++n, p = p->next()) {
105  }
106  return n;
107  }
108 
109  bool contains(const ObjectType& object) const
110  {
111  return std::find(begin(), end(), object);
112  }
113 };
114 
119 template <typename ObjectType> class OwnedLinkedObjectListTemplate : public LinkedObjectListTemplate<ObjectType>
120 {
121 public:
123  {
124  clear();
125  }
126 
127  bool remove(ObjectType* object)
128  {
129  bool res = LinkedObjectList::remove(object);
130  delete object;
131  return res;
132  }
133 
134  void clear()
135  {
136  while(remove(this->head())) {
137  //
138  }
139  }
140 };
const LinkedObject * head() const
Definition: LinkedObjectList.h:48
ObjectType::Iterator end()
Definition: LinkedObjectList.h:86
Definition: LinkedObjectList.h:62
bool add(LinkedObject *object)
bool contains(const ObjectType &object) const
Definition: LinkedObjectList.h:109
LinkedObjectListTemplate(ObjectType *object)
Definition: LinkedObjectList.h:67
LinkedObject * head()
Definition: LinkedObjectList.h:43
LinkedObjectList()
Definition: LinkedObjectList.h:21
LinkedObjectList(LinkedObject *object)
Definition: LinkedObjectList.h:25
bool isEmpty() const
Definition: LinkedObjectList.h:53
bool remove(LinkedObject *object)
void clear()
Definition: LinkedObjectList.h:38
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:119
Base virtual class to allow objects to be linked together.
Definition: LinkedObject.h:21
ObjectType::ConstIterator begin() const
Definition: LinkedObjectList.h:91
Singly-linked list of objects.
Definition: LinkedObjectList.h:18
const ObjectType * head() const
Definition: LinkedObjectList.h:76
ObjectType::ConstIterator end() const
Definition: LinkedObjectList.h:96
ObjectType::Iterator begin()
Definition: LinkedObjectList.h:81
virtual LinkedObject * next() const
Definition: LinkedObject.h:28
void clear()
Definition: LinkedObjectList.h:134
LinkedObject * mHead
Definition: LinkedObjectList.h:59
bool add(const LinkedObject *object)
Definition: LinkedObjectList.h:31
ObjectType * head()
Definition: LinkedObjectList.h:71
~OwnedLinkedObjectListTemplate()
Definition: LinkedObjectList.h:122
size_t count() const
Definition: LinkedObjectList.h:101