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

Generic smart pointer class that supports several deep copy ownership policies. More...

List of all members.

Public Member Functions

 CopiedPtr ()
 Default constructor.
template<typename U >
 CopiedPtr (U *pointer)
 Constructor from raw pointer.
 CopiedPtr (const CopiedPtr &origin)
 Copy constructor.
 ~CopiedPtr ()
 Destructor.
CopiedPtroperator= (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.

Detailed Description

template<typename T, template< typename > class OwnershipPolicy>
class thor::CopiedPtr< T, OwnershipPolicy >

Generic smart pointer class that supports several deep copy ownership policies.

Template Parameters:
TThe pointee type
OwnershipPolicyTemplate 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*)).
  • static T* Copy(const T* originPtr);
    The function performing a deep copy. Note that the Copy() function is never passed a null pointer. In case an empty smart pointer (pointing to NULL) is copied, Copy() isn't invoked - i.e., both smart pointers are empty after the operation. It might be of advantage if your copy mechanism checks pre- and postconditions with assert.

  • static void Destroy(T* pointer);
    The function used to destroy and deallocate the held object. The three provided policies just use the delete operator.

Constructor & Destructor Documentation

template<typename T, template< typename > class OwnershipPolicy>
thor::CopiedPtr< T, OwnershipPolicy >::CopiedPtr ( )

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>
template<typename U >
thor::CopiedPtr< T, OwnershipPolicy >::CopiedPtr ( 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 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).
Template Parameters:
UType of the assigned object, where U* is implicitly convertible to T*.
template<typename T, template< typename > class OwnershipPolicy>
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.

template<typename T, template< typename > class OwnershipPolicy>
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.


Member Function Documentation

template<typename T, template< typename > class OwnershipPolicy>
T* thor::CopiedPtr< 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 CopiedPtr's memory management.
template<typename T, template< typename > class OwnershipPolicy>
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.

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>
T& thor::CopiedPtr< T, OwnershipPolicy >::operator* ( ) const

Dereferences the pointer.

Precondition:
The smart pointer must be non-empty.
template<typename T, template< typename > class OwnershipPolicy>
T* thor::CopiedPtr< 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>
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.

template<typename T, template< typename > class OwnershipPolicy>
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.

template<typename T, template< typename > class OwnershipPolicy>
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.

template<typename T, template< typename > class OwnershipPolicy>
template<typename U >
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.

Parameters:
pointerThe 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).
Template Parameters:
UType of the assigned object, where U* is implicitly convertible to T*.
template<typename T, template< typename > class OwnershipPolicy>
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.


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