#include <lkptr.h>
Public Types | |
| typedef X | element_type |
| Standard name for the pointed object. | |
| typedef X | value_type |
| Standard name for the pointed object. | |
Public Member Functions | |
| lkptr (X *p=0) throw () | |
| Default constructor and constructor from a regular pointer. | |
| ~lkptr () | |
| Destructor. Delete only if last reference. | |
| lkptr (const lkptr &r) throw () | |
| Copy constructor: share the object with other pointers. | |
| lkptr & | operator= (const lkptr &r) |
| Copy operator: share the object with other pointers. | |
| void | swap (lkptr &r) throw () |
Swaps with r. | |
| X * | get () const throw () |
| Returns a pointer to referenced object. | |
| X & | value () const throw () |
| Returns the referenced object. | |
| X & | operator* () const throw () |
| Returns the referenced object. | |
| X * | operator-> () const throw () |
| Returns a pointer to referenced object. | |
| operator X * () const throw () | |
| Returns a pointer to referenced object. | |
| bool | isNull () const throw () |
Returns true if the object pointed is null and false otherwise. | |
| bool | unique () const throw () |
Return "true" when only one pointer references the object. | |
| int | use_count () const throw () |
Returns how many pointers are sharing the object referenced by "this". | |
| void | release () |
| Releases the object. | |
| void | weak_release () |
| Releases the object. | |
| void | reset (X *p=0) |
Resets the pointer so that it will point to "p". | |
| bool | ok () const throw () |
Verifies the invariant for class lkptr<X>. | |
Private Member Functions | |
| void | acquire (lkptr *r) throw () |
Makes this->m_ptr point to r.m_ptr. | |
| void | fast_release () |
| Releases the object. | |
Private Attributes | |
| X * | m_ptr |
Pointer to referenced object (can be a NULL pointer). | |
| lkptr * | m_prev |
| Next in pointer chain. | |
| lkptr * | m_next |
| Previous in pointer chain. | |
Friends | |
| class | test_lkptr |
Test lkptr<X>. | |
| template<class Y> | |
| bool | check_ok (const lkptr< Y > &p) throw () |
Verifies the invariant for class lkptr<Y>. | |
Only after the last pointer releases its reference will the object be deleted.
lkptr, but does not allocate anything on the free store.lkptr is implemented using 3 internal pointers which makes it 3 times as big as a regular pointer. In exchange, automatic garbage collection is provided by the C++ constructor - destructor protocol of execution.ok()."throw()" especification in the method signature).const.
void foo() { lkptr<AnyClass> p(new AnyClass); // "p" is one smart-pointer lkptr<AnyClass> q; // "q" is the other smart-pointer p->DoSomething(); // no syntax change when using pointers q = p; // share the object q->Change(); // both "*p" && "*q" get changed p = q; // both still point to the same object lkptr<AnyClass> r = p; // The 3 of them share the same object // delete p; // No need to worry about destruction // delete q; // the lkptr<> destructor will handle deletions for you. // delete r; }
Definition at line 102 of file lkptr.h.
| typedef X lkptr< X >::element_type |
| typedef X lkptr< X >::value_type |
| X* lkptr< X >::get | ( | ) | const throw () [inline] |
| X& lkptr< X >::value | ( | ) | const throw () [inline] |
| X& lkptr< X >::operator* | ( | ) | const throw () [inline] |
| X* lkptr< X >::operator-> | ( | ) | const throw () [inline] |
| lkptr< X >::operator X * | ( | ) | const throw () [inline] |
| bool lkptr< X >::isNull | ( | ) | const throw () [inline] |
| bool lkptr< X >::unique | ( | ) | const throw () [inline] |
| int lkptr< X >::use_count | ( | ) | const throw () [inline] |
| void lkptr< X >::release | ( | ) | [inline] |
| void lkptr< X >::weak_release | ( | ) | [inline] |
| void lkptr< X >::reset | ( | X * | p = 0 |
) | [inline] |
| bool lkptr< X >::ok | ( | ) | const throw () [inline] |
Verifies the invariant for class lkptr<X>.
"true" when "p" contains a correct value."true" even when it's value is corrupted."p" is corrupted.
"m_ptr" is the pointer to the referenced object.lkptr<X> occupies. <=================================> <=============>
| p1 p2 p3 | | p4 |
| +-+-+ +-+-+ +-+-+ | | +-+-+ |
<===>|*|*|<===>|*|*|<===>|*|*|<===> <===>|*|*|<===>
+-+-+ +-+-+ +-+-+ +-+-+
| * | | * | | * | | * |
+-|-+ +-|-+ +-|-+ +-|-+
| | | |
\|/ \|/ \|/ \|/
+----------------------+ +----------------------+
| Shared object | | Lonely Object |
+----------------------+ +----------------------+
+--------+-------+ "m_ptr" points to the object | m_prev | | +--------+ m_ptr + | m_next | | "m_next" is next in chain +--------+-------+ "m_prev" is previous in chain
m_next && m_prev pointing to itself (this).
Makes this->m_ptr point to r.m_ptr.
"this" after "r" in the double chained pointer list.fast_release() will restore the invariant into "this".NULL pointer. | void lkptr< X >::fast_release | ( | ) | [inline, private] |
friend class test_lkptr [friend] |
1.5.6