#include <vpr/Util/RefCountMemory.h>
Public Types | |
| enum | States { BAD = -69 } |
Public Member Functions | |
| RefCountMemory () | |
| Default constructor. | |
| RefCountMemory (const RefCountMemory &r) | |
| On creation, refcount == 0, so if you intend to own the memory created, you should ref it. | |
| RefCountMemory & | operator= (const RefCountMemory &r) |
| const int & | ref () |
| Increases the reference count. | |
| bool | unrefDelete () |
| Decreases the reference count by 1, then call checkDelete. | |
| const int & | unref () |
| Decreases the reference count. | |
| const int & | getRef () const |
| Gets the reference count. | |
| const int & | unrefGetRef () |
| Decreases the reference count by 1, return the new refcount. | |
| virtual bool | checkDelete () |
| If refcount <= 0, then delete the memory. | |
Protected Member Functions | |
| virtual | ~RefCountMemory () |
| Destructor. | |
(No, not radio controlled, sorry.)
To use reference counting on your objects, inherit from this class.
NOTES:
EXAMPLE USAGE: Object obj; obj.ref(); // count == 1; obj.ref(); // count == 2; int count = obj.getRef(); obj.unrefDelete(); // count == 1, memory is not deleted obj.unrefDelete(); // count == 0, memory is deleted
class Object : public vpr::RefCountMemory
Definition at line 73 of file RefCountMemory.h.
| vpr::RefCountMemory::RefCountMemory | ( | ) | [inline] |
Default constructor.
On creation, refcount == 0, so if you intend to own the memory created, you should ref it
Definition at line 152 of file RefCountMemory.h.
| vpr::RefCountMemory::RefCountMemory | ( | const RefCountMemory & | r | ) | [inline] |
On creation, refcount == 0, so if you intend to own the memory created, you should ref it.
Definition at line 91 of file RefCountMemory.h.
| virtual vpr::RefCountMemory::~RefCountMemory | ( | ) | [inline, protected, virtual] |
Destructor.
You MUST call checkDelete to delete, do not call delete!!!!!
Definition at line 101 of file RefCountMemory.h.
References BAD.
00102 { 00103 assert( ___mNumTimesReferenced != BAD && ___mNumTimesReferenced >= 0 && "this data has been dereferenced more times than referenced, someone is probably holding on to the data after they called deref(), or someone called deref too many times. also, make sure you use checkDelete, or unrefDelete." ); 00104 ___mNumTimesReferenced = BAD; 00105 }
| RefCountMemory& vpr::RefCountMemory::operator= | ( | const RefCountMemory & | r | ) | [inline] |
| const int & vpr::RefCountMemory::ref | ( | ) | [inline] |
Increases the reference count.
Definition at line 164 of file RefCountMemory.h.
00165 { 00166 assert( ___mNumTimesReferenced != BAD && "this data has been deleted, someone is probably holding on to the data after they called deref()" ); 00167 ++___mNumTimesReferenced; 00168 return this->getRef(); 00169 }
| bool vpr::RefCountMemory::unrefDelete | ( | ) | [inline] |
Decreases the reference count by 1, then call checkDelete.
This is equivalent to calling RefCountMemory::unref followed by RefCountMemory::checkDelete
true if deleted; false if not. Definition at line 207 of file RefCountMemory.h.
References checkDelete(), and unref().
00208 { 00209 this->unref(); 00210 return this->checkDelete(); 00211 }
| const int & vpr::RefCountMemory::unref | ( | ) | [inline] |
Decreases the reference count.
Definition at line 172 of file RefCountMemory.h.
Referenced by unrefDelete(), and unrefGetRef().
00173 { 00174 assert( ___mNumTimesReferenced != BAD && ___mNumTimesReferenced >= 0 && "this data has been dereferenced more times than referenced, someone is probably holding on to the data after they called deref(), or someone called deref too many times. also, make sure you use checkDelete, or unrefDelete." ); 00175 --___mNumTimesReferenced; 00176 return this->getRef(); 00177 }
| const int & vpr::RefCountMemory::getRef | ( | ) | const [inline] |
Gets the reference count.
Definition at line 157 of file RefCountMemory.h.
References BAD.
Referenced by ref(), unref(), and unrefGetRef().
00158 { 00159 assert( ___mNumTimesReferenced != BAD && "this data has been deleted, someone is probably holding on to the data after they called deref()" ); 00160 return ___mNumTimesReferenced; 00161 }
| const int & vpr::RefCountMemory::unrefGetRef | ( | ) | [inline] |
| bool vpr::RefCountMemory::checkDelete | ( | ) | [inline, virtual] |
If refcount <= 0, then delete the memory.
If the object has any refcountable children, then they will have RefCountMemory::unrefDelete() called on them.
Definition at line 186 of file RefCountMemory.h.
References BAD.
Referenced by unrefDelete().
00187 { 00188 assert( ___mNumTimesReferenced != BAD && ___mNumTimesReferenced >= 0 && "this data has been dereferenced more times than referenced, someone is probably holding on to the data after they called deref(), or someone called deref too many times. also, make sure you use checkDelete, or unrefDelete." ); 00189 if (___mNumTimesReferenced <= 0) 00190 { 00191 delete this; 00192 return true; 00193 } 00194 else 00195 { 00196 return false; 00197 } 00198 }
1.5.1