Skip to content

Commit 3010aa7

Browse files
committed
[CORE] Fix up RefCountClass, RefCountPtr<T>
1 parent 42d5c3a commit 3010aa7

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

Core/Libraries/Source/WWVegas/WWLib/ref_ptr.h

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@
138138
copying.
139139
140140
To create a RefCountPtr<T> from a raw pointer, use the global template functions
141-
Create_NEW should be used when wrapping a pointer that has just been created with NEW
142-
Create_Get should be used when wrapping a pointer that has been returned from a "Get" function
141+
Create_NoAddRef should be used when wrapping a pointer that has just been created with NEW
142+
Create_NoAddRef should be used when wrapping a pointer that has been returned from a "Get" function
143143
(the function added a reference prior to returning the pointer)
144-
Create_Peek should be used when wrapping a pointer that has been returned from a "Peek" function
144+
Create_AddRef should be used when wrapping a pointer that has been returned from a "Peek" function
145145
(the function did not add a reference prior to returning the pointer).
146146
147-
Create_Get and Create_Peek are provided to allow old code to migrate from manual reference count
147+
Create_NoAddRef and Create_AddRef are provided to allow old code to migrate from manual reference count
148148
management to RefCountPtr. New code written with RefCountPtr should rarely if ever use
149-
Create_Get and Create_Peek.
149+
Create_NoAddRef and Create_AddRef.
150150
151151
If it is absolutely necessary to extract the raw pointer, use Peek. Peek does not add a new
152152
reference to the object. Using a Peek'd object after its RefCountPtr has gone out of scope requires
@@ -158,9 +158,9 @@
158158
159159
Automatic construction of a RefCountPtr from a raw pointer is enabled if
160160
ALLOW_AUTOMATIC_REF_COUNT_PTR_CONSTRUCTION is defined.
161-
This may be useful when migrating existing code to use RefCountPtr, but is completely safe,
161+
This may be useful when migrating existing code to use RefCountPtr, but is not completely safe,
162162
since it is not possible to determine if the pointer is being Get'd or Peek'd.
163-
Please note that the constructor WILL add a reference to the object, which errs on the side
163+
Please note that the constructor WILL add a reference to the object, which errors on the side
164164
of leaking references rather than prematurely deleting objects. Whenever possible, use the
165165
explicit global Create_* functions rather than the automatic conversion.
166166
@@ -216,25 +216,25 @@
216216
};
217217
*/
218218

219-
#define ALLOW_AUTOMATIC_REF_COUNT_PTR_CONSTRUCTION
220219

221220
class DummyPtrType;
222221

223222
template <class T>
224223
class RefCountPtr
225224
{
226225
public:
227-
friend RefCountPtr<T> Create_NEW(T *t)
228-
{
229-
return RefCountPtr<T>(t, RefCountPtr<T>::GET);
230-
}
231226

232-
friend RefCountPtr<T> Create_Get(T *t)
227+
// Creates a RefCountPtr<T> and does not increment the reference counter of the passed object.
228+
// Is generally used for "New" and "Get" functions.
229+
static RefCountPtr<T> Create_NoAddRef(T *t)
233230
{
231+
WWASSERT(t == NULL || t->Num_Refs() >= 1);
234232
return RefCountPtr<T>(t, RefCountPtr<T>::GET);
235233
}
236234

237-
friend RefCountPtr<T> Create_Peek(T *t)
235+
// Creates a RefCountPtr<T> and increments the reference counter of the passed object.
236+
// Is generally used for "Peek" functions.
237+
static RefCountPtr<T> Create_AddRef(T *t)
238238
{
239239
return RefCountPtr<T>(t, RefCountPtr<T>::PEEK);
240240
}
@@ -373,20 +373,28 @@ class RefCountPtr
373373

374374
T & operator *(void) const
375375
{
376-
G_ASSERT(0 != Referent);
376+
WWASSERT(0 != Referent);
377377
return *Referent;
378378
}
379379

380-
// Note : This should typiccally only be used when mixing code that uses RefCountPtr and
380+
// Note : This should typically only be used when mixing code that uses RefCountPtr and
381381
// manually managed ref counts on raw points.
382-
// Code that consistently uses RefCountPtr should never get ahold of a raw T*
382+
// Code that consistently uses RefCountPtr should never get a hold of a raw T*
383383
T * Peek(void) const
384384
{
385385
return Referent;
386386
}
387387

388+
// Releases the held pointer without changing its reference counter.
389+
T * Release(void)
390+
{
391+
T * p = Referent;
392+
Referent = 0;
393+
return p;
394+
}
395+
388396
private:
389-
enum ReferenceHandling { GET, PEEK};
397+
enum ReferenceHandling { GET, PEEK };
390398

391399
RefCountPtr(T * referent, ReferenceHandling reference_handling)
392400
: Referent(referent)
@@ -442,7 +450,7 @@ bool operator !=(DummyPtrType * dummy, const RefCountPtr<RHS> & rhs)
442450
template <class Derived, class Base>
443451
RefCountPtr<Derived> Static_Cast(const RefCountPtr<Base> & base)
444452
{
445-
return Create_Peek((Derived *)base.Peek());
453+
return RefCountPtr<Derived>::Create_AddRef((Derived *)base.Peek());
446454
}
447455

448456
#endif

Core/Libraries/Source/WWVegas/WWLib/refcount.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include "LISTNODE.H"
4949
#endif
5050

51+
#include "wwdebug.h"
52+
5153
class RefCountClass;
5254

5355

@@ -148,7 +150,7 @@ class RefCountClass
148150
Dec_Total_Refs(this);
149151
#endif
150152
NumRefs--;
151-
assert(NumRefs >= 0);
153+
WWASSERT(NumRefs >= 0);
152154
if (NumRefs == 0) const_cast<RefCountClass*>(this)->Delete_This();
153155
}
154156

@@ -182,7 +184,7 @@ class RefCountClass
182184
#ifndef NDEBUG
183185
Remove_Active_Ref(this);
184186
#endif
185-
assert(NumRefs == 0);
187+
WWASSERT(NumRefs == 0);
186188
}
187189

188190
private:

Generals/Code/GameEngine/Source/Common/StateMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ StateReturnType StateMachine::updateStateMachine()
439439
// Calling m_currentState->update() can release this state machine in certain circumstances,
440440
// for example if something kills the entity of this state machine as a result of this state update.
441441
// See https://github.com/TheSuperHackers/GeneralsGameCode/issues/212
442-
RefCountPtr<StateMachine> refThis(this);
442+
RefCountPtr<StateMachine> refThis = RefCountPtr<StateMachine>::Create_AddRef(this);
443443

444444
// update() can change m_currentState, so save it for a moment...
445445
State* stateBeforeUpdate = m_currentState;

GeneralsMD/Code/GameEngine/Source/Common/StateMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ StateReturnType StateMachine::updateStateMachine()
439439
// Calling m_currentState->update() can release this state machine in certain circumstances,
440440
// for example if something kills the entity of this state machine as a result of this state update.
441441
// See https://github.com/TheSuperHackers/GeneralsGameCode/issues/212
442-
RefCountPtr<StateMachine> refThis(this);
442+
RefCountPtr<StateMachine> refThis = RefCountPtr<StateMachine>::Create_AddRef(this);
443443

444444
// update() can change m_currentState, so save it for a moment...
445445
State* stateBeforeUpdate = m_currentState;

0 commit comments

Comments
 (0)