Embedded Template Library  1.0
intrusive_stack.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_STACK_INCLUDED
32 #define ETL_INTRUSIVE_STACK_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 "28"
43 
44 namespace etl
45 {
46  //***************************************************************************
49  //***************************************************************************
51  {
52  public:
53 
54  intrusive_stack_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_stack_empty(string_type file_name_, numeric_type line_number_)
69  : intrusive_stack_exception(ETL_ERROR_TEXT("intrusive_stack: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_top != ETL_NULLPTR)
96  {
97  etl::link(value, p_top);
98  }
99 
100  p_top = &value;
101 
102  ++current_size;
103  }
104 
105  //*************************************************************************
108  //*************************************************************************
109  void pop()
110  {
111 #if defined(ETL_CHECK_PUSH_POP)
112  ETL_ASSERT(!empty(), ETL_ERROR(intrusive_stack_empty));
113 #endif
114  link_type* p_next = p_top->etl_next;
115  p_top = p_next;
116  --current_size;
117  }
118 
119  //*************************************************************************
123  //*************************************************************************
124  template <typename TContainer>
125  void pop_into(TContainer& destination)
126  {
127  link_type* p_link = p_top;
128  pop();
129  destination.push(*p_link);
130  }
131 
132  //*************************************************************************
134  //*************************************************************************
135  void reverse()
136  {
137  link_type* previous = ETL_NULLPTR;
138  link_type* current = p_top;
139  link_type* next;
140 
141  while (current != ETL_NULLPTR)
142  {
143  next = current->etl_next;
144  current->etl_next = previous;
145  previous = current;
146  current = next;
147  }
148 
149  p_top = previous;
150  }
151 
152  //*************************************************************************
154  //*************************************************************************
155  void clear()
156  {
157  while (!empty())
158  {
159  pop();
160  }
161 
162  current_size = 0;
163  }
164 
165  //*************************************************************************
167  //*************************************************************************
168  bool empty() const
169  {
170  return current_size == 0;
171  }
172 
173  //*************************************************************************
175  //*************************************************************************
176  size_t size() const
177  {
178  return current_size;
179  }
180 
181  protected:
182 
183  //*************************************************************************
185  //*************************************************************************
187  : p_top(ETL_NULLPTR),
188  current_size(0)
189  {
190  }
191 
192  //*************************************************************************
194  //*************************************************************************
196  {
197  }
198 
199  link_type* p_top;
200 
201  size_t current_size;
202  };
203 
204  //***************************************************************************
210  //***************************************************************************
211  template <typename TValue, typename TLink>
213  {
214  public:
215 
216  // Node typedef.
217  typedef typename etl::intrusive_stack_base<TLink>::link_type link_type;
218 
219  // STL style typedefs.
220  typedef TValue value_type;
221  typedef value_type* pointer;
222  typedef const value_type* const_pointer;
223  typedef value_type& reference;
224  typedef const value_type& const_reference;
225  typedef size_t size_type;
226 
227  //*************************************************************************
229  //*************************************************************************
231  : intrusive_stack_base<TLink>()
232  {
233  }
234 
235  //*************************************************************************
239  //*************************************************************************
240  reference top()
241  {
242  return *static_cast<TValue*>(this->p_top);
243  }
244 
245  //*************************************************************************
248  //*************************************************************************
249  const_reference top() const
250  {
251  return *static_cast<const TValue*>(this->p_top);
252  }
253 
254  private:
255 
256  // Disable copy construction and assignment.
258  intrusive_stack& operator = (const intrusive_stack& rhs);
259  };
260 }
261 
262 #undef ETL_FILE
263 
264 #endif
Definition: intrusive_stack.h:65
Definition: intrusive_stack.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()
Definition: intrusive_stack.h:109
size_t size() const
Returns the number of elements.
Definition: intrusive_stack.h:176
reference top()
Definition: intrusive_stack.h:240
void reverse()
Reverses the stack order.
Definition: intrusive_stack.h:135
void clear()
Clears the stack to the empty state.
Definition: intrusive_stack.h:155
const_reference top() const
Definition: intrusive_stack.h:249
~intrusive_stack_base()
Destructor.
Definition: intrusive_stack.h:195
void pop_into(TContainer &destination)
Definition: intrusive_stack.h:125
intrusive_stack()
Constructor.
Definition: intrusive_stack.h:230
size_t current_size
Counts the number of elements in the list.
Definition: intrusive_stack.h:201
bool empty() const
Checks if the stack is in the empty state.
Definition: intrusive_stack.h:168
link_type * p_top
The current top of the stack.
Definition: intrusive_stack.h:199
void push(link_type &value)
Definition: intrusive_stack.h:91
intrusive_stack_base()
Constructor.
Definition: intrusive_stack.h:186
Definition: intrusive_stack.h:213
Definition: intrusive_stack.h:81
Definition: absolute.h:37