ICU 63.1 63.1

localpointer.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2009-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: localpointer.h
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2009nov13
16 * created by: Markus W. Scherer
17 */
18
19 #ifndef __LOCALPOINTER_H__
20 #define __LOCALPOINTER_H__
21
41 #include "unicode/utypes.h"
42
43 #if U_SHOW_CPLUSPLUS_API
44
45 U_NAMESPACE_BEGIN
46
65 template<typename T>
66 class LocalPointerBase {
67 public:
73 explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
79 ~LocalPointerBase() { /* delete ptr; */ }
85 UBool isNull() const { return ptr==NULL; }
91 UBool isValid() const { return ptr!=NULL; }
99 bool operator==(const T *other) const { return ptr==other; }
107 bool operator!=(const T *other) const { return ptr!=other; }
113 T *getAlias() const { return ptr; }
119 T &operator*() const { return *ptr; }
125 T *operator->() const { return ptr; }
132 T *orphan() {
133 T *p=ptr;
134 ptr=NULL;
135 return p;
136 }
144 void adoptInstead(T *p) {
145 // delete ptr;
146 ptr=p;
147 }
148 protected:
153 T *ptr;
154 private:
155 // No comparison operators with other LocalPointerBases.
156 bool operator==(const LocalPointerBase<T> &other);
157 bool operator!=(const LocalPointerBase<T> &other);
158 // No ownership sharing: No copy constructor, no assignment operator.
159 LocalPointerBase(const LocalPointerBase<T> &other);
160 void operator=(const LocalPointerBase<T> &other);
161 // No heap allocation. Use only on the stack.
162 static void * U_EXPORT2 operator new(size_t size);
163 static void * U_EXPORT2 operator new[](size_t size);
164 #if U_HAVE_PLACEMENT_NEW
165 static void * U_EXPORT2 operator new(size_t, void *ptr);
166 #endif
167 };
168
187 template<typename T>
188 class LocalPointer : public LocalPointerBase<T> {
189 public:
190 using LocalPointerBase<T>::operator*;
191 using LocalPointerBase<T>::operator->;
197 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
211 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
212 if(p==NULL && U_SUCCESS(errorCode)) {
213 errorCode=U_MEMORY_ALLOCATION_ERROR;
214 }
215 }
221 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
222 src.ptr=NULL;
223 }
228 ~LocalPointer() {
229 delete LocalPointerBase<T>::ptr;
230 }
238 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
239 return moveFrom(src);
240 }
241 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
251 LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
252 delete LocalPointerBase<T>::ptr;
253 LocalPointerBase<T>::ptr=src.ptr;
254 src.ptr=NULL;
255 return *this;
256 }
262 void swap(LocalPointer<T> &other) U_NOEXCEPT {
263 T *temp=LocalPointerBase<T>::ptr;
264 LocalPointerBase<T>::ptr=other.ptr;
265 other.ptr=temp;
266 }
273 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
274 p1.swap(p2);
275 }
282 void adoptInstead(T *p) {
283 delete LocalPointerBase<T>::ptr;
284 LocalPointerBase<T>::ptr=p;
285 }
301 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
302 if(U_SUCCESS(errorCode)) {
303 delete LocalPointerBase<T>::ptr;
304 LocalPointerBase<T>::ptr=p;
305 if(p==NULL) {
306 errorCode=U_MEMORY_ALLOCATION_ERROR;
307 }
308 } else {
309 delete p;
310 }
311 }
312 };
313
332 template<typename T>
333 class LocalArray : public LocalPointerBase<T> {
334 public:
335 using LocalPointerBase<T>::operator*;
336 using LocalPointerBase<T>::operator->;
342 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
356 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
357 if(p==NULL && U_SUCCESS(errorCode)) {
358 errorCode=U_MEMORY_ALLOCATION_ERROR;
359 }
360 }
366 LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
367 src.ptr=NULL;
368 }
373 ~LocalArray() {
374 delete[] LocalPointerBase<T>::ptr;
375 }
383 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
384 return moveFrom(src);
385 }
386 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
396 LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
397 delete[] LocalPointerBase<T>::ptr;
398 LocalPointerBase<T>::ptr=src.ptr;
399 src.ptr=NULL;
400 return *this;
401 }
407 void swap(LocalArray<T> &other) U_NOEXCEPT {
408 T *temp=LocalPointerBase<T>::ptr;
409 LocalPointerBase<T>::ptr=other.ptr;
410 other.ptr=temp;
411 }
418 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
419 p1.swap(p2);
420 }
427 void adoptInstead(T *p) {
428 delete[] LocalPointerBase<T>::ptr;
429 LocalPointerBase<T>::ptr=p;
430 }
446 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
447 if(U_SUCCESS(errorCode)) {
448 delete[] LocalPointerBase<T>::ptr;
449 LocalPointerBase<T>::ptr=p;
450 if(p==NULL) {
451 errorCode=U_MEMORY_ALLOCATION_ERROR;
452 }
453 } else {
454 delete[] p;
455 }
456 }
464 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
465 };
466
487 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
488 class LocalPointerClassName : public LocalPointerBase<Type> { \
489 public: \
490 using LocalPointerBase<Type>::operator*; \
491 using LocalPointerBase<Type>::operator->; \
492 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
493 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
494 : LocalPointerBase<Type>(src.ptr) { \
495 src.ptr=NULL; \
496 } \
497 ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
498 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
499 return moveFrom(src); \
500 } \
501 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
502 if (ptr != NULL) { closeFunction(ptr); } \
503 LocalPointerBase<Type>::ptr=src.ptr; \
504 src.ptr=NULL; \
505 return *this; \
506 } \
507 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
508 Type *temp=LocalPointerBase<Type>::ptr; \
509 LocalPointerBase<Type>::ptr=other.ptr; \
510 other.ptr=temp; \
511 } \
512 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
513 p1.swap(p2); \
514 } \
515 void adoptInstead(Type *p) { \
516 if (ptr != NULL) { closeFunction(ptr); } \
517 ptr=p; \
518 } \
519 }
520
521 U_NAMESPACE_END
522
523 #endif /* U_SHOW_CPLUSPLUS_API */
524 #endif /* __LOCALPOINTER_H__ */
icu::LocalPointer::operator=
LocalPointer< T > & operator=(LocalPointer< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:238
icu::LocalArray::adoptInsteadAndCheckErrorCode
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:446
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:464
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:113
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:282
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:694
icu::LocalPointer::LocalPointer
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:211
icu::LocalArray::LocalArray
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:342
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:188
icu::LocalArray::LocalArray
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:356
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::LocalPointer::swap
void swap(LocalPointer< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:262
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition: localpointer.h:153
U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:443
icu::LocalPointerBase::operator!=
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=NULL need not be changed.
Definition: localpointer.h:107
icu::LocalPointer::LocalPointer
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:197
icu::LocalPointer::moveFrom
LocalPointer< T > & moveFrom(LocalPointer< T > &src) U_NOEXCEPT
Move assignment, leaves src with isNull().
Definition: localpointer.h:251
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:273
icu::LocalPointerBase::orphan
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:132
icu::LocalPointerBase::LocalPointerBase
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:73
icu::LocalPointerBase
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:66
icu::LocalPointerBase::isNull
UBool isNull() const
NULL check.
Definition: localpointer.h:85
icu::LocalPointer::LocalPointer
LocalPointer(LocalPointer< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:221
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
icu::LocalArray::swap
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) U_NOEXCEPT
Non-member LocalArray swap function.
Definition: localpointer.h:418
NULL
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
icu::LocalArray
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:333
icu::LocalArray::LocalArray
LocalArray(LocalArray< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:366
icu::LocalPointer::~LocalPointer
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:228
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
icu::LocalArray::operator=
LocalArray< T > & operator=(LocalArray< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:383
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:427
icu::LocalPointerBase::isValid
UBool isValid() const
NULL check.
Definition: localpointer.h:91
icu::LocalPointerBase::operator==
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==NULL need not be changed.
Definition: localpointer.h:99
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
icu::LocalArray::swap
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:407
icu::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:373
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition: localpointer.h:125
icu::LocalPointer::adoptInsteadAndCheckErrorCode
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:301
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition: localpointer.h:119
icu::LocalPointerBase::~LocalPointerBase
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:79
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::LocalArray::moveFrom
LocalArray< T > & moveFrom(LocalArray< T > &src) U_NOEXCEPT
Move assignment, leaves src with isNull().
Definition: localpointer.h:396
icu::LocalPointerBase::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:144
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225