Stores a pointer to an object with refcount. More...
#include <klfutil.h>
Public Types | |
typedef T * | Pointer |
Public Member Functions | |
KLFRefPtr () | |
KLFRefPtr (const KLFRefPtr ©) | |
~KLFRefPtr () | |
T * | ptr () |
const T * | ptr () const |
bool | autoDelete () const |
void | setAutoDelete (bool on) |
void | setPointer (Pointer newptr) |
void | setNull () |
KLFRefPtr< T > & | operator= (const KLFRefPtr< T > &other) |
template<class OtherPtr > | |
KLFRefPtr< T > & | operator= (OtherPtr aptr) |
KLFRefPtr< T > & | operator= (Pointer newptr) |
operator T * () | |
operator const T * () const | |
T * | operator() () |
const T * | operator() () const |
Pointer | operator-> () |
Pointer | operator-> () const |
template<class OtherPtr > | |
OtherPtr | dyn_cast () |
template<class OtherPtr > | |
const OtherPtr | dyn_cast () const |
bool | operator== (void *otherptr) const |
bool | operator== (const KLFRefPtr< T > &otherptr) const |
bool | operator!= (void *otherptr) const |
bool | operator!= (const KLFRefPtr< T > &otherptr) const |
Stores a pointer to an object with refcount.
This class provides a normal datatype (with default constructor, copy constructor, assignment, ...) that stores a pointer to a structure that provides the functions ref() and deref() to track reference counts.
When this object is copied, or a pointer is assigned, then the pointed object's ref() function is called. When another pointer is assigned, or when this object is destroyed, then the function deref() is called on the previously pointed object, and the object is possibly delete'd
if needed and required.
Automatic object deletion upon zero refcount is optional, see autoDelete() and setAutoDelete(). It is on by default.
When constructed with the default constructor, pointers are initialized to NULL.
The copy constructor and the assignment operator also preserves autodelete setting, eg.
KLFRefPtr<Obj> ptr1 = ...; ptr1.setAutoDelete(false); KLFRefPtr<Obj> ptr2(ptr1); // ptr2.autoDelete() == false
The pointed/referenced object must:
int
, is strictly positive as long as the object is still referenced.Example:
class MyObj { QString name; int refcount; public: MyObj(const QString& s) : name(s), refcount(0) { } int ref() { return ++refcount; } int deref() { return --refcount; } ... QString objname() const { return name; } }; int main() { KLFRefPtr<MyObj> ptr; KLFRefPtr<MyObj> ptr2; ptr = new MyObj("Alice"); ptr2 = ptr; // this will increase "Alice"'s refcount by one ptr = new MyObj("Bob"); // "Alice" is still pointed by ptr2, so it is not yet deleted. ptr2 = NULL; // now "Alice" is deleted (refcount reached zero) ptr = obj_random_name(); ptr2 = obj_random_name(); do_something(ptr): do_something_2(ptr2); // ptr->field works as expected: printf("ptr points on object name=%s\n", qPrintable(ptr->objname())); } KLFRefPtr<MyObj> obj_random_name() { static QStringList names = QStringList()<<"Marty"<<"Jane"<<"Don"<<"John"<<"Phoebe"<<"Matthew"<<"Melissa"<<"Jessica" KLFRefPtr<MyObj> o = new MyObj(names[rand()%names.size()]); // the MyObj instance survives the `return' statement because of refcount increase in copy constructor return o; } void do_something(MyObj *object) { ... } void do_something_2(KLFRefPtr<MyObj> ptr) { ... }
bool KLFRefPtr< T >::autoDelete | ( | ) | const [inline] |
Definition at line 717 of file klfutil.h.
Referenced by KLFPosSearchable::Pos::data().
KLFRefPtr< T >::operator const T * | ( | ) | const [inline] |
KLFRefPtr< T >::operator T * | ( | ) | [inline] |
bool KLFRefPtr< T >::operator!= | ( | void * | otherptr | ) | const [inline] |
T* KLFRefPtr< T >::operator() | ( | ) | [inline] |
const T* KLFRefPtr< T >::operator() | ( | ) | const [inline] |
bool KLFRefPtr< T >::operator== | ( | void * | otherptr | ) | const [inline] |
Definition at line 722 of file klfutil.h.
Referenced by KLFRefPtr< KLFPointerGuard >::operator!=().
void KLFRefPtr< T >::setAutoDelete | ( | bool | on | ) | [inline] |
Definition at line 651 of file klfutil.h.
Referenced by KLFSyncGuardPtr< T >::reset().
void KLFRefPtr< T >::setPointer | ( | Pointer | newptr | ) | [inline] |
Definition at line 642 of file klfutil.h.
Referenced by KLFRefPtr< KLFPointerGuard >::operator=(), and KLFRefPtr< KLFPointerGuard >::setNull().