InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
K2::scoped_array< T > Class Template Reference

#include <K2SmartPtr.h>

Inheritance diagram for K2::scoped_array< T >:

Public Types

typedef T element_type
 

Public Member Functions

 scoped_array (T *p=0)
 
 ~scoped_array ()
 
void reset (T *p=0)
 
T * get () const
 
T * release ()
 
T & operator[] (std::size_t i) const
 

Detailed Description

template<typename T>
class K2::scoped_array< T >

scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to is guaranteed, either on destruction of the scoped_array or via an explicit reset(). See shared_array or std::vector if your needs are more complex.

Notes:

  • It cannot be used in C++ standard library containers.
  • See shared_array if scoped_array does not meet your needs.
  • Use scoped_ptr to hold a pointer to a single object.
  • A std::vector is an alternative to a scoped_array that is a bit heavier duty but far more flexible.

Constructor & Destructor Documentation

template<typename T >
K2::scoped_array< T >::scoped_array(T * p = 0)
inlineexplicit

Constructs a scoped_array, storing a copy of p which MUST have been allocated using the operator new[] (or must be nil).

template<typename T >
K2::scoped_array< T >::~scoped_array()
inline

Deletes the array pointed by the stored pointer. Note that a delete[] on a nil pointer is harmless.

Member Function Documentation

template<typename T >
T* K2::scoped_array< T >::get () const
inline

Gets the stored pointer.

Returns
the stored pointer.
template<typename T >
T& K2::scoped_array< T >::operator[] (std::size_t i) const
inline

Subscript operator. Returns the element at the specified position in the array. Behavior is undefined and almost certainly undesirable if the stored pointer is nil, or if i is less than 0 or is greater than or equal to the number of elements in the array.

Parameters
i[IN] - index in the array of the element.
Returns
a reference to the element i of the array pointed by the stored pointer.
template<typename T >
T* K2::scoped_array< T >::release ()
inline

Detaches from the stored pointer by transferring the ownership to the caller. Note: the signature of this method is error prone because it allows the caller to lose the resource by not assign it to anything.

Returns
the stored pointer.
template<typename T >
void K2::scoped_array< T >::reset (T * p = 0)
inline

Deletes the array pointed by the stored pointer and then re-assigns it to the new value.