OpenBarnyard
 
Loading...
Searching...
No Matches
TQList.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/T2Iterator.h"
3
5
6template <class T>
7class TQList
8{
9public:
10 class TNode
11 {
12 public:
13 friend TQList;
16
17 public:
19 {
20 m_pNext = TSTATICCAST( T, this );
21 m_pPrev = TSTATICCAST( T, this );
22 }
23
25 {
26 m_pPrev->m_pNext = m_pNext;
27 m_pNext->m_pPrev = m_pPrev;
28 m_pNext = TSTATICCAST( T, this );
29 m_pPrev = TSTATICCAST( T, this );
30 }
31
33 {
34 return this != m_pNext;
35 }
36
37 T* Next() const
38 {
39 return m_pNext;
40 }
41
42 T* Prev() const
43 {
44 return m_pPrev;
45 }
46
47 void Remove()
48 {
49 m_pPrev->m_pNext = m_pNext;
50 m_pNext->m_pPrev = m_pPrev;
51 m_pNext = TSTATICCAST( T, this );
52 m_pPrev = TSTATICCAST( T, this );
53 }
54
55 private:
56 void InsertAfter( TNode* a_pNode )
57 {
58 TASSERT( TFALSE == IsLinked() );
59 m_pPrev = TSTATICCAST( T, a_pNode );
60 m_pNext = a_pNode->m_pNext;
61 a_pNode->m_pNext = TSTATICCAST( T, this );
62 m_pNext->m_pPrev = TSTATICCAST( T, this );
63 }
64
65 void InsertBefore( TNode* a_pNode )
66 {
67 TASSERT( TFALSE == IsLinked() );
68 m_pNext = TSTATICCAST( T, a_pNode );
69 m_pPrev = a_pNode->m_pPrev;
70 a_pNode->m_pPrev = TSTATICCAST( T, this );
71 m_pPrev->m_pNext = TSTATICCAST( T, this );
72 }
73
74 private:
75 T* m_pNext;
76 T* m_pPrev;
77 };
78
79public:
80 TQList() = default;
81
83
84 void PushFront( T* a_pNode )
85 {
86 a_pNode->InsertAfter( &m_oRoot );
87 }
88
89 void Push( T* a_pNode )
90 {
91 a_pNode->InsertBefore( &m_oRoot );
92 }
93
94 T* Pop()
95 {
96 auto pFirstElement = m_oRoot.m_pNext;
97 pFirstElement->Remove();
98 return pFirstElement;
99 }
100
102 {
103 return m_oRoot.m_pNext;
104 }
105
107 {
108 return m_oRoot.m_pNext;
109 }
110
111 const TNode* End() const
112 {
113 return &m_oRoot;
114 }
115
116 void Clear()
117 {
118 while ( !IsEmpty() )
119 {
120 Remove( Begin() );
121 }
122 }
123
124 void Remove( T* a_pNode )
125 {
126 a_pNode->Remove();
127 }
128
130 {
131 return !m_oRoot.IsLinked();
132 }
133
134private:
135 TNode m_oRoot;
136};
137
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define T2_DEFINE_ITERATOR(TYPE, NODE_TYPE)
Definition T2Iterator.h:19
#define T2_DEFINE_ITERATOR_FRIEND()
Definition T2Iterator.h:16
#define TFALSE
Definition Typedefs.h:24
bool TBOOL
Definition Typedefs.h:6
TNode::Iterator Head() const
Definition TQList.h:101
T * Pop()
Definition TQList.h:94
~TQList()
Definition TQList.h:82
void PushFront(T *a_pNode)
Definition TQList.h:84
void Clear()
Definition TQList.h:116
void Push(T *a_pNode)
Definition TQList.h:89
TNode::Iterator Begin() const
Definition TQList.h:106
TBOOL IsEmpty() const
Definition TQList.h:129
TQList()=default
const TNode * End() const
Definition TQList.h:111
void Remove(T *a_pNode)
Definition TQList.h:124
friend TQList
Definition TQList.h:13
Toshi::T2Iterator< T, TNode > Iterator
Definition TQList.h:15
T * Next() const
Definition TQList.h:37
TBOOL IsLinked() const
Definition TQList.h:32
void Remove()
Definition TQList.h:47
T * Prev() const
Definition TQList.h:42