Template safe pointer (reference-counted, auto-delete, thread-safe). More...
Public Types | |
typedef T | DataType |
Generic definition of the pointed type for this safe pointer. | |
typedef MUTEX | MutexType |
Generic definition of the mutex for this safe pointer. | |
Public Member Functions | |
SafePtr (const SafePtr< T, MUTEX > &sp) | |
Copy constructor. More... | |
SafePtr (SafePtr< T, MUTEX > &&sp) noexcept | |
Move constructor. More... | |
SafePtr (T *p=nullptr) | |
Default constructor using an optional unmanaged object. More... | |
~SafePtr () | |
Destructor. More... | |
template<typename NEWMUTEX > | |
SafePtr< T, NEWMUTEX > | changeMutex () |
Change mutex type. More... | |
void | clear () |
Clear this instance of the safe pointer. More... | |
int | count () const |
Get the reference count value. More... | |
template<typename ST , typename std::enable_if< std::is_base_of< T, ST >::value >::type * = nullptr> | |
SafePtr< ST, MUTEX > | downcast () |
Downcast operation. More... | |
bool | isNull () const |
Check if this safe pointer is a null pointer. More... | |
bool | operator!= (const SafePtr< T, MUTEX > &sp) const |
Unequality operator. More... | |
T & | operator* () const |
Accessor operator. More... | |
T * | operator-> () const |
Redirection operator. More... | |
SafePtr< T, MUTEX > & | operator= (const SafePtr< T, MUTEX > &sp) |
Assignment between safe pointers. More... | |
SafePtr< T, MUTEX > & | operator= (SafePtr< T, MUTEX > &&sp) noexcept |
Move assignment between safe pointers. More... | |
SafePtr< T, MUTEX > & | operator= (T *p) |
Assignment from a standard pointer T* . More... | |
bool | operator== (const SafePtr< T, MUTEX > &sp) const |
Equality operator. More... | |
T * | pointer () const |
Get a standard pointer T* on the pointed object. More... | |
T * | release () |
Release the pointed object from the safe pointer management. More... | |
void | reset (T *p=nullptr) |
Deallocate the previous pointed object and set the pointer to the new object. More... | |
template<typename ST , typename std::enable_if< std::is_base_of< ST, T >::value >::type * = nullptr> | |
SafePtr< ST, MUTEX > | upcast () |
Upcast operation. More... | |
Template safe pointer (reference-counted, auto-delete, thread-safe).
The ts::SafePtr template class is an implementation of the safe pointer design pattern. A safe pointer implements the semantics of a standard pointer with automatic memory management.
Safe pointer objects pointing to the same object can be assigned like any standard pointer (elementary type). A reference counter on the pointed object is maintained and the pointed object is automatically deleted when no more safe pointer references the object.
Limitation: The automatic deletion of the pointed object occurs only when the reference counter reaches zero. There are cases where this never happens. Typically, when two objects reference each other but are no longer referenced anywhere else, these two objects are lost forever and will never be deleted. So, beware that smart pointers do not prevent from memory leaks in some pathological cases. As usual, be sure to design safely.
Limitation: Because the automatic deletion is performed using the operator delete
, safe pointers cannot point on array types since arrays must be deleted using the operator delete
[].
The standard operators for elementary type pointers also exist for safe pointers (assignment, comparison, dereferencing, etc.)
A safe pointer can be null, this is the default value. In this case, the safe pointer does not reference any object. To test if a safe pointer is a null pointer, use the method isNull()
. Do not use comparisons such as p == nullptr
, the result will be incorrect.
The ts::SafePtr template class can be made thread-safe using a mutex. The type of mutex to use is given by the template parameter MUTEX which must be a subclass of ts::MutexInterface. By default, ts::NullMutex is used. The default implementation is consequently not thread-safe but there is no synchronization overhead. To use safe pointers in a multi-thread environment, specify an actual mutex implementation for the target environment.
T | The type of the pointed object. Cannot be an array type. |
MUTEX | A subclass of ts::MutexInterface which is used to synchronize access to the safe pointer internal state. |
|
inline |
Default constructor using an optional unmanaged object.
The optional argument p can be either nullptr
or the address of an unmanaged dynamically allocated object (i.e. which has been allocated using the operator new
). In this case, unmanaged means that the object must not be already controlled by another set of safe pointers.
This constructor is typically used in combination with the operator new
to allocate an object which is immediatly managed by safe pointers.
Example:
[in] | p | A pointer to an object of class T. The default value is nullptr . In this case, the safe pointer is a null pointer. |
std::bad_alloc | Thrown if insufficient memory is available for internal safe pointer management. |
|
inline |
Copy constructor.
This object references the same T object as sp. If sp is a null pointer, this object is also a null pointer.
[in] | sp | Another safe pointer instance. |
|
inlinenoexcept |
Move constructor.
[in,out] | sp | Another safe pointer instance. |
ts::SafePtr< T, MUTEX >::~SafePtr | ( | ) |
Destructor.
If this object is not a null pointer, the reference counter is decremented. When the reference counter reaches zero, the pointed object is automatically deleted.
SafePtr<T,MUTEX>& ts::SafePtr< T, MUTEX >::operator= | ( | const SafePtr< T, MUTEX > & | sp | ) |
Assignment between safe pointers.
After the assignment, this object references the same T object as sp. If this object was previously not the null pointer, the reference counter of the previously referenced T object is decremented. If the reference counter reaches zero, the previously pointed object is automatically deleted.
[in] | sp | The value to assign. |
|
noexcept |
Move assignment between safe pointers.
[in,out] | sp | The value to assign. |
SafePtr<T,MUTEX>& ts::SafePtr< T, MUTEX >::operator= | ( | T * | p | ) |
Assignment from a standard pointer T*
.
The pointed T
object becomes managed by this safe pointer.
The standard pointer p can be either nullptr
or the address of an unmanaged dynamically allocated object (i.e. which has been allocated using the operator new
). In this case, unmanaged means that the object must not be already controlled by another set of safe pointers.
After the assignment, this object references the T object. If this safe pointer object was previously not the null pointer, the reference counter of the previously referenced T object is decremented. If the reference counter reaches zero, the previously pointed object is automatically deleted.
[in] | p | A pointer to an object of class T. |
std::bad_alloc | Thrown if insufficient memory is available for internal safe pointer management. |
|
inline |
Equality operator.
Check if this safe pointer and the sp safe pointer point to same object.
Caveat: Null pointers are not reliably compared with this operator. It shall not be used to compare against null pointer. Do not check == nullptr
, use the method isNull()
instead. Also, if both safe pointers are null pointers, the result is unpredictable, it can be true or false.
[in] | sp | A safe pointer to compare with. |
|
inline |
Unequality operator.
Check if this safe pointer and the sp safe pointer point to different objects.
Caveat: Null pointers are not reliably compared with this operator. It shall not be used to compare against null pointer. Do not check != nullptr
, use the method isNull()
instead. Also, if both safe pointers are null pointers, the result is unpredictable, it can be true or false.
[in] | sp | A safe pointer to compare with. |
|
inline |
Redirection operator.
With this operator, a safe pointer can be used in the same way as a standard pointer.
Example:
If this object is the null pointer, the operator returns zero and the further dereferencing operation will likely throw an exception.
T*
to the pointed object or nullptr
if this object is the null pointer.
|
inline |
Accessor operator.
With this operator, a safe pointer can be used in the same way as a standard pointer.
Example:
If this object is the null pointer, the operator will likely throw an exception.
T&
to the pointed object.
|
inline |
Release the pointed object from the safe pointer management.
The previously pointed object is not deallocated, its address is returned. All safe pointers which pointed to the object now point to nullptr
.
Caveat: The previously pointed object will no longer be automatically deleted. The caller must explicitly delete it later using the returned pointer value.
T*
to the previously pointed object. Return nullptr
if this object was the null pointer.
|
inline |
Deallocate the previous pointed object and set the pointer to the new object.
All safe pointers which pointed to the same object now point to the new one. The previously pointed object is deleted using the operator delete
.
The standard pointer p can be either nullptr
or the address of an unmanaged dynamically allocated object (i.e. which has been allocated using the operator new
). In this case, unmanaged means that the object must not be already controlled by another set of safe pointers.
[in] | p | A pointer to an object of class T. |
|
inline |
Clear this instance of the safe pointer.
The referenced object is deallocated if no more reference exists. Then, this safe pointer becomes the null pointer. sp.clear()
is equivalent to sp = (T*)(nullptr)
.
std::bad_alloc | Thrown if insufficient memory is available for internal safe pointer management. |
|
inline |
Check if this safe pointer is a null pointer.
|
inline |
Upcast operation.
This method converts a safe pointer to an object of class T into a safe pointer to an object of a super-class ST of T.
If this object is not the null pointer, the ownership of the pointed object is transfered to a new safe pointer to ST. This new safe pointer is returned. This object (the safe pointer to T) and all other safe pointers to T which pointed to the same object become null pointers.
ST | A super-class of T (immediate or indirect). |
|
inline |
Downcast operation.
This method converts a safe pointer to an object of class T into a safe pointer to an object of a subclass ST of T. This operation is equivalent to a dynamic_cast operator on regular pointers.
If this object is not the null pointer and points to an instance of ST, the ownership of the pointed object is transfered to a new safe pointer to ST. This new safe pointer is returned. This object (the safe pointer to T) and all other safe pointers to T which pointed to the same object become null pointers.
If this object is the null pointer or points to a T object which is not an instance of ST, the returned value is the null pointer and this object is unmodified.
ST | A subclass of T (immediate or indirect). |
|
inline |
Change mutex type.
This method converts a safe pointer to an object of class T into a safe pointer to the same class T with a different class of mutex.
If this object is not the null pointer, the ownership of the pointed object is transfered to a new safe pointer. This new safe pointer is returned. This object and all other safe pointers of the same class which pointed to the same object become null pointers.
NEWMUTEX | Another subclass of ts::MutexInterface which is used to synchronize access to the new safe pointer internal state. |
|
inline |
Get a standard pointer T*
on the pointed object.
Warning: This is an unchecked operation. Do not store this pointer to dereference it later since the pointed object may have been deleted in the meantime if no more safe pointer reference the object.
T*
to the pointed object or nullptr
if this object is the null pointer.
|
inline |
Get the reference count value.
This is informational only. In multi-threaded environments, the actual reference count may change before the result is actually used.