Class array_pointer

Synopsis

#include "breeze/memory/array_pointer.hpp"

template< typename T >
class array_pointer

Description

A smart pointer to an array object.

A smart pointer that retains sole ownership of an array through a pointer. An array_pointer< T > is similar to a unique_ptr< T[], std::default_delete< T[] > > but this template was written way before C++11.

Like for unique_ptr, instances of this template satisfy the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.

Methods

array_pointer overloadDeleted copy constructor.
array_pointer overloadConstructs an array_pointer that doesn't own any array.
array_pointer overloadSets the internal pointer to p, taking ownership of the pointed to array if p is non-null (in which case, the array must have been allocated with new [].
array_pointer overloadMove constructs from another array_pointer.
~array_pointerExecutes delete [] raw_pointer().
operator= overloadDeleted copy assignment operator.
operator= overloadMove assigns from another array_pointer.
raw_pointerReturns: A pointer to the owned array.
resetIf the argument equals raw_pointer(), does nothing; otherwise, executes delete [] raw_pointer() and sets the internal pointer to the argument.

Source

Lines 35-95 in breeze/memory/array_pointer.hpp.

template< typename T >
class array_pointer
{
public:
    //!     Deleted copy constructor.
    // -----------------------------------------------------------------------
                        array_pointer( array_pointer const & ) = delete ;

    //!     Deleted copy assignment operator.
    // -----------------------------------------------------------------------
    array_pointer &     operator =( array_pointer const & ) = delete ;

    //!     Constructs an \c array_pointer that doesn't own any array.
    //!
    //!     \post
    //!         raw_pointer() == nullptr
    // -----------------------------------------------------------------------
                        array_pointer() noexcept ;

    //!     Sets the internal pointer to \c p, taking ownership of the
    //!     pointed to array if \c p is non-null (in which case, the
    //!     array must have been allocated with <code>new []</code>.
    // -----------------------------------------------------------------------
    explicit            array_pointer( T * p ) noexcept ;

    //!     Move constructs from another \c array_pointer.
    // -----------------------------------------------------------------------
                        array_pointer( array_pointer && ) noexcept ;

    //!     Executes <code>delete [] raw_pointer()</code>.
    //!
    //!     Requires for \c T to be a complete type at the point of
    //!     instantiation.
    // ----------------------------------------------------------------------.
                        ~array_pointer() noexcept ;

    //!     Move assigns from another \c array_pointer.
    // -----------------------------------------------------------------------
    array_pointer &     operator =( array_pointer && ) noexcept ;

    //!     \return
    //!         A pointer to the owned array.
    // -----------------------------------------------------------------------
    T *                 raw_pointer() const noexcept ;

    //!     If the argument equals \c raw_pointer(), does nothing;
    //!     otherwise, executes <code>delete [] raw_pointer()</code> and
    //!     sets the internal pointer to the argument.
    //!
    //!     Requires for \c T to be a complete type at the point of
    //!     instantiation.
    //!
    //!     \post
    //!         raw_pointer() == p
    // -----------------------------------------------------------------------
    void                reset( T * p ) noexcept ;

private:
    T *                 m_ptr ;
    void                do_delete() noexcept ;
} ;