ICU 56.1: localpointer.h File Reference

ICU 56.1 56.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

Data Structures | Namespaces | Macros
localpointer.h File Reference

C++ API: "Smart pointers" for use with and in ICU4C C++ code. More...

`#include "unicode/utypes.h"`

Go to the source code of this file.

## Data Structures
class | icu::LocalPointerBase< T >
| "Smart pointer" base class; do not use directly: use LocalPointer etc. More...
class | icu::LocalPointer< T >
| "Smart pointer" class, deletes objects via the standard C++ delete operator. More...
class | icu::LocalArray< T >
| "Smart pointer" class, deletes objects via the C++ array delete[] operator. More...

## Namespaces
| icu
| File coll.h.

## Macros
#define | U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
| "Smart pointer" definition macro, deletes objects via the closeFunction. More...

## Detailed Description

C++ API: "Smart pointers" for use with and in ICU4C C++ code.

These classes are inspired by

- std::auto_ptr
- boost::scoped_ptr & boost::scoped_array
- Taligent Safe Pointers (TOnlyPointerTo)

but none of those provide for all of the goals for ICU smart pointers:

- Smart pointer owns the object and releases it when it goes out of scope.
- No transfer of ownership via copy/assignment to reduce misuse. Simpler & more robust.
- ICU-compatible: No exceptions.
- Need to be able to orphan/release the pointer and its ownership.
- Need variants for normal C++ object pointers, C++ arrays, and ICU C service objects.

For details see http://site.icu-project.org/design/cpp/scoped_ptr

Definition in file localpointer.h.

## Macro Definition Documentation

#define U_DEFINE_LOCAL_OPEN_POINTER | ( |  | LocalPointerClassName,
|  |  | Type,
|  |  | closeFunction
| ) |  |

Value:
class LocalPointerClassName : public LocalPointerBase<Type> { \
public: \
explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
~LocalPointerClassName() { closeFunction(ptr); } \
LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
closeFunction(ptr); \
LocalPointerBase<Type>::ptr=src.ptr; \
src.ptr=NULL; \
return *this; \
} \
void swap(LocalPointerClassName &other) U_NOEXCEPT { \
Type *temp=LocalPointerBase<Type>::ptr; \
LocalPointerBase<Type>::ptr=other.ptr; \
other.ptr=temp; \
} \
friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
p1.swap(p2); \
} \
void adoptInstead(Type *p) { \
closeFunction(ptr); \
ptr=p; \
} \
}
NULL
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:521

"Smart pointer" definition macro, deletes objects via the closeFunction.

Defines a subclass of LocalPointerBase which works just like LocalPointer<Type> except that this subclass will use the closeFunction rather than the C++ delete operator.

Requirement: The closeFunction must tolerate a NULL pointer. (We could add a NULL check here but it is normally redundant.)

Usage example:

1 LocalUCaseMapPointer csm(ucasemap_open(localeID, options, &errorCode));
2 utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
3 utf8Out, (int32_t)sizeof(utf8Out),
4 utf8In, utf8InLength, &errorCode);
5 if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCaseMap

See also
  LocalPointerBase
  LocalPointer

Stable:
  ICU 4.4

Definition at line 535 of file localpointer.h.