ICU 58.2 58.2

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

- File List
- Globals

localpointer.h
Go to the documentation of this file.
1 // Copyright (C) 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: US-ASCII
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 }
216 #if U_HAVE_RVALUE_REFERENCES
217
222 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
223 src.ptr=NULL;
224 }
225 #endif
226
230 ~LocalPointer() {
231 delete LocalPointerBase<T>::ptr;
232 }
233 #if U_HAVE_RVALUE_REFERENCES
234
241 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
242 return moveFrom(src);
243 }
244 #endif
245 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
255 LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
256 delete LocalPointerBase<T>::ptr;
257 LocalPointerBase<T>::ptr=src.ptr;
258 src.ptr=NULL;
259 return *this;
260 }
266 void swap(LocalPointer<T> &other) U_NOEXCEPT {
267 T *temp=LocalPointerBase<T>::ptr;
268 LocalPointerBase<T>::ptr=other.ptr;
269 other.ptr=temp;
270 }
277 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
278 p1.swap(p2);
279 }
286 void adoptInstead(T *p) {
287 delete LocalPointerBase<T>::ptr;
288 LocalPointerBase<T>::ptr=p;
289 }
305 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
306 if(U_SUCCESS(errorCode)) {
307 delete LocalPointerBase<T>::ptr;
308 LocalPointerBase<T>::ptr=p;
309 if(p==NULL) {
310 errorCode=U_MEMORY_ALLOCATION_ERROR;
311 }
312 } else {
313 delete p;
314 }
315 }
316 };
317
336 template<typename T>
337 class LocalArray : public LocalPointerBase<T> {
338 public:
339 using LocalPointerBase<T>::operator*;
340 using LocalPointerBase<T>::operator->;
346 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
360 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
361 if(p==NULL && U_SUCCESS(errorCode)) {
362 errorCode=U_MEMORY_ALLOCATION_ERROR;
363 }
364 }
365 #if U_HAVE_RVALUE_REFERENCES
366
371 LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
372 src.ptr=NULL;
373 }
374 #endif
375
379 ~LocalArray() {
380 delete[] LocalPointerBase<T>::ptr;
381 }
382 #if U_HAVE_RVALUE_REFERENCES
383
390 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
391 return moveFrom(src);
392 }
393 #endif
394 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
404 LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
405 delete[] LocalPointerBase<T>::ptr;
406 LocalPointerBase<T>::ptr=src.ptr;
407 src.ptr=NULL;
408 return *this;
409 }
415 void swap(LocalArray<T> &other) U_NOEXCEPT {
416 T *temp=LocalPointerBase<T>::ptr;
417 LocalPointerBase<T>::ptr=other.ptr;
418 other.ptr=temp;
419 }
426 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
427 p1.swap(p2);
428 }
435 void adoptInstead(T *p) {
436 delete[] LocalPointerBase<T>::ptr;
437 LocalPointerBase<T>::ptr=p;
438 }
454 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
455 if(U_SUCCESS(errorCode)) {
456 delete[] LocalPointerBase<T>::ptr;
457 LocalPointerBase<T>::ptr=p;
458 if(p==NULL) {
459 errorCode=U_MEMORY_ALLOCATION_ERROR;
460 }
461 } else {
462 delete[] p;
463 }
464 }
472 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
473 };
474
495 #if U_HAVE_RVALUE_REFERENCES
496 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
497 class LocalPointerClassName : public LocalPointerBase<Type> { \
498 public: \
499 using LocalPointerBase<Type>::operator*; \
500 using LocalPointerBase<Type>::operator->; \
501 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
502 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
503 : LocalPointerBase<Type>(src.ptr) { \
504 src.ptr=NULL; \
505 } \
506 ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
507 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
508 return moveFrom(src); \
509 } \
510 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
511 if (ptr != NULL) { closeFunction(ptr); } \
512 LocalPointerBase<Type>::ptr=src.ptr; \
513 src.ptr=NULL; \
514 return *this; \
515 } \
516 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
517 Type *temp=LocalPointerBase<Type>::ptr; \
518 LocalPointerBase<Type>::ptr=other.ptr; \
519 other.ptr=temp; \
520 } \
521 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
522 p1.swap(p2); \
523 } \
524 void adoptInstead(Type *p) { \
525 if (ptr != NULL) { closeFunction(ptr); } \
526 ptr=p; \
527 } \
528 }
529 #else
530 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
531 class LocalPointerClassName : public LocalPointerBase<Type> { \
532 public: \
533 using LocalPointerBase<Type>::operator*; \
534 using LocalPointerBase<Type>::operator->; \
535 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
536 ~LocalPointerClassName() { closeFunction(ptr); } \
537 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
538 if (ptr != NULL) { closeFunction(ptr); } \
539 LocalPointerBase<Type>::ptr=src.ptr; \
540 src.ptr=NULL; \
541 return *this; \
542 } \
543 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
544 Type *temp=LocalPointerBase<Type>::ptr; \
545 LocalPointerBase<Type>::ptr=other.ptr; \
546 other.ptr=temp; \
547 } \
548 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
549 p1.swap(p2); \
550 } \
551 void adoptInstead(Type *p) { \
552 if (ptr != NULL) { closeFunction(ptr); } \
553 ptr=p; \
554 } \
555 }
556 #endif
557
558 U_NAMESPACE_END
559
560 #endif /* U_SHOW_CPLUSPLUS_API */
561 #endif /* __LOCALPOINTER_H__ */
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:454
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:286
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
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:683
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:346
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:113
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition: localpointer.h:125
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:360
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:266
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition: localpointer.h:153
icu::LocalPointerBase::isNull
UBool isNull() const
NULL check.
Definition: localpointer.h:85
U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:438
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition: localpointer.h:119
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:255
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:277
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::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:220
icu::LocalArray::swap
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) U_NOEXCEPT
Non-member LocalArray swap function.
Definition: localpointer.h:426
NULL
#define NULL
Define NULL if necessary, to 0 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:337
icu::LocalPointer::~LocalPointer
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:230
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:531
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:435
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
icu::LocalArray::swap
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:415
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::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:379
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:305
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:404
icu::LocalPointerBase::isValid
UBool isValid() const
NULL check.
Definition: localpointer.h:91
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
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:472
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259