Embedded Template Library  1.0
intrusive_queue.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2016 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_INTRUSIVE_QUEUE_INCLUDED
32 #define ETL_INTRUSIVE_QUEUE_INCLUDED
33 
34 #include <stddef.h>
35 
36 #include "platform.h"
37 #include "type_traits.h"
38 #include "error_handler.h"
39 #include "intrusive_links.h"
40 
41 #undef ETL_FILE
42 #define ETL_FILE "29"
43 
44 namespace etl
45 {
46  //***************************************************************************
49  //***************************************************************************
51  {
52  public:
53 
54  intrusive_queue_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
55  : exception(reason_, file_name_, line_number_)
56  {
57  }
58  };
59 
60  //***************************************************************************
63  //***************************************************************************
65  {
66  public:
67 
68  intrusive_queue_empty(string_type file_name_, numeric_type line_number_)
69  : intrusive_queue_exception(ETL_ERROR_TEXT("intrusive_queue:empty", ETL_FILE"A"), file_name_, line_number_)
70  {
71  }
72  };
73 
74  //***************************************************************************
78  //***************************************************************************
79  template <typename TLink>
81  {
82  public:
83 
84  // Node typedef.
85  typedef TLink link_type;
86 
87  //*************************************************************************
90  //*************************************************************************
91  void push(link_type& value)
92  {
93  value.clear();
94 
95  if (p_back != ETL_NULLPTR)
96  {
97  etl::link(p_back, value);
98  }
99  else
100  {
101  p_front = &value;
102  }
103 
104  p_back = &value;
105 
106  ++current_size;
107  }
108 
109  //*************************************************************************
112  //*************************************************************************
113  void pop()
114  {
115 #if defined(ETL_CHECK_PUSH_POP)
116  ETL_ASSERT(!empty(), ETL_ERROR(intrusive_queue_empty));
117 #endif
118  link_type* p_next = p_front->etl_next;
119 
120  p_front = p_next;
121 
122  // Now empty?
123  if (p_front == ETL_NULLPTR)
124  {
125  p_back = ETL_NULLPTR;
126  }
127 
128  --current_size;
129  }
130 
131  //*************************************************************************
135  //*************************************************************************
136  template <typename TContainer>
137  void pop_into(TContainer& destination)
138  {
139  link_type* p_link = p_front;
140  pop();
141  destination.push(*p_link);
142  }
143 
144  //*************************************************************************
146  //*************************************************************************
147  void clear()
148  {
149  while (!empty())
150  {
151  pop();
152  }
153 
154  current_size = 0;
155  }
156 
157  //*************************************************************************
159  //*************************************************************************
160  bool empty() const
161  {
162  return current_size == 0;
163  }
164 
165  //*************************************************************************
167  //*************************************************************************
168  size_t size() const
169  {
170  return current_size;
171  }
172 
173  protected:
174 
175  //*************************************************************************
177  //*************************************************************************
179  : p_front(ETL_NULLPTR),
180  p_back(ETL_NULLPTR),
181  current_size(0)
182  {
183  }
184 
185  //*************************************************************************
187  //*************************************************************************
189  {
190  }
191 
192  link_type* p_front;
193  link_type* p_back;
194 
195  size_t current_size;
196  };
197 
198  //***************************************************************************
204  //***************************************************************************
205  template <typename TValue, typename TLink>
207  {
208  public:
209 
210  // Node typedef.
211  typedef typename etl::intrusive_queue_base<TLink> link_type;
212 
213  // STL style typedefs.
214  typedef TValue value_type;
215  typedef value_type* pointer;
216  typedef const value_type* const_pointer;
217  typedef value_type& reference;
218  typedef const value_type& const_reference;
219  typedef size_t size_type;
220 
221  //*************************************************************************
223  //*************************************************************************
225  : intrusive_queue_base<TLink>()
226  {
227  }
228 
229  //*************************************************************************
233  //*************************************************************************
234  reference front()
235  {
236  return *static_cast<TValue*>(this->p_front);
237  }
238 
239  //*************************************************************************
243  //*************************************************************************
244  reference back()
245  {
246  return *static_cast<TValue*>(this->p_back);
247  }
248 
249  //*************************************************************************
253  //*************************************************************************
254  const_reference front() const
255  {
256  return *static_cast<const TValue*>(this->p_front);
257  }
258 
259  //*************************************************************************
263  //*************************************************************************
264  const_reference back() const
265  {
266  return *static_cast<const TValue*>(this->p_back);
267  }
268 
269  private:
270 
271  // Disable copy construction and assignment.
273  intrusive_queue& operator = (const intrusive_queue& rhs);
274  };
275 }
276 
277 #undef ETL_FILE
278 
279 #endif
Definition: intrusive_queue.h:65
Definition: intrusive_queue.h:51
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
void pop_into(TContainer &destination)
Definition: intrusive_queue.h:137
const_reference front() const
Definition: intrusive_queue.h:254
const_reference back() const
Definition: intrusive_queue.h:264
reference back()
Definition: intrusive_queue.h:244
size_t current_size
Counts the number of elements in the list.
Definition: intrusive_queue.h:195
link_type * p_front
The current front of the queue.
Definition: intrusive_queue.h:192
void pop()
Definition: intrusive_queue.h:113
bool empty() const
Checks if the queue is in the empty state.
Definition: intrusive_queue.h:160
intrusive_queue()
Constructor.
Definition: intrusive_queue.h:224
~intrusive_queue_base()
Destructor.
Definition: intrusive_queue.h:188
void push(link_type &value)
Definition: intrusive_queue.h:91
reference front()
Definition: intrusive_queue.h:234
link_type * p_back
The current back of the queue.
Definition: intrusive_queue.h:193
void clear()
Clears the queue to the empty state.
Definition: intrusive_queue.h:147
intrusive_queue_base()
Constructor.
Definition: intrusive_queue.h:178
size_t size() const
Returns the number of elements.
Definition: intrusive_queue.h:168
Definition: intrusive_queue.h:207
Definition: intrusive_queue.h:81
Definition: absolute.h:37