Public Types | List of all members
blaze::UniquePtr< T, D > Class Template Reference

Scope-limited management of dynamically allocated resourses.The UniquePtr class implements a scope-restricted, lightweight smart pointer that manages a dynamically allocated resource. In contrast to other smart pointer implementations, UniquePtr is non-copyable and therefore restricted to manage resources within a single scope, but does so with a minimum of runtime overhead. The following example demonstrates the application of UniquePtr: More...

#include <UniquePtr.h>

Inherits blaze::NonCopyable.

Public Types

typedef RemoveReference< T >::Type * Pointer
 Pointer type of the managed resource.
 
typedef RemoveReference< T >::Type & Reference
 Reference type of the managed resource.
 
typedef D Deleter
 Type of the resource deleter.
 

Public Member Functions

Constructors
 UniquePtr (Pointer ptr=NULL)
 The default constructor for UniquePtr. More...
 
Destructor
 ~UniquePtr ()
 The destructor for UniquePtr.
 
Access operators
Reference operator* () const
 Direct access to the managed resource. More...
 
Pointer operator-> () const
 Direct access to the managed resource. More...
 
Utility functions
Pointer get () const
 Returns a pointer to the managed resource. More...
 
Pointer release ()
 Releases the ownership of the managed resource to the caller. More...
 
void reset (Pointer ptr=NULL)
 Resets the unique pointer and replaces the managed resource with the given resource. More...
 
void swap (UniquePtr &up)
 Swapping the contents of two unique pointers. More...
 
Conversion operator
 operator bool () const
 Returns whether the unique pointer is set to a non-NULL pointer. More...
 

Private Attributes

Member variables
Pointer ptr_
 Pointer to the managed resource.
 
Deleter deleter_
 Resource deleter.
 

Detailed Description

template<typename T, typename D = PtrDelete>
class blaze::UniquePtr< T, D >

Scope-limited management of dynamically allocated resourses.

The UniquePtr class implements a scope-restricted, lightweight smart pointer that manages a dynamically allocated resource. In contrast to other smart pointer implementations, UniquePtr is non-copyable and therefore restricted to manage resources within a single scope, but does so with a minimum of runtime overhead. The following example demonstrates the application of UniquePtr:

{
blaze::UniquePtr<std::string> mystring( new std::string( "My string" ) );
// ... Working with the string
} // The string is automatically destroyed at the end of scope according to the RAII principle

Note that UniquePtr's interface is optimized for single objects created via the new operator and that by default DefaultDelete is used. For the management of dynamically allocated arrays, UniqueArray can be used:

{
blaze::UniqueArray<int> mystring( new int[10] );
// ... Working with the integer array
} // The array is automatically destroyed at the end of scope according to the RAII principle

Note that the template argument T must not have any array extent (similar to the unique_ptr implementation of the C++11 standard, where the array extent differentiates between single objects and arrays). Providing a type with array extent will result in a compile time error!

Constructor & Destructor Documentation

template<typename T , typename D >
blaze::UniquePtr< T, D >::UniquePtr ( Pointer  ptr = NULL)
inline

The default constructor for UniquePtr.

Parameters
ptrThe resource to be managed by the unique pointer

Member Function Documentation

template<typename T , typename D >
UniquePtr< T, D >::Pointer blaze::UniquePtr< T, D >::get ( ) const
inline

Returns a pointer to the managed resource.

Returns
Pointer to the managed resource or NULL if no resource is managed.

This function returns a pointer to the managed resource (or NULL in case no resource is currently managed). Note however that the ownership remains with the unqiue pointer.

template<typename T , typename D >
blaze::UniquePtr< T, D >::operator bool ( ) const
inline

Returns whether the unique pointer is set to a non-NULL pointer.

Returns
true in case the unique pointer is not NULL, false if it is NULL.
template<typename T , typename D >
UniquePtr< T, D >::Reference blaze::UniquePtr< T, D >::operator* ( ) const
inline

Direct access to the managed resource.

Returns
Reference to the managed resource.
template<typename T , typename D >
UniquePtr< T, D >::Pointer blaze::UniquePtr< T, D >::operator-> ( ) const
inline

Direct access to the managed resource.

Returns
Pointer to the managed resource.
template<typename T , typename D >
UniquePtr< T, D >::Pointer blaze::UniquePtr< T, D >::release ( )
inline

Releases the ownership of the managed resource to the caller.

Returns
Pointer to the managed resource or NULL if no resource is managed.

This function returns a pointer to the managed resource (or NULL in case no resource is currently managed). The ownership of the resource is released and passed to the caller.

template<typename T , typename D >
void blaze::UniquePtr< T, D >::reset ( Pointer  ptr = NULL)
inline

Resets the unique pointer and replaces the managed resource with the given resource.

Parameters
ptrThe new resource to be managed by the unique pointer.
Returns
void
template<typename T , typename D >
void blaze::UniquePtr< T, D >::swap ( UniquePtr< T, D > &  ptr)
inline

Swapping the contents of two unique pointers.

Parameters
ptrThe unique pointer to be swapped.
Returns
void
Exceptions
no-throwguarantee.

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