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 
34  {
35  return mNext;
36  }
37 
38  bool operator==(const LinkedObject& other) const
39  {
40  return this == &other;
41  }
42 
43  bool operator!=(const LinkedObject& other) const
44  {
45  return this != &other;
46  }
47 
48 private:
49  friend class LinkedObjectList;
50  LinkedObject* mNext{nullptr};
51 };
52 
56 template <typename ObjectType> class LinkedObjectTemplate : public LinkedObject
57 {
58 public:
59  template <typename T, typename TPtr, typename TRef>
60  class IteratorTemplate : public std::iterator<std::forward_iterator_tag, T>
61  {
62  public:
63  IteratorTemplate(TPtr x) : mObject(x)
64  {
65  }
66 
67  IteratorTemplate(TRef& x) : mObject(&x)
68  {
69  }
70 
71  IteratorTemplate(const IteratorTemplate& other) : mObject(other.mObject)
72  {
73  }
74 
76  {
77  mObject = mObject->getNext();
78  return *this;
79  }
80 
82  {
83  Iterator tmp(*this);
84  operator++();
85  return tmp;
86  }
87 
88  bool operator==(const IteratorTemplate& rhs) const
89  {
90  return mObject == rhs.mObject;
91  }
92 
93  bool operator!=(const IteratorTemplate& rhs) const
94  {
95  return mObject != rhs.mObject;
96  }
97 
98  TRef operator*()
99  {
100  return *mObject;
101  }
102 
103  TPtr operator->()
104  {
105  return mObject;
106  }
107 
108  operator TPtr()
109  {
110  return mObject;
111  }
112 
113  private:
114  TPtr mObject;
115  };
116 
117  using Iterator = IteratorTemplate<ObjectType, ObjectType*, ObjectType&>;
118  using ConstIterator = IteratorTemplate<const ObjectType, const ObjectType*, const ObjectType&>;
119 
120  ObjectType* getNext() const
121  {
122  return reinterpret_cast<ObjectType*>(this->next());
123  }
124 
125  Iterator begin() const
126  {
127  return Iterator(this);
128  }
129 
130  Iterator end() const
131  {
132  return Iterator(nullptr);
133  }
134 
135  Iterator cbegin() const
136  {
137  return ConstIterator(this);
138  }
139 
140  Iterator cend() const
141  {
142  return ConstIterator(nullptr);
143  }
144 };
bool operator!=(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:93
Definition: LinkedObject.h:60
bool operator==(const LinkedObject &other) const
Definition: LinkedObject.h:38
TRef operator*()
Definition: LinkedObject.h:98
LinkedObject * getNext() const
Definition: LinkedObject.h:33
bool operator==(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:88
IteratorTemplate(const IteratorTemplate &other)
Definition: LinkedObject.h:71
Iterator begin() const
Definition: LinkedObject.h:125
TPtr operator->()
Definition: LinkedObject.h:103
virtual ~LinkedObject()
Definition: LinkedObject.h:24
IteratorTemplate operator++(int)
Definition: LinkedObject.h:81
IteratorTemplate(TPtr x)
Definition: LinkedObject.h:63
bool operator!=(const LinkedObject &other) const
Definition: LinkedObject.h:43
Iterator cend() const
Definition: LinkedObject.h:140
Base class template for linked items with type casting.
Definition: LinkedObject.h:56
Base virtual class to allow objects to be linked together.
Definition: LinkedObject.h:21
Iterator cbegin() const
Definition: LinkedObject.h:135
Singly-linked list of objects.
Definition: LinkedObjectList.h:18
IteratorTemplate(TRef &x)
Definition: LinkedObject.h:67
IteratorTemplate< Device, Device *, Device &> Iterator
Definition: LinkedObject.h:117
virtual LinkedObject * next() const
Definition: LinkedObject.h:28
Iterator end() const
Definition: LinkedObject.h:130
IteratorTemplate & operator++()
Definition: LinkedObject.h:75
IteratorTemplate< const Device, const Device *, const Device &> ConstIterator
Definition: LinkedObject.h:118
SerializationFormat operator++(SerializationFormat &fmt)
Definition: ArduinoJson.h:120
ObjectType * getNext() const
Definition: LinkedObject.h:120