ICU 56.1: localpointer.h Source File

ICU 56.1 56.1

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

- File List
- Globals

localpointer.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2009-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: localpointer.h
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2009nov13
14 * created by: Markus W. Scherer
15 */
16
17 #ifndef __LOCALPOINTER_H__
18 #define __LOCALPOINTER_H__
19
39 #include "unicode/utypes.h"
40
41 #if U_SHOW_CPLUSPLUS_API
42
43 U_NAMESPACE_BEGIN
44
63 template<typename T>
64 class LocalPointerBase {
65 public:
71 explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
77 ~LocalPointerBase() { /* delete ptr; */ }
83 UBool isNull() const { return ptr==NULL; }
89 UBool isValid() const { return ptr!=NULL; }
97 bool operator==(const T *other) const { return ptr==other; }
105 bool operator!=(const T *other) const { return ptr!=other; }
111 T *getAlias() const { return ptr; }
117 T &operator*() const { return *ptr; }
123 T *operator->() const { return ptr; }
130 T *orphan() {
131 T *p=ptr;
132 ptr=NULL;
133 return p;
134 }
142 void adoptInstead(T *p) {
143 // delete ptr;
144 ptr=p;
145 }
146 protected:
151 T *ptr;
152 private:
153 // No comparison operators with other LocalPointerBases.
154 bool operator==(const LocalPointerBase<T> &other);
155 bool operator!=(const LocalPointerBase<T> &other);
156 // No ownership sharing: No copy constructor, no assignment operator.
157 LocalPointerBase(const LocalPointerBase<T> &other);
158 void operator=(const LocalPointerBase<T> &other);
159 // No heap allocation. Use only on the stack.
160 static void * U_EXPORT2 operator new(size_t size);
161 static void * U_EXPORT2 operator new[](size_t size);
162 #if U_HAVE_PLACEMENT_NEW
163 static void * U_EXPORT2 operator new(size_t, void *ptr);
164 #endif
165 };
166
185 template<typename T>
186 class LocalPointer : public LocalPointerBase<T> {
187 public:
193 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
194 #ifndef U_HIDE_DRAFT_API
195
208 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
209 if(p==NULL && U_SUCCESS(errorCode)) {
210 errorCode=U_MEMORY_ALLOCATION_ERROR;
211 }
212 }
213 #if U_HAVE_RVALUE_REFERENCES
214
219 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
220 src.ptr=NULL;
221 }
222 #endif
223 #endif /* U_HIDE_DRAFT_API */
224
228 ~LocalPointer() {
229 delete LocalPointerBase<T>::ptr;
230 }
231 #ifndef U_HIDE_DRAFT_API
232 #if U_HAVE_RVALUE_REFERENCES
233
240 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
241 return moveFrom(src);
242 }
243 #endif
244
253 LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
254 delete LocalPointerBase<T>::ptr;
255 LocalPointerBase<T>::ptr=src.ptr;
256 src.ptr=NULL;
257 return *this;
258 }
264 void swap(LocalPointer<T> &other) U_NOEXCEPT {
265 T *temp=LocalPointerBase<T>::ptr;
266 LocalPointerBase<T>::ptr=other.ptr;
267 other.ptr=temp;
268 }
275 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
276 p1.swap(p2);
277 }
278 #endif /* U_HIDE_DRAFT_API */
279
285 void adoptInstead(T *p) {
286 delete LocalPointerBase<T>::ptr;
287 LocalPointerBase<T>::ptr=p;
288 }
289 #ifndef U_HIDE_DRAFT_API
290
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 #endif /* U_HIDE_DRAFT_API */
317 };
318
337 template<typename T>
338 class LocalArray : public LocalPointerBase<T> {
339 public:
345 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
346 #ifndef U_HIDE_DRAFT_API
347
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 #endif /* U_HIDE_DRAFT_API */
376
380 ~LocalArray() {
381 delete[] LocalPointerBase<T>::ptr;
382 }
383 #ifndef U_HIDE_DRAFT_API
384 #if U_HAVE_RVALUE_REFERENCES
385
392 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
393 return moveFrom(src);
394 }
395 #endif
396
405 LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
406 delete[] LocalPointerBase<T>::ptr;
407 LocalPointerBase<T>::ptr=src.ptr;
408 src.ptr=NULL;
409 return *this;
410 }
416 void swap(LocalArray<T> &other) U_NOEXCEPT {
417 T *temp=LocalPointerBase<T>::ptr;
418 LocalPointerBase<T>::ptr=other.ptr;
419 other.ptr=temp;
420 }
427 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
428 p1.swap(p2);
429 }
430 #endif /* U_HIDE_DRAFT_API */
431
437 void adoptInstead(T *p) {
438 delete[] LocalPointerBase<T>::ptr;
439 LocalPointerBase<T>::ptr=p;
440 }
441 #ifndef U_HIDE_DRAFT_API
442
457 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
458 if(U_SUCCESS(errorCode)) {
459 delete[] LocalPointerBase<T>::ptr;
460 LocalPointerBase<T>::ptr=p;
461 if(p==NULL) {
462 errorCode=U_MEMORY_ALLOCATION_ERROR;
463 }
464 } else {
465 delete[] p;
466 }
467 }
468 #endif /* U_HIDE_DRAFT_API */
469
476 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
477 };
478
502 #if U_HAVE_RVALUE_REFERENCES
503 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
504 class LocalPointerClassName : public LocalPointerBase<Type> { \
505 public: \
506 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
507 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
508 : LocalPointerBase<Type>(src.ptr) { \
509 src.ptr=NULL; \
510 } \
511 ~LocalPointerClassName() { closeFunction(ptr); } \
512 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
513 return moveFrom(src); \
514 } \
515 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
516 closeFunction(ptr); \
517 LocalPointerBase<Type>::ptr=src.ptr; \
518 src.ptr=NULL; \
519 return *this; \
520 } \
521 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
522 Type *temp=LocalPointerBase<Type>::ptr; \
523 LocalPointerBase<Type>::ptr=other.ptr; \
524 other.ptr=temp; \
525 } \
526 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
527 p1.swap(p2); \
528 } \
529 void adoptInstead(Type *p) { \
530 closeFunction(ptr); \
531 ptr=p; \
532 } \
533 }
534 #else
535 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
536 class LocalPointerClassName : public LocalPointerBase<Type> { \
537 public: \
538 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
539 ~LocalPointerClassName() { closeFunction(ptr); } \
540 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
541 closeFunction(ptr); \
542 LocalPointerBase<Type>::ptr=src.ptr; \
543 src.ptr=NULL; \
544 return *this; \
545 } \
546 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
547 Type *temp=LocalPointerBase<Type>::ptr; \
548 LocalPointerBase<Type>::ptr=other.ptr; \
549 other.ptr=temp; \
550 } \
551 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
552 p1.swap(p2); \
553 } \
554 void adoptInstead(Type *p) { \
555 closeFunction(ptr); \
556 ptr=p; \
557 } \
558 }
559 #endif
560
561 U_NAMESPACE_END
562
563 #endif /* U_SHOW_CPLUSPLUS_API */
564 #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:457
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:285
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:97
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:711
icu::LocalPointer::LocalPointer
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:208
icu::LocalArray::LocalArray
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:345
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:111
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition: localpointer.h:123
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:186
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:264
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition: localpointer.h:151
icu::LocalPointerBase::isNull
UBool isNull() const
NULL check.
Definition: localpointer.h:83
U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:513
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition: localpointer.h:117
icu::LocalPointer::LocalPointer
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:193
icu::LocalPointer::moveFrom
LocalPointer< T > & moveFrom(LocalPointer< T > &src) U_NOEXCEPT
Move assignment, leaves src with isNull().
Definition: localpointer.h:253
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:275
icu::LocalPointerBase::orphan
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:130
icu::LocalPointerBase::LocalPointerBase
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:71
icu::LocalPointerBase
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:64
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:427
NULL
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
icu::LocalArray
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:338
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:521
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:437
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
icu::LocalArray::swap
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:416
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:105
icu::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:380
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:77
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:405
icu::LocalPointerBase::isValid
UBool isValid() const
NULL check.
Definition: localpointer.h:89
icu::LocalPointerBase::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:142
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:476
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234