ICU 59.1  59.1
Public Member Functions | Friends
icu::LocalArray< T > Class Template Reference

"Smart pointer" class, deletes objects via the C++ array delete[] operator. More...

#include <localpointer.h>

Inheritance diagram for icu::LocalArray< T >:
icu::LocalPointerBase< T >

Public Member Functions

 LocalArray (T *p=NULL)
 Constructor takes ownership. More...
 
 LocalArray (T *p, UErrorCode &errorCode)
 Constructor takes ownership and reports an error if NULL. More...
 
 LocalArray (LocalArray< T > &&src) U_NOEXCEPT
 Move constructor, leaves src with isNull(). More...
 
 ~LocalArray ()
 Destructor deletes the array it owns. More...
 
LocalArray< T > & operator= (LocalArray< T > &&src) U_NOEXCEPT
 Move assignment operator, leaves src with isNull(). More...
 
LocalArray< T > & moveFrom (LocalArray< T > &src) U_NOEXCEPT
 Move assignment, leaves src with isNull(). More...
 
void swap (LocalArray< T > &other) U_NOEXCEPT
 Swap pointers. More...
 
void adoptInstead (T *p)
 Deletes the array it owns, and adopts (takes ownership of) the one passed in. More...
 
void adoptInsteadAndCheckErrorCode (T *p, UErrorCode &errorCode)
 Deletes the array it owns, and adopts (takes ownership of) the one passed in. More...
 
T & operator[] (ptrdiff_t i) const
 Array item access (writable). More...
 
- Public Member Functions inherited from icu::LocalPointerBase< T >
 LocalPointerBase (T *p=NULL)
 Constructor takes ownership. More...
 
 ~LocalPointerBase ()
 Destructor deletes the object it owns. More...
 
UBool isNull () const
 NULL check. More...
 
UBool isValid () const
 NULL check. More...
 
bool operator== (const T *other) const
 Comparison with a simple pointer, so that existing code with ==NULL need not be changed. More...
 
bool operator!= (const T *other) const
 Comparison with a simple pointer, so that existing code with !=NULL need not be changed. More...
 
T * getAlias () const
 Access without ownership change. More...
 
T & operator* () const
 Access without ownership change. More...
 
T * operator-> () const
 Access without ownership change. More...
 
T * orphan ()
 Gives up ownership; the internal pointer becomes NULL. More...
 
void adoptInstead (T *p)
 Deletes the object it owns, and adopts (takes ownership of) the one passed in. More...
 

Friends

void swap (LocalArray< T > &p1, LocalArray< T > &p2) U_NOEXCEPT
 Non-member LocalArray swap function. More...
 

Additional Inherited Members

- Protected Attributes inherited from icu::LocalPointerBase< T >
T * ptr
 Actual pointer. More...
 

Detailed Description

template<typename T>
class icu::LocalArray< T >

"Smart pointer" class, deletes objects via the C++ array delete[] operator.

For most methods see the LocalPointerBase base class. Adds operator[] for array item access.

Usage example:

LocalArray<UnicodeString> a(new UnicodeString[2]);
a[0].append((char16_t)0x61);
if(some condition) { return; } // no need to explicitly delete the array
a.adoptInstead(new UnicodeString[4]);
a[3].append((char16_t)0x62).append((char16_t)0x63).reverse();
// no need to explicitly delete the array
See also
LocalPointerBase
Stable:
ICU 4.4

Definition at line 337 of file localpointer.h.

Constructor & Destructor Documentation

§ LocalArray() [1/3]

template<typename T>
icu::LocalArray< T >::LocalArray ( T *  p = NULL)
inlineexplicit

Constructor takes ownership.

Parameters
psimple pointer to an array of T objects that is adopted
Stable:
ICU 4.4

Definition at line 346 of file localpointer.h.

§ LocalArray() [2/3]

template<typename T>
icu::LocalArray< T >::LocalArray ( T *  p,
UErrorCode errorCode 
)
inline

