Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
intrusive_stack.h
Go to the documentation of this file.
1
2
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 John Wellbelove
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 "
platform.h
"
35
#include "
error_handler.h
"
36
#include "
intrusive_links.h
"
37
#include "
type_traits.h
"
38
39
#include <stddef.h>
40
41
namespace
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>
92
class
intrusive_stack_base
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
//*************************************************************************
195
intrusive_stack_base
()
196
:
p_top
(&
terminator
)
197
,
current_size
(0)
198
{
199
}
200
201
//*************************************************************************
203
//*************************************************************************
204
~intrusive_stack_base
() {}
205
206
link_type*
p_top
;
207
link_type
terminator
;
208
209
size_t
current_size
;
210
};
211
212
//***************************************************************************
219
//***************************************************************************
220
template
<
typename
TValue,
typename
TLink>
221
class
intrusive_stack
:
public
etl::intrusive_stack_base
<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
//*************************************************************************
239
intrusive_stack
()
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.
271
intrusive_stack
(
const
intrusive_stack
&);
272
intrusive_stack
& operator=(
const
intrusive_stack
& rhs);
273
};
274
}
// namespace etl
275
276
#endif
etl::intrusive_stack_empty
Definition
intrusive_stack.h:62
etl::intrusive_stack_value_is_already_linked
Definition
intrusive_stack.h:76
error_handler.h
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition
exception.h:81
etl::exception
Definition
exception.h:59
etl::intrusive_stack_base::pop
void pop()
Definition
intrusive_stack.h:117
etl::intrusive_stack_base::size
size_t size() const
Returns the number of elements.
Definition
intrusive_stack.h:185
etl::intrusive_stack::top
reference top()
Definition
intrusive_stack.h:250
etl::intrusive_stack_base::reverse
void reverse()
Reverses the stack order.
Definition
intrusive_stack.h:144
etl::intrusive_stack_base::clear
void clear()
Clears the stack to the empty state.
Definition
intrusive_stack.h:164
etl::intrusive_stack::top
const_reference top() const
Definition
intrusive_stack.h:262
etl::intrusive_stack_base::~intrusive_stack_base
~intrusive_stack_base()
Destructor.
Definition
intrusive_stack.h:204
etl::intrusive_stack_base::pop_into
void pop_into(TContainer &destination)
Definition
intrusive_stack.h:134
etl::intrusive_stack::intrusive_stack
intrusive_stack()
Constructor.
Definition
intrusive_stack.h:239
etl::link_type::current_size
size_t current_size
Definition
intrusive_stack.h:209
etl::intrusive_stack_base::empty
bool empty() const
Checks if the stack is in the empty state.
Definition
intrusive_stack.h:177
etl::link_type::p_top
link_type * p_top
Definition
intrusive_stack.h:206
etl::link_type::terminator
link_type terminator
Definition
intrusive_stack.h:207
etl::intrusive_stack_base::push
void push(link_type &value)
Definition
intrusive_stack.h:103
etl::intrusive_stack_base::intrusive_stack_base
intrusive_stack_base()
Constructor.
Definition
intrusive_stack.h:195
etl::intrusive_stack
Definition
intrusive_stack.h:222
etl::intrusive_stack_base
Definition
intrusive_stack.h:93
intrusive_links.h
etl
Definition
absolute.h:40
platform.h
type_traits.h
include
etl
intrusive_stack.h
Generated on
for Embedded Template Library by
1.17.0