Bromeon
Public Member Functions | Related Functions
thor::ScopedPtr< T > Class Template Reference

Noncopyable smart pointer that destroys objects going out of scope. More...

Inheritance diagram for thor::ScopedPtr< T >:
Inheritance graph

List of all members.

Public Member Functions

 ScopedPtr ()
 Default constructor.
 ScopedPtr (T *pointer)
 Constructor from raw pointer.
 ~ScopedPtr ()
 Destructor.
void Swap (ScopedPtr &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 (ScopedPtr< T > &lhs, ScopedPtr< T > &rhs)
 Swaps two ScopedPtr<T> instances.

Detailed Description

template<typename T>
class thor::ScopedPtr< T >

Noncopyable smart pointer that destroys objects going out of scope.

Template Parameters:
TThe pointee type

Copy constructor and assignment operators can't be called using this smart pointer. This implementation is useful for local RAII smart pointers that don't need to share or copy ownership and that prevent you from accidentally doing it.


Constructor & Destructor Documentation

template<typename T>
thor::ScopedPtr< T >::ScopedPtr ( )

Default constructor.

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

template<typename T>
thor::ScopedPtr< T >::ScopedPtr ( T *  pointer) [explicit]

Constructor from raw pointer.

Parameters:
pointerThe 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.
template<typename T>
thor::ScopedPtr< T >::~ScopedPtr ( )

Destructor.

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


Member Function Documentation

template<typename T>
T* thor::ScopedPtr< T >::Get ( ) const

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

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

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

Dereferences the pointer.

Precondition:
The smart pointer must be non-empty.
template<typename T>
T* thor::ScopedPtr< T >::operator-> ( ) const

Dereferences the pointer for member access.

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

template<typename T>
void thor::ScopedPtr< 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.

template<typename T>
void thor::ScopedPtr< T >::Reset ( T *  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. If the dynamic type of pointer is a class derived from T, then T must have a virtual destructor.
template<typename T>
void thor::ScopedPtr< T >::Swap ( ScopedPtr< 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.


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