A reference counted smart pointer that supports alternate models of ownership.
More...
template<class T>
class regina::SafePtr< T >
A reference counted smart pointer that supports alternate models of ownership.
Specifically, there are two models of ownership for the pointee (of type T):
- The pointee may be owned by the smart pointer(s), in which case it will be destroyed when the last smart pointer to it is destroyed.
- Alternatively, the pointee may be owned by some other C++ object not using this smart pointer class. In this case, even when the last smart point to it is destroyed, the pointee itself will not be destroyed.
The pointee can indicate at runtime which model of ownership is in effect, through the return value of the function T::hasOwner().
The requirements for the pointee type T are as follows:
- T must subclass from
SafePointeeBase<T>
.
- T must implement a member function
bool hasOwner()
, which returns true
if and only if some other C++ object (which is not a SafePtr) owns the pointee.
Destruction works as follows:
- If a pointee's destructor is called, all SafePtr pointers that point to it will become expired. These SafePtr pointers are still safe to use, but get() will return
null
.
- If a pointee has one or more SafePtr pointers to it, then when the last of these safe pointers is destroyed, the pointee itself will be destroyed if and only if the pointee's hasOwner() returns
false
.
Under the hood, SafePtr uses SafeRemnant to achieve this.
- Author
- Matthias Goerner