StackList.h
Go to the documentation of this file.
1
#ifndef INSIGHTS_STACK_LIST_H
2
#define INSIGHTS_STACK_LIST_H
3
4
/// \brief Base class for \ref StackList.
5
///
6
template
<
typename
T>
7
struct
StackListEntry
8
{
9
StackListEntry
*
next
{
nullptr
};
10
StackListEntry
*
prev
{
nullptr
};
11
};
12
13
/// \brief StackList is a container for a list which elements exist only on the stack.
14
///
15
/// The purpose is to keep allocation with new and delete away as long as stack seems to be
16
/// available. The class is range based for loop ready.
17
/// To use it a class needs to derive from \ref StackListEntry.
18
///
19
/// Example:
20
/// \code
21
// class Foo : public StackListEntry<Foo> {...};
22
/// \endcode
23
template
<
typename
T>
24
class
StackList
25
{
26
public
:
27
using
TStackListEntry
=
StackListEntry<T>
;
28
29
StackList
() =
default
;
30
31
void
push
(
TStackListEntry
& entry) noexcept
32
{
33
entry.next =
nullptr
;
34
35
if
(!mFirst) {
36
mFirst = &entry;
37
mFirst->
prev
=
nullptr
;
38
39
}
else
{
40
mLast->
next
= &entry;
41
entry.
prev
= mLast;
42
}
43
44
mLast = &entry;
45
}
46
47
T*
pop
() noexcept
48
{
49
TStackListEntry
* last = mLast;
50
51
if
(mLast) {
52
mLast = mLast->
prev
;
53
54
if
(mLast) {
55
mLast->
next
=
nullptr
;
56
57
last->
next
=
nullptr
;
58
last->
prev
=
nullptr
;
59
}
else
{
60
mFirst =
nullptr
;
61
}
62
}
63
64
return
static_cast<
T*
>
(last);
65
}
66
67
T&
back
() noexcept {
return
*
static_cast<
T*
>
(mLast); }
68
69
constexpr
bool
empty
() const noexcept {
return
(
nullptr
== mLast); }
70
71
class
StackListIterator
72
{
73
public
:
74
StackListIterator
(
StackList
& list)
75
: mCurrent{list.mFirst}
76
, mLast{list.mLast}
77
{
78
}
79
80
constexpr
StackListIterator
&
begin
() noexcept {
return
*
this
; }
81
constexpr
StackListIterator
&
end
() noexcept {
return
*
this
; }
82
83
constexpr T&
operator*
() noexcept {
return
*
static_cast<
T*
>
(mCurrent); }
84
85
constexpr
StackListIterator
&
operator++
() noexcept
86
{
87
if
(mCurrent) {
88
mCurrent = mCurrent->
next
;
89
}
90
91
return
*
this
;
92
}
93
94
constexpr
bool
operator!=
(
const
TStackListEntry
*)
const
noexcept {
return
(
nullptr
!= mCurrent); }
95
96
private
:
97
TStackListEntry
* mCurrent;
98
TStackListEntry
* mLast;
99
};
100
101
constexpr
StackListIterator
begin
() noexcept {
return
StackListIterator
{*
this
}; }
102
constexpr
TStackListEntry
*
end
() noexcept {
return
mLast; }
103
104
private
:
105
TStackListEntry
* mFirst{
nullptr
};
106
TStackListEntry
* mLast{
nullptr
};
107
};
108
109
#endif
StackList::StackListIterator
Definition:
StackList.h:72
StackList::StackListIterator::operator++
constexpr StackListIterator & operator++() noexcept
Definition:
StackList.h:85
StackList::StackListIterator::operator*
constexpr T & operator*() noexcept
Definition:
StackList.h:83
StackList::StackListIterator::StackListIterator
StackListIterator(StackList &list)
Definition:
StackList.h:74
StackList::StackListIterator::operator!=
constexpr bool operator!=(const TStackListEntry *) const noexcept
Definition:
StackList.h:94
StackList::StackListIterator::begin
constexpr StackListIterator & begin() noexcept
Definition:
StackList.h:80
StackList::StackListIterator::end
constexpr StackListIterator & end() noexcept
Definition:
StackList.h:81
StackList
StackList is a container for a list which elements exist only on the stack.
Definition:
StackList.h:25
StackList::end
constexpr TStackListEntry * end() noexcept
Definition:
StackList.h:102
StackList::StackList
StackList()=default
StackList::pop
T * pop() noexcept
Definition:
StackList.h:47
StackList::back
T & back() noexcept
Definition:
StackList.h:67
StackList::begin
constexpr StackListIterator begin() noexcept
Definition:
StackList.h:101
StackList::empty
constexpr bool empty() const noexcept
Definition:
StackList.h:69
StackList::TStackListEntry
StackListEntry< T > TStackListEntry
Definition:
StackList.h:27
StackList::push
void push(TStackListEntry &entry) noexcept
Definition:
StackList.h:31
StackListEntry
Base class for StackList.
Definition:
StackList.h:8
StackListEntry::prev
StackListEntry * prev
Definition:
StackList.h:10
StackListEntry::next
StackListEntry * next
Definition:
StackList.h:9
Generated by
1.9.1