LinkedObject.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  * LinkedObject.h
8  *
9  ****/
10 #pragma once
11 
12 #include <iterator>
13 #include <algorithm>
14 
22 {
23 public:
24  virtual ~LinkedObject()
25  {
26  }
27 
28  virtual LinkedObject* next() const
29  {
30  return mNext;
31  }
32 
33  bool insertAfter(LinkedObject* object)
34  {
35  if(object == nullptr) {
36  return false;
37  }
38  mNext = object->mNext;
39  object->mNext = this;
40  return true;
41  }
42 
43  bool operator==(const LinkedObject& other) const
44  {
45  return this == &other;
46  }
47 
48  bool operator!=(const LinkedObject& other) const
49  {
50  return this != &other;
51  }
52 
53 private:
54  friend class LinkedObjectList;
55  LinkedObject* mNext{nullptr};
56 };
57 
61 template <typename ObjectType> class LinkedObjectTemplate : public LinkedObject
62 {
63 public:
64  template <typename T, typename TPtr, typename TRef>
65  class IteratorTemplate : public std::iterator<std::forward_iterator_tag, T>
66  {
67  public:
68  IteratorTemplate(TPtr x) : mObject(x)
69  {
70  }
71 
72  IteratorTemplate(TRef& x) : mObject(&x)
73  {
74  }
75 
76  IteratorTemplate(const IteratorTemplate& other) : mObject(other.mObject)
77  {
78  }
79 
81  {
82  this->mObject = static_cast<TPtr>(this->mObject->next());
83  return *this;
84  }
85 
87  {
88  Iterator tmp(*this);
89  operator++();
90  return tmp;
91  }
92 
93  bool operator==(const IteratorTemplate& rhs) const
94  {
95  return mObject == rhs.mObject;
96  }
97 
98  bool operator!=(const IteratorTemplate& rhs) const
99  {
100  return mObject != rhs.mObject;
101  }
102 
103  TRef operator*()
104  {
105  return *mObject;
106  }
107 
108  TPtr operator->()
109  {
110  return mObject;
111  }
112 
113  operator TPtr()
114  {
115  return mObject;
116  }
117 
118  private:
119  TPtr mObject;
120  };
121 
122  using Iterator = IteratorTemplate<ObjectType, ObjectType*, ObjectType&>;
123  using ConstIterator = IteratorTemplate<const ObjectType, const ObjectType*, const ObjectType&>;
124 
126  {
127  return static_cast<ObjectType*>(this->next());
128  }
129 
130  bool insertAfter(ObjectType* object)
131  {
132  return LinkedObject::insertAfter(object);
133  }
134 
135  Iterator begin() const
136  {
137  return Iterator(this);
138  }
139 
140  Iterator end() const
141  {
142  return Iterator(nullptr);
143  }
144 
145  Iterator cbegin() const
146  {
147  return ConstIterator(this);
148  }
149 
150  Iterator cend() const
151  {
152  return ConstIterator(nullptr);
153  }
154 };
bool operator!=(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:98
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34
Definition: LinkedObject.h:65
bool operator==(const LinkedObject &other) const
Definition: LinkedObject.h:43
TRef operator*()
Definition: LinkedObject.h:103
bool operator==(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:93
IteratorTemplate(const IteratorTemplate &other)
Definition: LinkedObject.h:76
Iterator begin() const
Definition: LinkedObject.h:135
bool insertAfter(ObjectType *object)
Definition: LinkedObject.h:130
TPtr operator->()
Definition: LinkedObject.h:108
virtual ~LinkedObject()
Definition: LinkedObject.h:24
IteratorTemplate operator++(int)
Definition: LinkedObject.h:86
IteratorTemplate(TPtr x)
Definition: LinkedObject.h:68
bool operator!=(const LinkedObject &other) const
Definition: LinkedObject.h:48
Iterator cend() const
Definition: LinkedObject.h:150
bool insertAfter(LinkedObject *object)
Definition: LinkedObject.h:33
Base class template for linked items with type casting.
Definition: LinkedObject.h:61
Base virtual class to allow objects to be linked together.
Definition: LinkedObject.h:21
Iterator cbegin() const
Definition: LinkedObject.h:145
Singly-linked list of objects.
Definition: LinkedObjectList.h:18
IteratorTemplate(TRef &x)
Definition: LinkedObject.h:72
IteratorTemplate< Info, Info *, Info &> Iterator
Definition: LinkedObject.h:122
virtual LinkedObject * next() const
Definition: LinkedObject.h:28
Iterator end() const
Definition: LinkedObject.h:140
IteratorTemplate & operator++()
Definition: LinkedObject.h:80
IteratorTemplate< const Info, const Info *, const Info &> ConstIterator
Definition: LinkedObject.h:123
SerializationFormat operator++(SerializationFormat &fmt)
Definition: ArduinoJson.h:120
ObjectType * getNext() const
Definition: LinkedObject.h:125