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