Public Types |
typedef X | element_type |
| Standard name for the pointed object.
|
typedef X | value_type |
| Standard name for the pointed object.
|
typedef X * | pointer |
| Standard name for the pointer to the object.
|
Public Member Functions |
| lkptr (X *p=0) throw () |
template<typename Y > |
| lkptr (Y *p) throw () |
| lkptr (const lkptr &r) throw () |
template<typename Y > |
| lkptr (const lkptr< Y > &r) throw () |
| ~lkptr () |
| Destructor.
|
lkptr & | operator= (const lkptr &r) |
| Copy operator: share the object with other pointers.
|
template<typename Y > |
lkptr & | operator= (const lkptr< Y > &r) |
| Copy operator from a derived clase: share the object with other pointers.
|
void | swap (lkptr &r) throw () |
| Swaps with r .
|
X * | get () const throw () |
X & | value () const throw () |
X & | operator* () const throw () |
X * | operator-> () const throw () |
bool | isNull () const throw () |
bool | unique () const throw () |
int | use_count () const throw () |
void | release () |
| Releases the object.
|
void | reset (X *p=0) |
| Resets the pointer so that it will point to "p" .
|
void | reset (const lkptr &r) |
| (*this) = r .
|
template<typename Y > |
void | reset (const lkptr< Y > &r) |
| (*this) = r [ from a derived class ].
|
bool | ok () const throw () |
Private Attributes |
base_lkptr< X > | b |
| Contains the 3 pointers for the lkptr<> .
|
Friends |
class | lkptr |
class | test_lkptr |
| Test lkptr<X> .
|
template<typename Y > |
bool | check_ok (const lkptr< Y > &p) throw () |
template<typename X>
class lkptr< X >
These smart pointer share the object they point to.
Only after the last pointer releases its reference will the object be deleted.
- Whenever the object will be changed by one of the pointers, all the other pointers will also see the change.
- The implementation stores three pointers for every
lkptr
, but does not allocate anything on the free store.
- Every
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.
- These pointers are double linked together. When only one pointer is in the chain, then the object will be deleted if the pointer releases it. Otherwise, the object will not be deleted because the other pointers in the chain are still referencing it.
- These pointers are not intrusive, because they do not need an extra field in the pointed object to keep track of how many pointers reference the same object.
- These pointers do not allocate extra memory to keep track of the reference count, because the linked list itself binds together all pointers that reference the same object.
- A diagram for the pointer chain is shown in the documentation for method
ok()
.
- Many of the pointer operators will never throw exceptions (hence the
"throw()"
especification in the method signature).
- Memory leaks are extremely rare. Reference counting/linking can leak in the case of circular references (i.e., when the pointed object itself contains a counted/linked pointer, which points to an object that contains the original counted/linked pointer).
- These pointers cannot be used in ROM memory because they modify each other when double linking, even if they are
const
.
- Usage:
- This work is based on Sharon's paper:
Definition at line 485 of file lkptr.h.