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 pointer 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:
- An object will stay alive as long as one SafePtr is pointing to it.
- It is never safe to call
delete
on a raw pointer to an object of type T, unless you know (e.g., from human analysis of the program logic) that no SafePtr points to that same object.
- Subclasses of SafePointeeBase may provide their own destruction routines for users to call instead of
delete
, which are always safe to call regardless of whether there are SafePtr or not. For example, users should always delete packets by calling Packet::safeDelete(), not delete
.
- Destroying a SafePtr will destroy the underlying object of type T if and only if: (i) no other SafePtr is pointing to that object, and (ii) the pointee's hasOwner() returns
false
.
- Author
- Matthias Goerner