Loading...
Searching...
No Matches
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 >:

## Public Member Functions

| LocalPointer (T *p=nullptr)
| Constructor takes ownership.
| LocalPointer (T *p, UErrorCode &errorCode)
| Constructor takes ownership and reports an error if nullptr.
| LocalPointer (LocalPointer< T > &&src) noexcept
| Move constructor, leaves src with isNull().
| LocalPointer (std::unique_ptr< T > &&p)
| Constructs a LocalPointer from a C++11 std::unique_ptr.
| ~LocalPointer ()
| Destructor deletes the object it owns.
LocalPointer< T > & | operator= (LocalPointer< T > &&src) noexcept
| Move assignment operator, leaves src with isNull().
LocalPointer< T > & | operator= (std::unique_ptr< T > &&p) noexcept
| Move-assign from an std::unique_ptr to this LocalPointer.
void | swap (LocalPointer< T > &other) noexcept
| 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.
| operator std::unique_ptr< T > () &&
| Conversion operator to a C++11 std::unique_ptr.
Public Member Functions inherited from icu::LocalPointerBase< T >
| LocalPointerBase (T *p=nullptr)
| Constructor takes ownership.
| ~LocalPointerBase ()
| Destructor deletes the object it owns.
UBool | isNull () const
| nullptr check.
UBool | isValid () const
| nullptr check.
bool | operator== (const T *other) const
| Comparison with a simple pointer, so that existing code with ==nullptr need not be changed.
bool | operator!= (const T *other) const
| Comparison with a simple pointer, so that existing code with !=nullptr need not be changed.
T * | getAlias () const
| Access without ownership change.
T & | operator* () const
| Access without ownership change.
T * | operator-> () const
| Access without ownership change.
T * | orphan ()
| Gives up ownership; the internal pointer becomes nullptr.
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) noexcept
| Non-member LocalPointer swap function.

## Additional Inherited Members

Static Public Member Functions inherited from icu::LocalPointerBase< T >
static void * | operator new (size_t)=delete
static void * | operator new[] (size_t)=delete
static void * | operator new (size_t, void *)=delete
Protected Attributes inherited from icu::LocalPointerBase< T >
T * | ptr
| 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:

LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
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
icu::LocalPointer::LocalPointer
LocalPointer(T *p=nullptr)
Constructor takes ownership.
Definition localpointer.h:198
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:449

See also
  LocalPointerBase

Stable
  ICU 4.4

Definition at line 189 of file localpointer.h.

## Constructor & Destructor Documentation

## LocalPointer() [1/4]

template<typename T>

icu::LocalPointer< T >::LocalPointer | ( | T * | p = nullptr | ) |

inline explicit

Constructor takes ownership.

Parameters

  p | simple pointer to an object that is adopted

Stable
  ICU 4.4

Definition at line 198 of file localpointer.h.

References icu::LocalPointerBase< T >::LocalPointerBase().

Referenced by LocalPointer(), operator=(), operator=(), swap(), and swap.

## LocalPointer() [2/4]

template<typename T>

icu::LocalPointer< T >::LocalPointer | ( | T * | p,
|  | UErrorCode & | errorCode )

inline

Constructor takes ownership and reports an error if nullptr.

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

  p | simple pointer to an object that is adopted
  errorCode | in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR if p==nullptr and no other failure code had been set

Stable
  ICU 55

Definition at line 212 of file localpointer.h.

References icu::LocalPointerBase< T >::LocalPointerBase(), U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

## LocalPointer() [3/4]

template<typename T>

icu::LocalPointer< T >::LocalPointer | ( | LocalPointer< T > && | src | ) |

inline noexcept

Move constructor, leaves src with isNull().

Parameters

  src | source smart pointer

Stable
  ICU 56

Definition at line 222 of file localpointer.h.

References LocalPointer(), and icu::LocalPointerBase< T >::LocalPointerBase().

## LocalPointer() [4/4]

template<typename T>

icu::LocalPointer< T >::LocalPointer | ( | std::unique_ptr< T > && | p | ) |

inline explicit

Constructs a LocalPointer from a C++11 std::unique_ptr.

The LocalPointer steals the object owned by the std::unique_ptr.

This constructor works via move semantics. If your std::unique_ptr is in a local variable, you must use std::move.

Parameters

  p | The std::unique_ptr from which the pointer will be stolen.

Stable
  ICU 64

Definition at line 236 of file localpointer.h.

References icu::LocalPointerBase< T >::LocalPointerBase().

## ~LocalPointer()

template<typename T>

icu::LocalPointer< T >::~LocalPointer | ( |  | ) |

inline

Destructor deletes the object it owns.

Stable
  ICU 4.4

Definition at line 243 of file localpointer.h.

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

## 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

  p | simple pointer to an object that is adopted

Stable
  ICU 4.4

Definition at line 298 of file localpointer.h.

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

Referenced by operator=().

## 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 nullptr, then U_MEMORY_ALLOCATION_ERROR is set, the current object is deleted, and nullptr is set.

Parameters

  p | simple pointer to an object that is adopted
  errorCode | in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR if p==nullptr and no other failure code had been set

Stable
  ICU 55

Definition at line 317 of file localpointer.h.

References icu::LocalPointerBase< T >::ptr, U_MEMORY_ALLOCATION_ERROR, and U_SUCCESS.

## operator std::unique_ptr< T >()

template<typename T>

icu::LocalPointer< T >::operator std::unique_ptr< T > | ( |  | ) | &&

inline

Conversion operator to a C++11 std::unique_ptr.

Disowns the object and gives it to the returned std::unique_ptr.

This operator works via move semantics. If your LocalPointer is in a local variable, you must use std::move.

Returns
  An std::unique_ptr owning the pointer previously owned by this icu::LocalPointer.

Stable
  ICU 64

Definition at line 340 of file localpointer.h.

References icu::LocalPointerBase< T >::orphan().

## operator=() [1/2]

template<typename T>

LocalPointer< T > & icu::LocalPointer< T >::operator= | ( | LocalPointer< T > && | src | ) |

inline noexcept

Move assignment operator, leaves src with isNull().

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

Parameters

  src | source smart pointer

Returns
  *this

Stable
  ICU 56

Definition at line 253 of file localpointer.h.

References LocalPointer(), and icu::LocalPointerBase< T >::ptr.

## operator=() [2/2]

template<typename T>

LocalPointer< T > & icu::LocalPointer< T >::operator= | ( | std::unique_ptr< T > && | p | ) |

inline noexcept

Move-assign from an std::unique_ptr to this LocalPointer.

Steals the pointer from the std::unique_ptr.

Parameters

  p | The std::unique_ptr from which the pointer will be stolen.

Returns
  *this

Stable
  ICU 64

Definition at line 268 of file localpointer.h.

References adoptInstead(), and LocalPointer().

## swap()

template<typename T>

void icu::LocalPointer< T >::swap | ( | LocalPointer< T > & | other | ) |

inline noexcept

Swap pointers.

Parameters

  other | other smart pointer

Stable
  ICU 56

Definition at line 278 of file localpointer.h.

References LocalPointer(), and 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

  p1 | will get p2's pointer
  p2 | will get p1's pointer

Stable
  ICU 56

Definition at line 289 of file localpointer.h.

References LocalPointer().

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

- common/unicode/localpointer.h