Smart pointer with move semantics. More...
Public Member Functions | |
MovedPtr () | |
Default constructor. | |
MovedPtr (T *pointer) | |
Constructor from raw pointer. | |
MovedPtr (MovedPtr &origin) | |
Move constructor. | |
~MovedPtr () | |
Destructor. | |
MovedPtr & | operator= (MovedPtr &origin) |
Move assignment operator. | |
void | Swap (MovedPtr &other) |
Swaps the pointers of *this and other. | |
void | Reset () |
Resets the internal pointer to NULL. | |
void | Reset (T *pointer) |
Resets the internal pointer to the passed value. | |
T * | Release () |
Transfers ownership of the held object to the caller. | |
operator SafeBool () const | |
Checks whether the pointer currently points to NULL. | |
T & | operator* () const |
Dereferences the pointer. | |
T * | operator-> () const |
Dereferences the pointer for member access. | |
T * | Get () const |
Permits access to the internal pointer. Designed for rare use. | |
Related Functions | |
template<typename T > | |
void | thor::swap (MovedPtr< T > &lhs, MovedPtr< T > &rhs) |
Swaps two MovedPtr<T> instances. |
Smart pointer with move semantics.
T | The pointee type |
This type of smart pointer allows movable objects - that is, copies and assignments transfer ownership. Because of that, these operations modify the origin, which can be counter-intuitive, as it doesn't behave like usual value semantics. Besides, every instance is unique, no two MovedPtr objects can point to the same object (but both can point to NULL).
Why another movable smart pointer, if there's already std::auto_ptr? There are some reasons:
thor::MovedPtr< T >::MovedPtr | ( | ) |
Default constructor.
Initializes the smart pointer with a null pointer. This operation doesn't require T to be defined.
thor::MovedPtr< T >::MovedPtr | ( | T * | pointer | ) | [explicit] |
Constructor from raw pointer.
pointer | The initial pointer value, either pointing to an object allocated by the new operator or to NULL. If the dynamic type of pointer is a class derived from T, then T must have a virtual destructor. |
thor::MovedPtr< T >::MovedPtr | ( | MovedPtr< T > & | origin | ) |
Move constructor.
Transfers ownership from origin to *this. After this operation, origin is empty.
thor::MovedPtr< T >::~MovedPtr | ( | ) |
Destructor.
Destroys the object and deallocates the corresponding memory. For null pointers, nothing happens.
T* thor::MovedPtr< T >::Get | ( | ) | const |
Permits access to the internal pointer. Designed for rare use.
thor::MovedPtr< T >::operator SafeBool | ( | ) | const |
Checks whether the pointer currently points to NULL.
This allows you to write statements of the form if(ptr) or if(!ptr) in a type-safe way (ptr refers to a smart-pointer instance). The actual return type is implementation-defined.
T& thor::MovedPtr< T >::operator* | ( | ) | const |
Dereferences the pointer.
T* thor::MovedPtr< T >::operator-> | ( | ) | const |
Dereferences the pointer for member access.
MovedPtr& thor::MovedPtr< T >::operator= | ( | MovedPtr< T > & | origin | ) |
Move assignment operator.
Transfers ownership from origin to *this. After this operation, origin is empty.
T* thor::MovedPtr< T >::Release | ( | ) |
Transfers ownership of the held object to the caller.
Upon invoking Release(), the caller becomes responsible for the memory management of the returned pointer. This instance points to null after the operation.
void thor::MovedPtr< T >::Reset | ( | ) |
Resets the internal pointer to NULL.
Destroys the current object, and sets the internal pointer to NULL. This operation doesn't require T to be defined.
void thor::MovedPtr< T >::Reset | ( | T * | pointer | ) |
Resets the internal pointer to the passed value.
Destroys the current object, and re-assigns the pointer.
pointer | The new pointer value, either pointing to an object allocated by the new operator or to NULL. If the dynamic type of pointer is a class derived from T, then T must have a virtual destructor. |
void thor::MovedPtr< T >::Swap | ( | MovedPtr< T > & | other | ) |
Swaps the pointers of *this and other.
Swap() doesn't operate on the object itself, only on the pointer. T may be incomplete at invocation.