Generic smart pointer class that supports several deep copy ownership policies. More...
Public Member Functions | |
CopiedPtr () | |
Default constructor. | |
template<typename U > | |
CopiedPtr (U *pointer) | |
Constructor from raw pointer. | |
CopiedPtr (const CopiedPtr &origin) | |
Copy constructor. | |
~CopiedPtr () | |
Destructor. | |
CopiedPtr & | operator= (const CopiedPtr &origin) |
Copy assignment operator. | |
void | Swap (CopiedPtr &other) |
Swaps the pointers of *this and other. | |
void | Reset () |
Resets the internal pointer to NULL. | |
template<typename U > | |
void | Reset (U *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 , template< typename > class OwnershipPolicy> | |
void | thor::swap (CopiedPtr< T, OwnershipPolicy > &lhs, CopiedPtr< T, OwnershipPolicy > &rhs) |
Swaps two CopiedPtr<T, OwnershipPolicy> instances. |
Generic smart pointer class that supports several deep copy ownership policies.
T | The pointee type |
OwnershipPolicy | Template used for copy and destruction semantics. The policies StaticCopy, DynamicCopy and VirtualClone are shipped with the library, but you can also specify your own. A user-defined copy policy must be a class template with T as template type-parameter. It has to support the following public functions (by the way, they can be function templates themselves to recognize a pointer type different from T*, namely U* - the same as used in CopiedPtr(U*) and Reset(U*)).
|
thor::CopiedPtr< T, OwnershipPolicy >::CopiedPtr | ( | ) |
Default constructor.
Initializes the smart pointer with a null pointer. This operation doesn't require T to be defined.
thor::CopiedPtr< T, OwnershipPolicy >::CopiedPtr | ( | U * | pointer | ) | [explicit] |
Constructor from raw pointer.
pointer | The initial pointer value, either pointing to an object allocated by the new operator or to NULL. In case you use this CopiedPtr with the DynamicCopy policy, ensure that the static and dynamic type of the passed pointer are equal (therefore, don't pass base class pointers that actually refer to derived classes). |
U | Type of the assigned object, where U* is implicitly convertible to T*. |
thor::CopiedPtr< T, OwnershipPolicy >::CopiedPtr | ( | const CopiedPtr< T, OwnershipPolicy > & | origin | ) |
Copy constructor.
Deep copy: If origin is empty, this instance becomes empty as well. Otherwise, OwnershipPolicy::Copy() is invoked, passing the raw pointer of origin as argument. After this operation, both CopiedPtr instances are either empty, or hold two independent, equal objects.
thor::CopiedPtr< T, OwnershipPolicy >::~CopiedPtr | ( | ) |
Destructor.
Invokes OwnershipPolicy::Destroy() with the pointer as argument. For the three default policies, the behavior is equivalent to deleting the pointer.
T* thor::CopiedPtr< T, OwnershipPolicy >::Get | ( | ) | const |
Permits access to the internal pointer. Designed for rare use.
thor::CopiedPtr< T, OwnershipPolicy >::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::CopiedPtr< T, OwnershipPolicy >::operator* | ( | ) | const |
Dereferences the pointer.
T* thor::CopiedPtr< T, OwnershipPolicy >::operator-> | ( | ) | const |
Dereferences the pointer for member access.
CopiedPtr& thor::CopiedPtr< T, OwnershipPolicy >::operator= | ( | const CopiedPtr< T, OwnershipPolicy > & | origin | ) |
Copy assignment operator.
Deep copy: Implemented by copy and swap, calls the copy constructor. After this operation, both CopiedPtr instances are either empty, or hold an independent, equal object.
T* thor::CopiedPtr< T, OwnershipPolicy >::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::CopiedPtr< T, OwnershipPolicy >::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::CopiedPtr< T, OwnershipPolicy >::Reset | ( | U * | 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. In case you use this CopiedPtr with the DynamicCopy policy, ensure that the static and dynamic type of the pointer are equal (therefore, don't pass base class pointers that actually refer to derived classes). |
U | Type of the assigned object, where U* is implicitly convertible to T*. |
void thor::CopiedPtr< T, OwnershipPolicy >::Swap | ( | CopiedPtr< T, OwnershipPolicy > & | 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.