Store a guarded pointer, synchronizing other copies of this pointer. More...
#include <klfutil.h>
Public Types | |
typedef T * | Pointer |
Public Member Functions | |
KLFSyncGuardPtr () | |
~KLFSyncGuardPtr () | |
KLFSyncGuardPtr (const KLFSyncGuardPtr ©) | |
KLFSyncGuardPtr (T *obj) | |
Pointer | ptr () |
operator T * () | |
operator const T * () | |
T * | operator() () |
const T * | operator() () const |
T & | operator* () |
const T & | operator* () const |
T * | operator-> () |
const T * | operator-> () const |
Pointer | operator= (Pointer p) |
const KLFSyncGuardPtr & | operator= (const KLFSyncGuardPtr ©) |
void | invalidate () |
void | reset () |
Store a guarded pointer, synchronizing other copies of this pointer.
Stores a pointer to any object. Copies may be made of the KLFSyncGuardPtr<T> pointer and used normally. Once you destroy the pointed object using one of these pointers, then set that pointer to NULL and automatically all copies of that pointer will be set to NULL too.
Minimal example:
QString * a = new QString("string-A"); KLFSyncGuardPtr<QString> ptr3; { KLFSyncGuardPtr<QString> ptr1 = a; KLFSyncGuardPtr<QString> ptr2 = ptr1; ptr3 = ptr1; printf("Pointer 2 is now %p\n", (QString*)ptr2); printf("Pointer 2 = %s\n", qPrintable(*ptr2)); printf("Pointer 3 is now %p\n", (QString*)ptr3); printf("Pointer 3 = %s\n", qPrintable(*ptr3)); ptr1 = NULL; printf("Pointer 2 is now %p\n", (QString*)ptr2); } printf("Pointer 3 is now %p\n", (QString*)ptr3);
Note that if you assign another KLFSyncGuardPtr-guarded pointed object to a KLFSyncGuardPtr which is already tracking a pointer, then a warning is emitted. This is to avoid design flaws where in that case, the assignment will cause the current KLFSyncGuardPtr to become a copy of the new KLFSyncGuardPtr and all copies of the original pointer will become independant and unaffected. Example:
KLFSyncGuardPtr<Obj> ptr1 = myobject; KLFSyncGuardPtr<Obj> ptr2 = ptr1; // ptr2 is a copy of ptr1 KLFSyncGuardPtr<Obj> otherptr = otherobject; // another sync-guard-ptr object ptr1 = otherptr; // this will result in a warning. Indeed, ptr1 now tracks otherobject pointer and // leaves myobject. In particular, ptr2, which was a copy of ptr1, is not modified and still points // to myobject.
Use reset() to detach from other copies and start fresh again.
typedef T* KLFSyncGuardPtr< T >::Pointer |
KLFSyncGuardPtr< T >::KLFSyncGuardPtr | ( | ) | [inline] |
KLFSyncGuardPtr< T >::~KLFSyncGuardPtr | ( | ) | [inline] |
KLFSyncGuardPtr< T >::KLFSyncGuardPtr | ( | const KLFSyncGuardPtr< T > & | copy | ) | [inline] |
KLFSyncGuardPtr< T >::KLFSyncGuardPtr | ( | T * | obj | ) | [inline] |
void KLFSyncGuardPtr< T >::invalidate | ( | ) | [inline] |
Definition at line 909 of file klfutil.h.
References KLFPointerGuard::ptr.
Referenced by KLFSyncGuardPtr< T >::operator=().
KLFSyncGuardPtr< T >::operator const T * | ( | ) | [inline] |
Definition at line 867 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
KLFSyncGuardPtr< T >::operator T * | ( | ) | [inline] |
Definition at line 865 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
T* KLFSyncGuardPtr< T >::operator() | ( | ) | [inline] |
Definition at line 870 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
const T* KLFSyncGuardPtr< T >::operator() | ( | ) | const [inline] |
Definition at line 872 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
T& KLFSyncGuardPtr< T >::operator* | ( | ) | [inline] |
Definition at line 875 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
const T& KLFSyncGuardPtr< T >::operator* | ( | ) | const [inline] |
Definition at line 877 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
T* KLFSyncGuardPtr< T >::operator-> | ( | ) | [inline] |
Definition at line 880 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
const T* KLFSyncGuardPtr< T >::operator-> | ( | ) | const [inline] |
Definition at line 882 of file klfutil.h.
References KLFSyncGuardPtr< T >::ptr().
Pointer KLFSyncGuardPtr< T >::operator= | ( | Pointer | p | ) | [inline] |
Definition at line 886 of file klfutil.h.
References KLFSyncGuardPtr< T >::invalidate(), klfWarning, and KLFPointerGuard::ptr.
const KLFSyncGuardPtr& KLFSyncGuardPtr< T >::operator= | ( | const KLFSyncGuardPtr< T > & | copy | ) | [inline] |
Definition at line 901 of file klfutil.h.
References KLF_ASSERT_CONDITION, and KLFPointerGuard::ptr.
Pointer KLFSyncGuardPtr< T >::ptr | ( | ) | [inline] |
Definition at line 858 of file klfutil.h.
References KLFPointerGuard::ptr.
Referenced by KLFSyncGuardPtr< T >::operator const T *(), KLFSyncGuardPtr< T >::operator T *(), KLFSyncGuardPtr< T >::operator()(), KLFSyncGuardPtr< T >::operator*(), and KLFSyncGuardPtr< T >::operator->().
void KLFSyncGuardPtr< T >::reset | ( | ) | [inline] |
Definition at line 915 of file klfutil.h.
References KLFRefPtr< T >::setNull().