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///
6template<typename T>
8{
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
23template<typename T>
25{
26public:
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
72 {
73 public:
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
104private:
105 TStackListEntry* mFirst{nullptr};
106 TStackListEntry* mLast{nullptr};
107};
108
109#endif
StackListIterator(StackList &list)
Definition StackList.h:74
constexpr StackListIterator & operator++() noexcept
Definition StackList.h:85
constexpr StackListIterator & end() noexcept
Definition StackList.h:81
constexpr StackListIterator & begin() noexcept
Definition StackList.h:80
constexpr bool operator!=(const TStackListEntry *) const noexcept
Definition StackList.h:94
constexpr T & operator*() noexcept
Definition StackList.h:83
StackList is a container for a list which elements exist only on the stack.
Definition StackList.h:25
T & back() noexcept
Definition StackList.h:67
StackList()=default
constexpr TStackListEntry * end() noexcept
Definition StackList.h:102
constexpr StackListIterator begin() noexcept
Definition StackList.h:101
T * pop() noexcept
Definition StackList.h:47
constexpr bool empty() const noexcept
Definition StackList.h:69
StackListEntry< T > TStackListEntry
Definition StackList.h:27
void push(TStackListEntry &entry) noexcept
Definition StackList.h:31
Base class for StackList.
Definition StackList.h:8
StackListEntry * prev
Definition StackList.h:10
StackListEntry * next
Definition StackList.h:9