ICU 65.1 65.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 #include <memory>
46
47 U_NAMESPACE_BEGIN
48
67 template<typename T>
68 class LocalPointerBase {
69 public:
70 // No heap allocation. Use only on the stack.
71 static void* U_EXPORT2 operator new(size_t) = delete;
72 static void* U_EXPORT2 operator new[](size_t) = delete;
73 #if U_HAVE_PLACEMENT_NEW
74 static void* U_EXPORT2 operator new(size_t, void*) = delete;
75 #endif
76
82 explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
88 ~LocalPointerBase() { /* delete ptr; */ }
94 UBool isNull() const { return ptr==NULL; }
100 UBool isValid() const { return ptr!=NULL; }
108 bool operator==(const T *other) const { return ptr==other; }
116 bool operator!=(const T *other) const { return ptr!=other; }
122 T *getAlias() const { return ptr; }
128 T &operator*() const { return *ptr; }
134 T *operator->() const { return ptr; }
141 T *orphan() {
142 T *p=ptr;
143 ptr=NULL;
144 return p;
145 }
153 void adoptInstead(T *p) {
154 // delete ptr;
155 ptr=p;
156 }
157 protected:
162 T *ptr;
163 private:
164 // No comparison operators with other LocalPointerBases.
165 bool operator==(const LocalPointerBase<T> &other);
166 bool operator!=(const LocalPointerBase<T> &other);
167 // No ownership sharing: No copy constructor, no assignment operator.
168 LocalPointerBase(const LocalPointerBase<T> &other);
169 void operator=(const LocalPointerBase<T> &other);
170 };
171
190 template<typename T>
191 class LocalPointer : public LocalPointerBase<T> {
192 public:
193 using LocalPointerBase<T>::operator*;
194 using LocalPointerBase<T>::operator->;
200 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
214 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
215 if(p==NULL && U_SUCCESS(errorCode)) {
216 errorCode=U_MEMORY_ALLOCATION_ERROR;
217 }
218 }
224 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
225 src.ptr=NULL;
226 }
227
228 #ifndef U_HIDE_DRAFT_API
229
239 explicit LocalPointer(std::unique_ptr<T> &&p)
240 : LocalPointerBase<T>(p.release()) {}
241 #endif /* U_HIDE_DRAFT_API */
242
247 ~LocalPointer() {
248 delete LocalPointerBase<T>::ptr;
249 }
257 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
258 delete LocalPointerBase<T>::ptr;
259 LocalPointerBase<T>::ptr=src.ptr;
260 src.ptr=NULL;
261 return *this;
262 }
263
264 #ifndef U_HIDE_DRAFT_API
265
273 LocalPointer<T> &operator=(std::unique_ptr<T> &&p) U_NOEXCEPT {
274 adoptInstead(p.release());
275 return *this;
276 }
277 #endif /* U_HIDE_DRAFT_API */
278
284 void swap(LocalPointer<T> &other) U_NOEXCEPT {
285 T *temp=LocalPointerBase<T>::ptr;
286 LocalPointerBase<T>::ptr=other.ptr;
287 other.ptr=temp;
288 }
295 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
296 p1.swap(p2);
297 }
304 void adoptInstead(T *p) {
305 delete LocalPointerBase<T>::ptr;
306 LocalPointerBase<T>::ptr=p;
307 }
323 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
324 if(U_SUCCESS(errorCode)) {
325 delete LocalPointerBase<T>::ptr;
326 LocalPointerBase<T>::ptr=p;
327 if(p==NULL) {
328 errorCode=U_MEMORY_ALLOCATION_ERROR;
329 }
330 } else {
331 delete p;
332 }
333 }
334
335 #ifndef U_HIDE_DRAFT_API
336
347 operator std::unique_ptr<T> () && {
348 return std::unique_ptr<T>(LocalPointerBase<T>::orphan());
349 }
350 #endif /* U_HIDE_DRAFT_API */
351 };
352
371 template<typename T>
372 class LocalArray : public LocalPointerBase<T> {
373 public:
374 using LocalPointerBase<T>::operator*;
375 using LocalPointerBase<T>::operator->;
381 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
395 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
396 if(p==NULL && U_SUCCESS(errorCode)) {
397 errorCode=U_MEMORY_ALLOCATION_ERROR;
398 }
399 }
405 LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
406 src.ptr=NULL;
407 }
408
409 #ifndef U_HIDE_DRAFT_API
410
420 explicit LocalArray(std::unique_ptr<T[]> &&p)
421 : LocalPointerBase<T>(p.release()) {}
422 #endif /* U_HIDE_DRAFT_API */
423
428 ~LocalArray() {
429 delete[] LocalPointerBase<T>::ptr;
430 }
438 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
439 delete[] LocalPointerBase<T>::ptr;
440 LocalPointerBase<T>::ptr=src.ptr;
441 src.ptr=NULL;
442 return *this;
443 }
444
445 #ifndef U_HIDE_DRAFT_API
446
454 LocalArray<T> &operator=(std::unique_ptr<T[]> &&p) U_NOEXCEPT {
455 adoptInstead(p.release());
456 return *this;
457 }
458 #endif /* U_HIDE_DRAFT_API */
459
465 void swap(LocalArray<T> &other) U_NOEXCEPT {
466 T *temp=LocalPointerBase<T>::ptr;
467 LocalPointerBase<T>::ptr=other.ptr;
468 other.ptr=temp;
469 }
476 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
477 p1.swap(p2);
478 }
485 void adoptInstead(T *p) {
486 delete[] LocalPointerBase<T>::ptr;
487 LocalPointerBase<T>::ptr=p;
488 }
504 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
505 if(U_SUCCESS(errorCode)) {
506 delete[] LocalPointerBase<T>::ptr;
507 LocalPointerBase<T>::ptr=p;
508 if(p==NULL) {
509 errorCode=U_MEMORY_ALLOCATION_ERROR;
510 }
511 } else {
512 delete[] p;
513 }
514 }
522 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
523
524 #ifndef U_HIDE_DRAFT_API
525
536 operator std::unique_ptr<T[]> () && {
537 return std::unique_ptr<T[]>(LocalPointerBase<T>::orphan());
538 }
539 #endif /* U_HIDE_DRAFT_API */
540 };
541
562 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
563 class LocalPointerClassName : public LocalPointerBase<Type> { \
564 public: \
565 using LocalPointerBase<Type>::operator*; \
566 using LocalPointerBase<Type>::operator->; \
567 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
568 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
569 : LocalPointerBase<Type>(src.ptr) { \
570 src.ptr=NULL; \
571 } \
572 /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */ \
573 explicit LocalPointerClassName(std::unique_ptr<Type, decltype(&closeFunction)> &&p) \
574 : LocalPointerBase<Type>(p.release()) {} \
575 ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
576 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
577 if (ptr != NULL) { closeFunction(ptr); } \
578 LocalPointerBase<Type>::ptr=src.ptr; \
579 src.ptr=NULL; \
580 return *this; \
581 } \
582 /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */ \
583 LocalPointerClassName &operator=(std::unique_ptr<Type, decltype(&closeFunction)> &&p) { \
584 adoptInstead(p.release()); \
585 return *this; \
586 } \
587 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
588 Type *temp=LocalPointerBase<Type>::ptr; \
589 LocalPointerBase<Type>::ptr=other.ptr; \
590 other.ptr=temp; \
591 } \
592 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
593 p1.swap(p2); \
594 } \
595 void adoptInstead(Type *p) { \
596 if (ptr != NULL) { closeFunction(ptr); } \
597 ptr=p; \
598 } \
599 operator std::unique_ptr<Type, decltype(&closeFunction)> () && { \
600 return std::unique_ptr<Type, decltype(&closeFunction)>(LocalPointerBase<Type>::orphan(), closeFunction); \
601 } \
602 }
603
604 U_NAMESPACE_END
605
606 #endif /* U_SHOW_CPLUSPLUS_API */
607 #endif /* __LOCALPOINTER_H__ */
icu::LocalPointer::operator=
LocalPointer< T > & operator=(LocalPointer< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:257
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:504
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:522
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:122
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:304
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:704
icu::LocalPointer::LocalPointer
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:214
icu::LocalPointer::LocalPointer
LocalPointer(std::unique_ptr< T > &&p)
Constructs a LocalPointer from a C++11 std::unique_ptr.
Definition: localpointer.h:239
icu::LocalArray::LocalArray
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:381
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::LocalArray::LocalArray
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:395
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:284
icu::LocalPointer::operator=
LocalPointer< T > & operator=(std::unique_ptr< T > &&p) U_NOEXCEPT
Move-assign from an std::unique_ptr to this LocalPointer.
Definition: localpointer.h:273
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition: localpointer.h:162
U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:457
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:116
icu::LocalPointer::LocalPointer
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:200
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:295
icu::LocalPointerBase::orphan
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:141
icu::LocalPointerBase::LocalPointerBase
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:82
icu::LocalPointerBase
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:68
icu::LocalPointerBase::isNull
UBool isNull() const
NULL check.
Definition: localpointer.h:94
icu::LocalPointer::LocalPointer
LocalPointer(LocalPointer< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:224
icu::LocalArray::LocalArray
LocalArray(std::unique_ptr< T[]> &&p)
Constructs a LocalArray from a C++11 std::unique_ptr of an array type.
Definition: localpointer.h:420
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:251
icu::LocalArray::swap
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) U_NOEXCEPT
Non-member LocalArray swap function.
Definition: localpointer.h:476
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:372
icu::LocalArray::LocalArray
LocalArray(LocalArray< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:405
icu::LocalPointer::~LocalPointer
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:247
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
icu::LocalArray::operator=
LocalArray< T > & operator=(LocalArray< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:438
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:485
icu::LocalPointerBase::isValid
UBool isValid() const
NULL check.
Definition: localpointer.h:100
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:108
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::LocalArray::swap
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:465
icu::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:428
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition: localpointer.h:134
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:323
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition: localpointer.h:128
icu::LocalPointerBase::~LocalPointerBase
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:88
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::LocalPointerBase::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:153
icu::LocalArray::operator=
LocalArray< T > & operator=(std::unique_ptr< T[]> &&p) U_NOEXCEPT
Move-assign from an std::unique_ptr to this LocalPointer.
Definition: localpointer.h:454
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261