ICU 62.1 62.1
Public Member Functions | Friends
icu::LocalPointer< T > Class Template Reference

"Smart pointer" class, deletes objects via the standard C++ delete operator. More...

#include <localpointer.h>

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

Public Member Functions

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

Friends

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

Additional Inherited Members

- Protected Attributes inherited from icu::LocalPointerBase< T >
Tptr
 Actual pointer.
 

Detailed Description

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

"Smart pointer" class, deletes objects via the standard C++ delete operator.

For most methods see the LocalPointerBase base class.

Usage example:

int32_t length=s->length(); // 2
char16_t lead=s->charAt(0); // 0xd900
if(some condition) { return; } // no need to explicitly delete the pointer
s.adoptInstead(new UnicodeString((char16_t)0xfffc));
length=s->length(); // 1
// no need to explicitly delete the pointer
"Smart pointer" base class; do not use directly: use LocalPointer etc.
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:287
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:400
See also
LocalPointerBase
Stable
ICU 4.4

Definition at line 188 of file localpointer.h.

Constructor & Destructor Documentation

◆ LocalPointer() [1/3]

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

Constructor takes ownership.

Parameters
psimple pointer to an object that is adopted
Stable
ICU 4.4

Definition at line 197 of file localpointer.h.

◆ LocalPointer() [2/3]

template<typename T >
icu::LocalPointer< T >::LocalPointer ( 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 object 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 55

Definition at line 211 of file localpointer.h.

References NULL, U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

◆ LocalPointer() [3/3]

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

Move constructor, leaves src with isNull().

Parameters
srcsource smart pointer
Stable
ICU 56

Definition at line 221 of file localpointer.h.

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

◆ ~LocalPointer()

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

Destructor deletes the object it owns.

Stable
ICU 4.4

Definition at line 228 of file localpointer.h.

Member Function Documentation

◆ adoptInstead()

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

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

Parameters
psimple pointer to an object that is adopted
Stable
ICU 4.4

Definition at line 282 of file localpointer.h.

◆ adoptInsteadAndCheckErrorCode()

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

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

If U_FAILURE(errorCode), then the current object 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 object is deleted, and NULL is set.

Parameters
psimple pointer to an object 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 55

Definition at line 301 of file localpointer.h.

References NULL, U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

◆ moveFrom()

template<typename T >
LocalPointer< T > & icu::LocalPointer< T >::moveFrom ( LocalPointer< 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 251 of file localpointer.h.

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

◆ operator=()

template<typename T >
LocalPointer< T > & icu::LocalPointer< T >::operator= ( LocalPointer< 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 238 of file localpointer.h.

◆ swap()

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

Swap pointers.

Parameters
otherother smart pointer
Stable
ICU 56

Definition at line 262 of file localpointer.h.

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

Friends And Related Symbol Documentation

◆ swap

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

Non-member LocalPointer swap function.

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

Definition at line 273 of file localpointer.h.


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