Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
multilist.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWLib *
24 * *
25 * $Archive:: /Commando/Code/wwlib/multilist.cpp $*
26 * *
27 * Original Author:: Greg Hjelstrom *
28 * *
29 * $Author:: Greg_h $*
30 * *
31 * $Modtime:: 6/12/01 9:51a $*
32 * *
33 * $Revision:: 9 $*
34 * *
35 *---------------------------------------------------------------------------------------------*
36 * Functions: *
37 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38
39#include "multilist.h"
40#include "wwmemlog.h"
41
42/*
43** Delcare the pool for ListNodes
44*/
46
47
48/***********************************************************************************************
49
50 MultiListObjectClass Implementation.
51
52***********************************************************************************************/
53
55{
56 while (ListNode) {
57 ListNode->List->Internal_Remove(this);
58 }
59}
60
61
62/***********************************************************************************************
63
64 GenericMultiListClass Implementation.
65 This class has all of the real implementation. Templates are provided in the header file
66 which create typed lists by simply type-casting the void pointers that GenericMultiList uses.
67
68***********************************************************************************************/
69
74
76{
77 assert(obj);
78
79 MultiListNodeClass* lnode = obj->Get_List_Node();
80 while (lnode) {
81 if (lnode->List == this) return true;
82 lnode = lnode->NextList;
83 }
84 return false;
85}
86
88{
89 int counter = 0;
91 for (it.First(); !it.Is_Done(); it.Next()) {
92 counter++;
93 }
94 return counter;
95}
96
98{
100 assert(obj);
101
102 if (onlyonce && Is_In_List(obj)) {
103 return false;
104 }
105
106 // allocate a list node for this object
108 node->Object = obj;
109
110 // link the list node into the list of list nodes for the object
111 node->NextList = obj->Get_List_Node();
112 obj->Set_List_Node(node);
113
114 // link the node to the objects in *this* list
115 node->Prev = &(Head);
116 node->Next = Head.Next;
117 node->Next->Prev = node;
118 node->Prev->Next = node;
119
120 // identify this node as being part of this list
121 node->List = this;
122
123 return true;
124}
125
127{
129 assert(obj);
130
131 if (onlyonce && Is_In_List(obj)) {
132 return false;
133 }
134
135 // allocate a list node for this object
137 node->Object = obj;
138
139 // link the list node into the list of list nodes for the object
140 node->NextList = obj->Get_List_Node();
141 obj->Set_List_Node(node);
142
143 // link the node to the tail of this list (next should be head, prev should be head.Prev)
144 node->Prev = Head.Prev;
145 node->Next = &(Head);
146 node->Next->Prev = node;
147 node->Prev->Next = node;
148
149 // identify this node as being part of this list
150 node->List = this;
151
152 return true;
153}
154
155bool GenericMultiListClass::Internal_Add_After(MultiListObjectClass * obj,const MultiListObjectClass * existing_list_member,bool onlyonce)
156{
158 assert(obj);
159 assert(existing_list_member);
160
161 if (onlyonce && Is_In_List(obj)) {
162 return false;
163 }
164
165 // find the node hanging off 'existing_list_member' that corresponds to this list (O(numlists))
166 MultiListNodeClass * existing_node = existing_list_member->Get_List_Node();
167 while ((existing_node->List != this) && (existing_node)) {
168 existing_node = existing_node->NextList;
169 }
170
171 if (existing_node == NULL) {
172 return false; // he's not in this list!
173 }
174
175 // allocate a node
177 node->Object = obj;
178
179 // link the node into the list of list nodes for the object
180 node->NextList = obj->Get_List_Node();
181 obj->Set_List_Node(node);
182
183 // now, link the new node after existing_node
184 node->Prev = existing_node;
185 node->Next = existing_node->Next;
186 node->Next->Prev = node;
187 node->Prev->Next = node;
188 node->List = this;
189
190 return true;
191}
192
194{
195 // find the list node in this object that belongs to this list
196 MultiListNodeClass * lnode = obj->Get_List_Node();
197 MultiListNodeClass * prevlnode = 0;
198
199 while ((lnode) && (lnode->List != this)) {
200 prevlnode = lnode;
201 lnode = lnode->NextList;
202 }
203
204 if (lnode == 0) {
205 return false;
206 }
207
208 // now we've found the node which corresponds to this list,
209 // unlink from the list of objects
210 lnode->Prev->Next = lnode->Next;
211 lnode->Next->Prev = lnode->Prev;
212
213 // unlink from the list of list nodes
214 if (prevlnode) {
215 prevlnode->NextList = lnode->NextList;
216 } else {
217 assert(obj->Get_List_Node() == lnode); // must be first list obj is in...
218 obj->Set_List_Node(lnode->NextList);
219 }
220
221 // delete the link
222 delete lnode;
223
224 return true;
225}
226
228{
229 if (Head.Next == &Head) {
230 return 0; // no more objects
231 }
232
233 MultiListNodeClass * node = Head.Next;
234 MultiListObjectClass * obj = node->Object;
235
236 // remove the object from our list
237 Internal_Remove(obj);
238
239 // here you go.
240 return obj;
241}
242
243
244
245
#define NULL
Definition BaseType.h:92
bool Is_In_List(MultiListObjectClass *obj)
Definition multilist.h:149
bool Contains(MultiListObjectClass *obj)
Definition multilist.cpp:75
friend class MultiListObjectClass
Definition multilist.h:146
bool Internal_Remove(MultiListObjectClass *obj)
bool Internal_Add(MultiListObjectClass *obj, bool onlyonce=true)
Definition multilist.cpp:97
bool Internal_Add_Tail(MultiListObjectClass *obj, bool onlyonce=true)
virtual ~GenericMultiListClass(void)
Definition multilist.cpp:70
MultiListObjectClass * Internal_Remove_List_Head(void)
bool Internal_Add_After(MultiListObjectClass *obj, const MultiListObjectClass *existing_list_member, bool onlyonce=true)
friend class GenericMultiListIterator
Definition multilist.h:145
void First(GenericMultiListClass *list)
Definition multilist.h:182
MultiListObjectClass * Object
Definition multilist.h:107
MultiListNodeClass * Prev
Definition multilist.h:104
GenericMultiListClass * List
Definition multilist.h:108
MultiListNodeClass * NextList
Definition multilist.h:106
MultiListNodeClass * Next
Definition multilist.h:105
void Set_List_Node(MultiListNodeClass *node)
Definition multilist.h:85
virtual ~MultiListObjectClass(void)
Definition multilist.cpp:54
MultiListNodeClass * Get_List_Node() const
Definition multilist.h:84
#define DEFINE_AUTO_POOL(T, BLOCKSIZE)
Definition mempool.h:160
int counter
Definition patch.cpp:410
@ MEM_GAMEDATA
Definition wwmemlog.h:67
#define WWMEMLOG(category)
Definition wwmemlog.h:183