Embedded Template Library 1.0
Loading...
Searching...
No Matches
intrusive_stack.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_STACK_INCLUDED
32#define ETL_INTRUSIVE_STACK_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 //***************************************************************************
56
57 //***************************************************************************
60 //***************************************************************************
62 {
63 public:
64
66 : intrusive_stack_exception(ETL_ERROR_TEXT("intrusive_stack:empty", ETL_INTRUSIVE_STACK_FILE_ID"A"), file_name_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
74 //***************************************************************************
76 {
77 public:
78
80 : intrusive_stack_exception(ETL_ERROR_TEXT("intrusive_stack:value is already linked", ETL_INTRUSIVE_STACK_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_stack_value_is_already_linked));
105
106 value.etl_next = p_top;
107 p_top = &value;
108
109 ++current_size;
110 }
111
112 //*************************************************************************
115 //*************************************************************************
116 void pop()
117 {
118#if defined(ETL_CHECK_PUSH_POP)
119 ETL_ASSERT_OR_RETURN(!empty(), ETL_ERROR(intrusive_stack_empty));
120#endif
121 link_type* p_next = p_top->etl_next;
122 p_top->clear();
123 p_top = p_next;
124 --current_size;
125 }
126
127 //*************************************************************************
131 //*************************************************************************
132 template <typename TContainer>
134 {
135 link_type* p_link = p_top;
136 pop();
137 destination.push(*p_link);
138 }
139
140 //*************************************************************************
142 //*************************************************************************
143 void reverse()
144 {
145 link_type* previous = &terminator;
146 link_type* current = p_top;
147 link_type* next;
148
149 while (current != &terminator)
150 {
151 next = current->etl_next;
152 current->etl_next = previous;
153 previous = current;
154 current = next;
155 }
156
157 p_top = previous;
158 }
159
160 //*************************************************************************
162 //*************************************************************************
163 void clear()
164 {
165 while (!empty())
166 {
167 pop();
168 }
169
170 current_size = 0;
171 }
172
173 //*************************************************************************
175 //*************************************************************************
176 bool empty() const
177 {
178 return current_size == 0;
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 size_t size() const
185 {
186 return current_size;
187 }
188
189 protected:
190
191 //*************************************************************************
193 //*************************************************************************
195 : p_top(&terminator)
196 , current_size(0)
197 {
198 }
199
200 //*************************************************************************
202 //*************************************************************************
206
207 link_type* p_top;
208 link_type terminator;
209
211 };
212
213 //***************************************************************************
219 //***************************************************************************
220 template <typename TValue, typename TLink>
222 {
223 public:
224
225 // Node typedef.
227
228 // STL style typedefs.
229 typedef TValue value_type;
230 typedef value_type* pointer;
231 typedef const value_type* const_pointer;
232 typedef value_type& reference;
233 typedef const value_type& const_reference;
234 typedef size_t size_type;
235
236 //*************************************************************************
238 //*************************************************************************
243
244 //*************************************************************************
248 //*************************************************************************
250 {
251 return *static_cast<TValue*>(this->p_top);
252 }
253
254 //*************************************************************************
257 //*************************************************************************
258 const_reference top() const
259 {
260 return *static_cast<const TValue*>(this->p_top);
261 }
262
263 private:
264
265 // Disable copy construction and assignment.
268 };
269}
270
271#endif
Definition intrusive_stack.h:62
Definition intrusive_stack.h:48
Definition intrusive_stack.h:76
Definition exception.h:47
void pop()
Definition intrusive_stack.h:116
size_t size() const
Returns the number of elements.
Definition intrusive_stack.h:184
reference top()
Definition intrusive_stack.h:249
void reverse()
Reverses the stack order.
Definition intrusive_stack.h:143
void clear()
Clears the stack to the empty state.
Definition intrusive_stack.h:163
const_reference top() const
Definition intrusive_stack.h:258
~intrusive_stack_base()
Destructor.
Definition intrusive_stack.h:203
void pop_into(TContainer &destination)
Definition intrusive_stack.h:133
intrusive_stack()
Constructor.
Definition intrusive_stack.h:239
size_t current_size
Counts the number of elements in the list.
Definition intrusive_stack.h:210
bool empty() const
Checks if the stack is in the empty state.
Definition intrusive_stack.h:176
link_type * p_top
The current top of the stack.
Definition intrusive_stack.h:207
link_type terminator
Terminator link of the queue.
Definition intrusive_stack.h:208
void push(link_type &value)
Definition intrusive_stack.h:102
intrusive_stack_base()
Constructor.
Definition intrusive_stack.h:194
Definition intrusive_stack.h:222
Definition intrusive_stack.h:92
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164