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 "error_handler.h"
36#include "intrusive_links.h"
37#include "type_traits.h"
38
39#include <stddef.h>
40
41namespace etl
42{
43 //***************************************************************************
46 //***************************************************************************
47 class intrusive_stack_exception : public etl::exception
48 {
49 public:
50
51 intrusive_stack_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_stack_empty : public intrusive_stack_exception
62 {
63 public:
64
65 intrusive_stack_empty(string_type file_name_, numeric_type line_number_)
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 //***************************************************************************
75 class intrusive_stack_value_is_already_linked : public intrusive_stack_exception
76 {
77 public:
78
79 intrusive_stack_value_is_already_linked(string_type file_name_, numeric_type line_number_)
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 //***************************************************************************
90 //***************************************************************************
91 template <typename TLink>
93 {
94 public:
95
96 // Node typedef.
97 typedef TLink link_type;
98
99 //*************************************************************************
102 //*************************************************************************
103 void push(link_type& value)
104 {
105 ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_stack_value_is_already_linked));
106
107 value.etl_next = p_top;
108 p_top = &value;
109
110 ++current_size;
111 }
112
113 //*************************************************************************
116 //*************************************************************************
117 void pop()
118 {
119 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(!empty(), ETL_ERROR(intrusive_stack_empty));
120
121 link_type* p_next = p_top->etl_next;
122 p_top->clear();
123 p_top = p_next;
124 --current_size;
125 }
126
127 //*************************************************************************
132 //*************************************************************************
133 template <typename TContainer>
134 void pop_into(TContainer& destination)
135 {
136 link_type* p_link = p_top;
137 pop();
138 destination.push(*p_link);
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 void reverse()
145 {
146 link_type* previous = &terminator;
147 link_type* current = p_top;
148 link_type* next;
149
150 while (current != &terminator)
151 {
152 next = current->etl_next;
153 current->etl_next = previous;
154 previous = current;
155 current = next;
156 }
157
158 p_top = previous;
159 }
160
161 //*************************************************************************
163 //*************************************************************************
164 void clear()
165 {
166 while (!empty())
167 {
168 pop();
169 }
170
171 current_size = 0;
172 }
173
174 //*************************************************************************
176 //*************************************************************************
177 bool empty() const
178 {
179 return current_size == 0;
180 }
181
182 //*************************************************************************
184 //*************************************************************************
185 size_t size() const
186 {
187 return current_size;
188 }
189
190 protected:
191
192 //*************************************************************************
194 //*************************************************************************
196 : p_top(&terminator)
197 , current_size(0)
198 {
199 }
200
201 //*************************************************************************
203 //*************************************************************************
205
206 link_type* p_top;
207 link_type terminator;
208
210 };
211
212 //***************************************************************************
219 //***************************************************************************
220 template <typename TValue, typename TLink>
222 {
223 public:
224
225 // Node typedef.
226 typedef typename etl::intrusive_stack_base<TLink>::link_type link_type;
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 //*************************************************************************
240 : intrusive_stack_base<TLink>()
241 {
242 }
243
244 //*************************************************************************
249 //*************************************************************************
250 reference top()
251 {
252 ETL_ASSERT_CHECK_EXTRA(!this->empty(), ETL_ERROR(intrusive_stack_empty));
253 return *static_cast<TValue*>(this->p_top);
254 }
255
256 //*************************************************************************
261 //*************************************************************************
262 const_reference top() const
263 {
264 ETL_ASSERT_CHECK_EXTRA(!this->empty(), ETL_ERROR(intrusive_stack_empty));
265 return *static_cast<const TValue*>(this->p_top);
266 }
267
268 private:
269
270 // Disable copy construction and assignment.
272 intrusive_stack& operator=(const intrusive_stack& rhs);
273 };
274} // namespace etl
275
276#endif
Definition intrusive_stack.h:62
Definition intrusive_stack.h:76
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
void pop()
Definition intrusive_stack.h:117
size_t size() const
Returns the number of elements.
Definition intrusive_stack.h:185
reference top()
Definition intrusive_stack.h:250
void reverse()
Reverses the stack order.
Definition intrusive_stack.h:144
void clear()
Clears the stack to the empty state.
Definition intrusive_stack.h:164
const_reference top() const
Definition intrusive_stack.h:262
~intrusive_stack_base()
Destructor.
Definition intrusive_stack.h:204
void pop_into(TContainer &destination)
Definition intrusive_stack.h:134
intrusive_stack()
Constructor.
Definition intrusive_stack.h:239
size_t current_size
Definition intrusive_stack.h:209
bool empty() const
Checks if the stack is in the empty state.
Definition intrusive_stack.h:177
link_type * p_top
Definition intrusive_stack.h:206
link_type terminator
Definition intrusive_stack.h:207
void push(link_type &value)
Definition intrusive_stack.h:103
intrusive_stack_base()
Constructor.
Definition intrusive_stack.h:195
Definition intrusive_stack.h:222
Definition intrusive_stack.h:93
bitset_ext
Definition absolute.h:40