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

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

#include <UniqueArray.h>

Inherits blaze::NonCopyable.

Public Types

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

Public Member Functions

Constructors
 UniqueArray (Pointer ptr=NULL)
 The default constructor for the UniqueArray specialization. More...
 
Destructor
 ~UniqueArray ()
 The destructor for the UniqueArray specialization.
 
Access operators
Reference operator[] (size_t index) const
 Direct access to the array elements. More...
 
Utility functions
Pointer get () const
 Returns a pointer to the managed array. More...
 
Pointer release ()
 Releases the ownership of the managed array to the caller. More...
 
void reset (Pointer ptr=NULL)
 Resets the unique array and replaces the managed array with the given array. More...
 
void swap (UniqueArray &up)
 Swapping the contents of two unique arrays. 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 array.
 
Deleter deleter_
 Resource deleter.
 

Detailed Description

template<typename T, typename D = ArrayDelete>
class blaze::UniqueArray< T, D >

Scope-limited management of dynamically allocated arrays.

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

{
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 UniqueArray's interface is optimized for arrays and that by default ArrayDelete is used. For the management of single objects, UniquePtr can be used:

{
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 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::UniqueArray< T, D >::UniqueArray ( Pointer  ptr = NULL)
inline

The default constructor for the UniqueArray specialization.

Parameters
ptrThe array to be managed by the unique array

Member Function Documentation

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

Returns a pointer to the managed array.

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

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

template<typename T , typename D >
blaze::UniqueArray< 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 >
UniqueArray< T, D >::Reference blaze::UniqueArray< T, D >::operator[] ( size_t  index) const
inline

Direct access to the array elements.

Returns
Reference to the managed array.
template<typename T , typename D >
UniqueArray< T, D >::Pointer blaze::UniqueArray< T, D >::release ( )
inline

Releases the ownership of the managed array to the caller.

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

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

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

Resets the unique array and replaces the managed array with the given array.

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

Swapping the contents of two unique arrays.

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

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