Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SimpleQueue.hxx
Go to the documentation of this file.
1
34#ifndef _UTILS_SIMPLEQUEUE_HXX_
35#define _UTILS_SIMPLEQUEUE_HXX_
36
37#include "utils/QMember.hxx"
38
50public:
51 SimpleQueue() : head_(nullptr) {}
52
54 bool empty() {
55 return !head_;
56 }
57
59 void push_front(QMember* member) {
60 HASSERT(!member->next);
61 member->next = head_;
62 head_ = member;
63 }
64
69 QMember* f = head_;
70 head_ = f->next;
71 f->next = nullptr;
72 return f;
73 }
74
76 QMember* front() const {
77 return head_;
78 }
79
81 class end_iterator {};
82
84 class iterator {
85 public:
87 iterator(QMember** link): link_(link) {
89 }
90
91 bool operator==(const iterator& o) const {
92 return *link_ == *o.link_;
93 }
94
95 bool operator==(const end_iterator& o) const {
96 return *link_ == nullptr;
97 }
98
99 bool operator!=(const iterator& o) const {
100 return *link_ != *o.link_;
101 }
102
103 bool operator!=(const end_iterator& o) const {
104 return *link_ != nullptr;
105 }
106
107 iterator& operator++() {
108 HASSERT(*link_);
109 link_ = &(*link_)->next;
110 return *this;
111 }
112
113 QMember* operator->() {
114 return *link_;
115 }
116
117 QMember& operator*() {
118 return **link_;
119 }
120
121 private:
122 friend class SimpleQueue;
123
127 };
128
130 template<class T> class typed_iterator : public iterator {
131 public:
135 T* operator->() {
136 return static_cast<T*>(*link_);
137 }
138 T& operator*() {
139 return static_cast<T&>(**link_);
140 }
141 };
142
145 return iterator(&head_);
146 }
147
152 return end_iterator{};
153 }
154
160 void insert(const iterator& position, QMember* entry) {
161 HASSERT(!entry->next);
162 entry->next = *position.link_;
163 *position.link_ = entry;
164 }
165
171 void erase(const iterator& position) {
172 QMember* m = *position.link_;
173 HASSERT(m);
174 *position.link_ = m->next;
175 m->next = nullptr;
176 }
177
178protected:
180 static QMember* const PTR_END;
181
184};
185
186
191template<class T>
192class TypedQueue : public SimpleQueue {
193public:
195 void push_front(T* entry) {
197 }
198
202 return static_cast<T*>(SimpleQueue::pop_front());
203 }
204
206 T* front() const {
207 return static_cast<T*>(SimpleQueue::front());
208 }
209
212
215 return iterator(&head_);
216 }
217};
218
219#endif // _UTILS_SIMPLEQUEUE_HXX_
Essentially a "next" pointer container.
Definition QMember.hxx:42
QMember * next
pointer to the next member in the queue
Definition QMember.hxx:65
Static iterator class pointing to the "end" of the queue.
STL-compatible iterator for SimpleQueue.
iterator(QMember **link)
Constructor.
QMember ** link_
**link == *this for the iterator semantics.
STL-compatible iterator for TypedQueue.
typed_iterator(QMember **link)
Typed itartor consturctor.
A simple fast single-linked stack class with non-virtual methods.
QMember * front() const
peeks.
QMember * pop_front()
Removes the entry at the front of the queue.
iterator begin()
end_iterator end()
static QMember *const PTR_END
Used as a guard for comparing against for the end of the queue.
void push_front(QMember *member)
Adds an entry to the front of the queue.
QMember * head_
Top pointer for the stack.
void erase(const iterator &position)
Removes the entry pointed to by the iterator.
void insert(const iterator &position, QMember *entry)
Inserts the element entry before the position.
A simple, fast, type-safe single-linked queue class with non-virtual methods.
T * pop_front()
Removes the entry at the front of the queue.
iterator begin()
T * front() const
peeks.
typed_iterator< T > iterator
Typed iterator type.
void push_front(T *entry)
Inserts an entry to the front of the queue.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138