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> class IteratorTemplate
65  {
66  public:
67  using iterator_category = std::forward_iterator_tag;
68  using value_type = T;
69  using difference_type = std::ptrdiff_t;
70  using pointer = T*;
71  using reference = T&;
72 
73  IteratorTemplate(TPtr x) : mObject(x)
74  {
75  }
76 
77  IteratorTemplate(TRef& x) : mObject(&x)
78  {
79  }
80 
81  IteratorTemplate(const IteratorTemplate& other) : mObject(other.mObject)
82  {
83  }
84 
86  {
87  this->mObject = static_cast<TPtr>(this->mObject->next());
88  return *this;
89  }
90 
92  {
93  Iterator tmp(*this);
94  operator++();
95  return tmp;
96  }
97 
98  bool operator==(const IteratorTemplate& rhs) const
99  {
100  return mObject == rhs.mObject;
101  }
102 
103  bool operator!=(const IteratorTemplate& rhs) const
104  {
105  return mObject != rhs.mObject;
106  }
107 
108  TRef operator*()
109  {
110  return *mObject;
111  }
112 
113  TPtr operator->()
114  {
115  return mObject;
116  }
117 
118  operator TPtr()
119  {
120  return mObject;
121  }
122 
123  private:
124  TPtr mObject;
125  };
126 
129 
131  {
132  return static_cast<ObjectType*>(this->next());
133  }
134 
135  bool insertAfter(ObjectType* object)
136  {
137  return LinkedObject::insertAfter(object);
138  }
139 
140  Iterator begin() const
141  {
142  return Iterator(this);
143  }
144 
145  Iterator end() const
146  {
147  return Iterator(nullptr);
148  }
149 
150  Iterator cbegin() const
151  {
152  return ConstIterator(this);
153  }
154 
155  Iterator cend() const
156  {
157  return ConstIterator(nullptr);
158  }
159 };
Singly-linked list of objects.
Definition: LinkedObjectList.h:19
Definition: LinkedObject.h:65
T & reference
Definition: LinkedObject.h:71
std::ptrdiff_t difference_type
Definition: LinkedObject.h:69
IteratorTemplate(TRef &x)
Definition: LinkedObject.h:77
T value_type
Definition: LinkedObject.h:68
IteratorTemplate operator++(int)
Definition: LinkedObject.h:91
std::forward_iterator_tag iterator_category
Definition: LinkedObject.h:67
IteratorTemplate(TPtr x)
Definition: LinkedObject.h:73
TRef operator*()
Definition: LinkedObject.h:108
T * pointer
Definition: LinkedObject.h:70
IteratorTemplate & operator++()
Definition: LinkedObject.h:85
TPtr operator->()
Definition: LinkedObject.h:113
bool operator==(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:98
bool operator!=(const IteratorTemplate &rhs) const
Definition: LinkedObject.h:103
IteratorTemplate(const IteratorTemplate &other)
Definition: LinkedObject.h:81
Base class template for linked items with type casting.
Definition: LinkedObject.h:62
ObjectType * getNext() const
Definition: LinkedObject.h:130
bool insertAfter(ObjectType *object)
Definition: LinkedObject.h:135
Iterator cbegin() const
Definition: LinkedObject.h:150
Iterator end() const
Definition: LinkedObject.h:145
IteratorTemplate< ObjectType, ObjectType *, ObjectType & > Iterator
Definition: LinkedObject.h:127
Iterator cend() const
Definition: LinkedObject.h:155
IteratorTemplate< const ObjectType, const ObjectType *, const ObjectType & > ConstIterator
Definition: LinkedObject.h:128
Iterator begin() const
Definition: LinkedObject.h:140
Base virtual class to allow objects to be linked together.
Definition: LinkedObject.h:22
virtual ~LinkedObject()
Definition: LinkedObject.h:24
virtual LinkedObject * next() const
Definition: LinkedObject.h:28
bool operator!=(const LinkedObject &other) const
Definition: LinkedObject.h:48
bool operator==(const LinkedObject &other) const
Definition: LinkedObject.h:43
bool insertAfter(LinkedObject *object)
Definition: LinkedObject.h:33
ObjectType
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:34