Constructor takes ownership and reports an error if NULL.

This constructor is intended to be used with other-class constructors that may report a failure UErrorCode, so that callers need to check only for U_FAILURE(errorCode) and not also separately for isNull().

Parameters
psimple pointer to an array of T objects that is adopted
errorCodein/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR if p==NULL and no other failure code had been set
Stable:
ICU 56

Definition at line 360 of file localpointer.h.

References NULL, U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

§ LocalArray() [3/3]

template<typename T>
icu::LocalArray< T >::LocalArray ( LocalArray< T > &&  src)
inline

Move constructor, leaves src with isNull().

Parameters
srcsource smart pointer
Stable:
ICU 56

Definition at line 371 of file localpointer.h.

References NULL, and icu::LocalPointerBase< T >::ptr.

§ ~LocalArray()

template<typename T>
icu::LocalArray< T >::~LocalArray ( )
inline

Destructor deletes the array it owns.

Stable:
ICU 4.4

Definition at line 379 of file localpointer.h.

Member Function Documentation

§ adoptInstead()

template<typename T>
void icu::LocalArray< T >::adoptInstead ( T *  p)
inline

Deletes the array it owns, and adopts (takes ownership of) the one passed in.

Parameters
psimple pointer to an array of T objects that is adopted
Stable:
ICU 4.4

Definition at line 435 of file localpointer.h.

§ adoptInsteadAndCheckErrorCode()

template<typename T>
void icu::LocalArray< T >::adoptInsteadAndCheckErrorCode ( T *  p,
UErrorCode errorCode 
)
inline

Deletes the array it owns, and adopts (takes ownership of) the one passed in.

If U_FAILURE(errorCode), then the current array is retained and the new one deleted.

If U_SUCCESS(errorCode) but the input pointer is NULL, then U_MEMORY_ALLOCATION_ERROR is set, the current array is deleted, and NULL is set.

Parameters
psimple pointer to an array of T objects that is adopted
errorCodein/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR if p==NULL and no other failure code had been set
Stable:
ICU 56

Definition at line 454 of file localpointer.h.

References NULL, U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

§ moveFrom()

template<typename T>
LocalArray<T>& icu::LocalArray< T >::moveFrom ( LocalArray< T > &  src)
inline

Move assignment, leaves src with isNull().

The behavior is undefined if *this and src are the same object.

Can be called explicitly, does not need C++11 support.

Parameters
srcsource smart pointer
Returns
*this
Draft:
This API may be changed in the future versions and was introduced in ICU 56

Definition at line 404 of file localpointer.h.

References NULL, and icu::LocalPointerBase< T >::ptr.

§ operator=()

template<typename T>
LocalArray<T>& icu::LocalArray< T >::operator= ( LocalArray< T > &&  src)
inline

Move assignment operator, leaves src with isNull().

The behavior is undefined if *this and src are the same object.

Parameters
srcsource smart pointer
Returns
*this
Stable:
ICU 56

Definition at line 390 of file localpointer.h.

§ operator[]()

template<typename T>
T& icu::LocalArray< T >::operator[] ( ptrdiff_t  i) const
inline

Array item access (writable).

No index bounds check.

Parameters
iarray index
Returns
reference to the array item
Stable:
ICU 4.4

Definition at line 472 of file localpointer.h.

§ swap()

template<typename T>
void icu::LocalArray< T >::swap ( LocalArray< T > &  other)
inline

Swap pointers.

Parameters
otherother smart pointer
Stable:
ICU 56

Definition at line 415 of file localpointer.h.

References icu::LocalPointerBase< T >::ptr.

Friends And Related Function Documentation

§ swap

template<typename T>
void swap ( LocalArray< T > &  p1,
LocalArray< T > &  p2 
)
friend

Non-member LocalArray swap function.

Parameters
p1will get p2's pointer
p2will get p1's pointer
Stable:
ICU 56

Definition at line 426 of file localpointer.h.


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