forked from Phobos-developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericList.h
107 lines (88 loc) · 2.33 KB
/
GenericList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//General linked list class
#pragma once
#include <YRPPCore.h>
class GenericList;
class GenericNode
{
public:
GenericNode() : NextNode(nullptr), PrevNode(nullptr) { }
virtual ~GenericNode() { Unlink(); }
GenericNode(GenericNode& node) { node.Link(this); }
GenericNode& operator = (GenericNode& node)
{
if (&node != this)
node.Link(this);
return *this;
}
void Unlink()
{
if (this->IsValid())
{
this->PrevNode->NextNode = this->NextNode;
this->NextNode->PrevNode = this->PrevNode;
this->PrevNode = nullptr;
this->NextNode = nullptr;
}
}
GenericList* MainList() const
{
GenericNode const* node = this;
while (node->PrevNode)
node = this->PrevNode;
return (GenericList*)this;
}
void Link(GenericNode* pNode)
{
pNode->Unlink();
pNode->NextNode = this->NextNode;
pNode->PrevNode = this;
if (this->NextNode) this->NextNode->PrevNode = pNode;
this->NextNode = pNode;
}
GenericNode* Next() const { return this->NextNode; }
GenericNode* Prev() const { return this->PrevNode; }
bool IsValid() const { return this && this->NextNode && this->PrevNode; }
protected:
GenericNode* NextNode;
GenericNode* PrevNode;
};
class GenericList
{
public:
GenericList()
{
FirstNode.Link(&LastNode);
}
virtual ~GenericList() { }
GenericNode* First() const { return FirstNode.Next(); }
GenericNode* Last() const { return LastNode.Prev(); }
bool IsEmpty() const { return !FirstNode.Next()->IsValid(); }
void AddHead(GenericNode* pNode) { FirstNode.Link(pNode); }
void AddTail(GenericNode* pNode) { LastNode.Prev()->Link(pNode); }
void Delete() { while (this->FirstNode.Next()->IsValid()) GameDelete(this->FirstNode.Next()); }
protected:
GenericNode FirstNode;
GenericNode LastNode;
private:
GenericList(GenericList& list);
GenericList& operator = (GenericList const&) = delete;
};
template<class T> class List;
template<class T>
class Node : public GenericNode
{
public:
virtual ~Node(){ }
List<T>* MainList() const { return (List<T> *)GenericNode::MainList(); }
T* Next() const { return (T*)GenericNode::Next(); }
T* Prev() const { return (T*)GenericNode::Prev(); }
bool IsValid() const { return GenericNode::IsValid(); }
};
template<class T>
class List : public GenericList
{
public:
virtual ~List(){ }
T* First() const { return (T*)GenericList::First(); }
T* Last() const { return (T*)GenericList::Last(); }
};