Embedded Template Library 1.0
Loading...
Searching...
No Matches
intrusive_queue.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2016 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_INTRUSIVE_QUEUE_INCLUDED
32#define ETL_INTRUSIVE_QUEUE_INCLUDED
33
34#include "platform.h"
35#include "type_traits.h"
36#include "error_handler.h"
37#include "intrusive_links.h"
38
39#include <stddef.h>
40
41namespace etl
42{
43 //***************************************************************************
46 //***************************************************************************
47 class intrusive_queue_exception : public etl::exception
48 {
49 public:
50
51 intrusive_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
52 : exception(reason_, file_name_, line_number_)
53 {
54 }
55 };
56
57 //***************************************************************************
60 //***************************************************************************
61 class intrusive_queue_empty : public intrusive_queue_exception
62 {
63 public:
64
65 intrusive_queue_empty(string_type file_name_, numeric_type line_number_)
66 : intrusive_queue_exception(ETL_ERROR_TEXT("intrusive_queue:empty", ETL_INTRUSIVE_QUEUE_FILE_ID"A"), file_name_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
74 //***************************************************************************
75 class intrusive_queue_value_is_already_linked : public intrusive_queue_exception
76 {
77 public:
78
79 intrusive_queue_value_is_already_linked(string_type file_name_, numeric_type line_number_)
80 : intrusive_queue_exception(ETL_ERROR_TEXT("intrusive_queue:value is already linked", ETL_INTRUSIVE_QUEUE_FILE_ID"B"), file_name_, line_number_)
81 {
82 }
83 };
84
85 //***************************************************************************
89 //***************************************************************************
90 template <typename TLink>
92 {
93 public:
94
95 // Node typedef.
96 typedef TLink link_type;
97
98 //*************************************************************************
101 //*************************************************************************
102 void push(link_type& value)
103 {
104 ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_queue_value_is_already_linked));
105
106 if (empty())
107 {
108 terminator.etl_next = &value;
109 }
110 else
111 {
112 p_back->etl_next = &value;
113 }
114
115 p_back = &value;
116 value.etl_next = &terminator;
117
118 ++current_size;
119 }
120
121 //*************************************************************************
124 //*************************************************************************
125 void pop()
126 {
127 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(!empty(), ETL_ERROR(intrusive_queue_empty));
128
129 link_type* p_front = terminator.etl_next;
130
131 link_type* p_next = p_front->etl_next;
132 terminator.etl_next = p_next;
133
134 p_front->clear();
135
136 if (empty())
137 {
139 }
140
141 --current_size;
142 }
143
144 //*************************************************************************
148 //*************************************************************************
149 template <typename TContainer>
150 void pop_into(TContainer& destination)
151 {
152 link_type* p_link = terminator.etl_next;
153 pop();
154 destination.push(*p_link);
155 }
156
157 //*************************************************************************
159 //*************************************************************************
160 void clear()
161 {
162 while (!empty())
163 {
164 pop();
165 }
166
167 current_size = 0;
168 }
169
170 //*************************************************************************
172 //*************************************************************************
173 bool empty() const
174 {
175 return current_size == 0;
176 }
177
178 //*************************************************************************
180 //*************************************************************************
181 size_t size() const
182 {
183 return current_size;
184 }
185
186 protected:
187
188 //*************************************************************************
190 //*************************************************************************
192 : p_back (&terminator)
193 , current_size(0)
194 {
195 terminator.etl_next = &terminator;
196 }
197
198 //*************************************************************************
200 //*************************************************************************
204
205 link_type* p_back;
206 link_type terminator;
207
209 };
210
211 //***************************************************************************
217 //***************************************************************************
218 template <typename TValue, typename TLink>
220 {
221 public:
222
223 // Node typedef.
224 typedef typename etl::intrusive_queue_base<TLink> link_type;
225
226 // STL style typedefs.
227 typedef TValue value_type;
228 typedef value_type* pointer;
229 typedef const value_type* const_pointer;
230 typedef value_type& reference;
231 typedef const value_type& const_reference;
232 typedef size_t size_type;
233
234 //*************************************************************************
236 //*************************************************************************
238 : intrusive_queue_base<TLink>()
239 {
240 }
241
242 //*************************************************************************
246 //*************************************************************************
247 reference front()
248 {
249 return *static_cast<TValue*>(this->terminator.etl_next);
250 }
251
252 //*************************************************************************
256 //*************************************************************************
257 reference back()
258 {
259 return *static_cast<TValue*>(this->p_back);
260 }
261
262 //*************************************************************************
266 //*************************************************************************
267 const_reference front() const
268 {
269 return *static_cast<const TValue*>(this->terminator.etl_next);
270 }
271
272 //*************************************************************************
276 //*************************************************************************
277 const_reference back() const
278 {
279 return *static_cast<const TValue*>(this->p_back);
280 }
281
282 private:
283
284 // Disable copy construction and assignment.
286 intrusive_queue& operator = (const intrusive_queue& rhs);
287 };
288}
289
290#endif
Definition intrusive_queue.h:62
Definition intrusive_queue.h:76
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition exception.h:47
void pop_into(TContainer &destination)
Definition intrusive_queue.h:150
const_reference front() const
Definition intrusive_queue.h:267
const_reference back() const
Definition intrusive_queue.h:277
reference back()
Definition intrusive_queue.h:257
size_t current_size
Counts the number of elements in the list.
Definition intrusive_queue.h:208
void pop()
Definition intrusive_queue.h:125
link_type terminator
This link terminates the queue and points to the front of the queue.
Definition intrusive_queue.h:206
bool empty() const
Checks if the queue is in the empty state.
Definition intrusive_queue.h:173
intrusive_queue()
Constructor.
Definition intrusive_queue.h:237
~intrusive_queue_base()
Destructor.
Definition intrusive_queue.h:201
void push(link_type &value)
Definition intrusive_queue.h:102
reference front()
Definition intrusive_queue.h:247
link_type * p_back
Pointer to the current back of the queue.
Definition intrusive_queue.h:205
void clear()
Clears the queue to the empty state.
Definition intrusive_queue.h:160
intrusive_queue_base()
Constructor.
Definition intrusive_queue.h:191
size_t size() const
Returns the number of elements.
Definition intrusive_queue.h:181
Definition intrusive_queue.h:220
Definition intrusive_queue.h:92
bitset_ext
Definition absolute.h:39