Bromeon
Public Member Functions | Related Functions
thor::MovedPtr< T, OwnershipPolicy > Class Template Reference

Smart pointer with move semantics. More...

List of all members.

Public Member Functions

 MovedPtr ()
 Default constructor.
template<typename U >
 MovedPtr (U *pointer)
 Constructor from raw pointer.
 MovedPtr (MovedPtr &origin)
 Move constructor.
 ~MovedPtr ()
 Destructor.
MovedPtroperator= (MovedPtr &origin)
 Move assignment operator.
void Swap (MovedPtr &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>
MovedPtr< T, OwnershipPolicy > thor::Copy (const MovedPtr< T, OwnershipPolicy > &origin)
 Creates a MovedPtr from another MovedPtr by copying ownership.
template<typename T , template< typename > class OwnershipPolicy>
void thor::swap (MovedPtr< T, OwnershipPolicy > &lhs, MovedPtr< T, OwnershipPolicy > &rhs)
 Swaps two MovedPtr<T, OwnershipPolicy> instances.

Detailed Description

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
class thor::MovedPtr< T, OwnershipPolicy >

Smart pointer with move semantics.

Template Parameters:
TThe pointee type
OwnershipPolicyOnly necessary if you plan to transfer ownership to a CopiedPtr. See its declaration for more information.

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 several reasons:


Constructor & Destructor Documentation

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
thor::MovedPtr< T, OwnershipPolicy >::MovedPtr ( )

Default constructor.

Initializes the smart pointer with a null pointer. This operation doesn't require T to be defined.

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
template<typename U >
thor::MovedPtr< T, OwnershipPolicy >::MovedPtr ( U *  pointer) [explicit]

Constructor from raw pointer.

Parameters:
pointerThe initial pointer value, either pointing to an object allocated by the new operator or to NULL. In case you use this MovedPtr 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).
Template Parameters:
UType of the assigned object, where U* is implicitly convertible to T*.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
thor::MovedPtr< T, OwnershipPolicy >::MovedPtr ( MovedPtr< T, OwnershipPolicy > &  origin)

Move constructor.

Transfers ownership from origin to *this. After this operation, origin is empty.

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
thor::MovedPtr< T, OwnershipPolicy >::~MovedPtr ( )

Destructor.

Destroys the object and deallocates the corresponding memory. For null pointers, nothing happens.


Member Function Documentation

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
T* thor::MovedPtr< T, OwnershipPolicy >::Get ( ) const

Permits access to the internal pointer. Designed for rare use.

Returns:
Internally used pointer, use it wisely not to upset the MovedPtr's memory management.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
thor::MovedPtr< 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.

Returns:
bool-like value that behaves in conditional expressions like true, if the pointer is valid; and like false, if it points to NULL.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
T& thor::MovedPtr< T, OwnershipPolicy >::operator* ( ) const

Dereferences the pointer.

Precondition:
The smart pointer must be non-empty.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
T* thor::MovedPtr< T, OwnershipPolicy >::operator-> ( ) const

Dereferences the pointer for member access.

Precondition:
The smart pointer must be non-empty.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
MovedPtr& thor::MovedPtr< T, OwnershipPolicy >::operator= ( MovedPtr< T, OwnershipPolicy > &  origin)

Move assignment operator.

Transfers ownership from origin to *this. After this operation, origin is empty.

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
T* thor::MovedPtr< 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.

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
void thor::MovedPtr< 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.

template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
template<typename U >
void thor::MovedPtr< T, OwnershipPolicy >::Reset ( U *  pointer)

Resets the internal pointer to the passed value.

Destroys the current object, and re-assigns the pointer.

Parameters:
pointerThe initial pointer value, either pointing to an object allocated by the new operator or to NULL. In case you use this MovedPtr 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).
Template Parameters:
UType of the assigned object, where U* is implicitly convertible to T*.
template<typename T, template< typename > class OwnershipPolicy = DynamicCopy>
void thor::MovedPtr< T, OwnershipPolicy >::Swap ( MovedPtr< 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.


The documentation for this class was generated from the following file: