|
138 | 138 | copying.
|
139 | 139 |
|
140 | 140 | 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 |
143 | 143 | (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 |
145 | 145 | (the function did not add a reference prior to returning the pointer).
|
146 | 146 |
|
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 |
148 | 148 | 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. |
150 | 150 |
|
151 | 151 | If it is absolutely necessary to extract the raw pointer, use Peek. Peek does not add a new
|
152 | 152 | reference to the object. Using a Peek'd object after its RefCountPtr has gone out of scope requires
|
|
158 | 158 |
|
159 | 159 | Automatic construction of a RefCountPtr from a raw pointer is enabled if
|
160 | 160 | 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, |
162 | 162 | 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 |
164 | 164 | of leaking references rather than prematurely deleting objects. Whenever possible, use the
|
165 | 165 | explicit global Create_* functions rather than the automatic conversion.
|
166 | 166 |
|
|
216 | 216 | };
|
217 | 217 | */
|
218 | 218 |
|
219 |
| -#define ALLOW_AUTOMATIC_REF_COUNT_PTR_CONSTRUCTION |
220 | 219 |
|
221 | 220 | class DummyPtrType;
|
222 | 221 |
|
223 | 222 | template <class T>
|
224 | 223 | class RefCountPtr
|
225 | 224 | {
|
226 | 225 | public:
|
227 |
| - friend RefCountPtr<T> Create_NEW(T *t) |
228 |
| - { |
229 |
| - return RefCountPtr<T>(t, RefCountPtr<T>::GET); |
230 |
| - } |
231 | 226 |
|
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) |
233 | 230 | {
|
| 231 | + WWASSERT(t == NULL || t->Num_Refs() >= 1); |
234 | 232 | return RefCountPtr<T>(t, RefCountPtr<T>::GET);
|
235 | 233 | }
|
236 | 234 |
|
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) |
238 | 238 | {
|
239 | 239 | return RefCountPtr<T>(t, RefCountPtr<T>::PEEK);
|
240 | 240 | }
|
@@ -373,20 +373,28 @@ class RefCountPtr
|
373 | 373 |
|
374 | 374 | T & operator *(void) const
|
375 | 375 | {
|
376 |
| - G_ASSERT(0 != Referent); |
| 376 | + WWASSERT(0 != Referent); |
377 | 377 | return *Referent;
|
378 | 378 | }
|
379 | 379 |
|
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 |
381 | 381 | // 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* |
383 | 383 | T * Peek(void) const
|
384 | 384 | {
|
385 | 385 | return Referent;
|
386 | 386 | }
|
387 | 387 |
|
| 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 | + |
388 | 396 | private:
|
389 |
| - enum ReferenceHandling { GET, PEEK}; |
| 397 | + enum ReferenceHandling { GET, PEEK }; |
390 | 398 |
|
391 | 399 | RefCountPtr(T * referent, ReferenceHandling reference_handling)
|
392 | 400 | : Referent(referent)
|
@@ -442,7 +450,7 @@ bool operator !=(DummyPtrType * dummy, const RefCountPtr<RHS> & rhs)
|
442 | 450 | template <class Derived, class Base>
|
443 | 451 | RefCountPtr<Derived> Static_Cast(const RefCountPtr<Base> & base)
|
444 | 452 | {
|
445 |
| - return Create_Peek((Derived *)base.Peek()); |
| 453 | + return RefCountPtr<Derived>::Create_AddRef((Derived *)base.Peek()); |
446 | 454 | }
|
447 | 455 |
|
448 | 456 | #endif
|
0 commit comments