Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
refcount.h
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 : Commando / G Library *
24 * *
25 * $Archive:: /Commando/Code/wwlib/refcount.h $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 9/13/01 8:38p $*
30 * *
31 * $Revision:: 24 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36#if _MSC_VER >= 1000
37#pragma once
38#endif // _MSC_VER >= 1000
39
40#ifndef REFCOUNT_H
41#define REFCOUNT_H
42
43#ifndef ALWAYS_H
44#include "always.h"
45#endif
46
47#ifndef LISTNODE_H
48#include "listnode.h"
49#endif
50
51class RefCountClass;
52
53
54#ifndef NDEBUG
55
57{
58 char * File;
59 int Line;
60};
61
62#define NEW_REF( C, P ) ( (C*)RefCountClass::Set_Ref_Owner( W3DNEW C P, __FILE__, __LINE__ ) )
63#define SET_REF_OWNER( P ) ( RefCountClass::Set_Ref_Owner( P, __FILE__, __LINE__ ) )
64
65#else
66
67#define NEW_REF( C, P ) ( W3DNEW C P )
68#define SET_REF_OWNER( P ) P
69
70#endif
71
72
73/*
74** Macros for setting and releasing a pointer to a ref counted object.
75** If you have a member variable which can be pointed at a ref counted object and
76** you want to point it at some object. You must release whatever it currently points at,
77** point it at the new object, and add-ref the new object (if its not null...)
78*/
79#define REF_PTR_SET(dst,src) { if (src) (src)->Add_Ref(); if (dst) (dst)->Release_Ref(); (dst) = (src); }
80#define REF_PTR_RELEASE(x) { if (x) x->Release_Ref(); x = NULL; }
81
82
83/*
84** Rules regarding the use of RefCountClass
85**
86** If you call a function that returns a pointer to a RefCountClass,
87** you MUST Release_Ref() it
88**
89** If a functions calls you, and you return a pointer to a RefCountClass,
90** you MUST Add_Ref() it
91**
92** If you call a function and pass a pointer to a RefCountClass,
93** you DO NOT Add_Ref() it
94**
95** If a function calls you, and passes you a pointer to a RefCountClass,
96** if you keep the pointer, you MUST Add_Ref() and Release_Ref() it
97** otherwise, you DO NOT Add_Ref() or Release_Ref() it
98**
99*/
100
103
105{
106public:
107
109 NumRefs(1)
110 #ifndef NDEBUG
111 ,ActiveRefNode(this)
112 #endif
113 {
114 #ifndef NDEBUG
115 Add_Active_Ref(this);
116 Inc_Total_Refs(this);
117 #endif
118 }
119
121 NumRefs(1)
122 #ifndef NDEBUG
123 ,ActiveRefNode(this)
124 #endif
125 {
126 #ifndef NDEBUG
127 Add_Active_Ref(this);
128 Inc_Total_Refs(this);
129 #endif
130 }
131
132 /*
133 ** Add_Ref, call this function if you are going to keep a pointer
134 ** to this object.
135 */
136#ifdef NDEBUG
137 WWINLINE void Add_Ref(void) const { NumRefs++; }
138#else
139 void Add_Ref(void) const;
140#endif
141
142 /*
143 ** Release_Ref, call this function when you no longer need the pointer
144 ** to this object.
145 */
146 WWINLINE void Release_Ref(void) const {
147 #ifndef NDEBUG
148 Dec_Total_Refs(this);
149 #endif
150 NumRefs--;
151 assert(NumRefs >= 0);
152 if (NumRefs == 0) const_cast<RefCountClass*>(this)->Delete_This();
153 }
154
155
156 /*
157 ** Check the number of references to this object.
158 */
159 int Num_Refs(void) const { return NumRefs; }
160
161 /*
162 ** Delete_This - this function will be called when the object is being
163 ** destroyed as a result of its last reference being released. Its
164 ** job is to actually destroy the object.
165 */
166 virtual void Delete_This(void) { delete this; }
167
168 /*
169 ** Total_Refs - This static function can be used to get the total number
170 ** of references that have been made. Once you've released all of your
171 ** objects, it should go to zero.
172 */
173 static int Total_Refs(void) { return TotalRefs; }
174
175protected:
176
177 /*
178 ** Destructor, user should not have access to this...
179 */
180 virtual ~RefCountClass(void)
181 {
182 #ifndef NDEBUG
183 Remove_Active_Ref(this);
184 #endif
185 assert(NumRefs == 0);
186 }
187
188private:
189
190 /*
191 ** Current reference count of this object
192 */
193 mutable int NumRefs;
194
195 /*
196 ** Sum of all references to RefCountClass's. Should equal zero after
197 ** everything has been released.
198 */
199 static int TotalRefs;
200
201 /*
202 ** increments the total reference count
203 */
204 static void Inc_Total_Refs(const RefCountClass *);
205
206 /*
207 ** decrements the total reference count
208 */
209 static void Dec_Total_Refs(const RefCountClass *);
210
211public:
212
213#ifndef NDEBUG // Debugging stuff
214
215 /*
216 ** Node in the Active Refs List
217 */
219
220 /*
221 ** Auxiliary Active Ref Data
222 */
224
225 /*
226 ** List of the active referenced objects
227 */
229
230 /*
231 ** Adds the ref obj pointer to the active ref list
232 */
234
235 /*
236 ** Updates the owner file/line for the given ref obj in the active ref list
237 */
238 static RefCountClass * Set_Ref_Owner(RefCountClass *obj,char * file,int line);
239
240 /*
241 ** Remove the ref obj from the active ref list
242 */
243 static void Remove_Active_Ref(RefCountClass * obj);
244
245 /*
246 ** Confirm the active ref object using the pointer of the refbaseclass as a search key
247 */
248 static bool Validate_Active_Ref(RefCountClass * obj);
249
250#endif
251
252};
253
254
255
256#endif
#define WWINLINE
Definition always.h:172
Definition list.h:60
ActiveRefStruct ActiveRefInfo
Definition refcount.h:223
virtual ~RefCountClass(void)
Definition refcount.h:180
static RefCountClass * Add_Active_Ref(RefCountClass *obj)
Definition refcount.cpp:70
RefCountClass(const RefCountClass &)
Definition refcount.h:120
static RefCountClass * Set_Ref_Owner(RefCountClass *obj, char *file, int line)
Definition refcount.cpp:90
int Num_Refs(void) const
Definition refcount.h:159
RefCountNodeClass ActiveRefNode
Definition refcount.h:218
WWINLINE void Release_Ref(void) const
Definition refcount.h:146
static RefCountListClass ActiveRefList
Definition refcount.h:228
virtual void Delete_This(void)
Definition refcount.h:166
void Add_Ref(void) const
Definition refcount.cpp:171
RefCountClass(void)
Definition refcount.h:108
static void Remove_Active_Ref(RefCountClass *obj)
Definition refcount.cpp:115
static int Total_Refs(void)
Definition refcount.h:173
static bool Validate_Active_Ref(RefCountClass *obj)
Definition refcount.cpp:135
List< RefCountNodeClass * > RefCountListClass
Definition refcount.h:102
DataNode< RefCountClass * > RefCountNodeClass
Definition refcount.h:101