ICU 75.1 75.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=nullptr) : ptr(p) {}
88 ~LocalPointerBase() { /* delete ptr; */ }
94 UBool isNull() const { return ptr==nullptr; }
100 UBool isValid() const { return ptr!=nullptr; }
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=nullptr;
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) = delete;
166 bool operator!=(const LocalPointerBase<T> &other) = delete;
167 // No ownership sharing: No copy constructor, no assignment operator.
168 LocalPointerBase(const LocalPointerBase<T> &other) = delete;
169 void operator=(const LocalPointerBase<T> &other) = delete;
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=nullptr) : LocalPointerBase<T>(p) {}
214 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
215 if(p==nullptr && U_SUCCESS(errorCode)) {
216 errorCode=U_MEMORY_ALLOCATION_ERROR;
217 }
218 }
224 LocalPointer(LocalPointer<T> &&src) noexcept : LocalPointerBase<T>(src.ptr) {
225 src.ptr=nullptr;
226 }
227
238 explicit LocalPointer(std::unique_ptr<T> &&p)
239 : LocalPointerBase<T>(p.release()) {}
240
245 ~LocalPointer() {
246 delete LocalPointerBase<T>::ptr;
247 }
255 LocalPointer<T> &operator=(LocalPointer<T> &&src) noexcept {
256 delete LocalPointerBase<T>::ptr;
257 LocalPointerBase<T>::ptr=src.ptr;
258 src.ptr=nullptr;
259 return *this;
260 }
261
270 LocalPointer<T> &operator=(std::unique_ptr<T> &&p) noexcept {
271 adoptInstead(p.release());
272 return *this;
273 }
274
280 void swap(LocalPointer<T> &other) noexcept {
281 T *temp=LocalPointerBase<T>::ptr;
282 LocalPointerBase<T>::ptr=other.ptr;
283 other.ptr=temp;
284 }
291 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) noexcept {
292 p1.swap(p2);
293 }
300 void adoptInstead(T *p) {
301 delete LocalPointerBase<T>::ptr;
302 LocalPointerBase<T>::ptr=p;
303 }
319 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
320 if(U_SUCCESS(errorCode)) {
321 delete LocalPointerBase<T>::ptr;
322 LocalPointerBase<T>::ptr=p;
323 if(p==nullptr) {
324 errorCode=U_MEMORY_ALLOCATION_ERROR;
325 }
326 } else {
327 delete p;
328 }
329 }
330
342 operator std::unique_ptr<T> () && {
343 return std::unique_ptr<T>(LocalPointerBase<T>::orphan());
344 }
345 };
346
365 template<typename T>
366 class LocalArray : public LocalPointerBase<T> {
367 public:
368 using LocalPointerBase<T>::operator*;
369 using LocalPointerBase<T>::operator->;
375 explicit LocalArray(T *p=nullptr) : LocalPointerBase<T>(p) {}
389 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
390 if(p==nullptr && U_SUCCESS(errorCode)) {
391 errorCode=U_MEMORY_ALLOCATION_ERROR;
392 }
393 }
399 LocalArray(LocalArray<T> &&src) noexcept : LocalPointerBase<T>(src.ptr) {
400 src.ptr=nullptr;
401 }
402
413 explicit LocalArray(std::unique_ptr<T[]> &&p)
414 : LocalPointerBase<T>(p.release()) {}
415
420 ~LocalArray() {
421 delete[] LocalPointerBase<T>::ptr;
422 }
430 LocalArray<T> &operator=(LocalArray<T> &&src) noexcept {
431 delete[] LocalPointerBase<T>::ptr;
432 LocalPointerBase<T>::ptr=src.ptr;
433 src.ptr=nullptr;
434 return *this;
435 }
436
445 LocalArray<T> &operator=(std::unique_ptr<T[]> &&p) noexcept {
446 adoptInstead(p.release());
447 return *this;
448 }
449
455 void swap(LocalArray<T> &other) noexcept {
456 T *temp=LocalPointerBase<T>::ptr;
457 LocalPointerBase<T>::ptr=other.ptr;
458 other.ptr=temp;
459 }
466 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) noexcept {
467 p1.swap(p2);
468 }
475 void adoptInstead(T *p) {
476 delete[] LocalPointerBase<T>::ptr;
477 LocalPointerBase<T>::ptr=p;
478 }
494 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
495 if(U_SUCCESS(errorCode)) {
496 delete[] LocalPointerBase<T>::ptr;
497 LocalPointerBase<T>::ptr=p;
498 if(p==nullptr) {
499 errorCode=U_MEMORY_ALLOCATION_ERROR;
500 }
501 } else {
502 delete[] p;
503 }
504 }
512 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
513
525 operator std::unique_ptr<T[]> () && {
526 return std::unique_ptr<T[]>(LocalPointerBase<T>::orphan());
527 }
528 };
529
550 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
551 using LocalPointerClassName = internal::LocalOpenPointer<Type, closeFunction>
552
553 #ifndef U_IN_DOXYGEN
554 namespace internal {
561 template <typename Type, auto closeFunction>
562 class LocalOpenPointer : public LocalPointerBase<Type> {
563 using LocalPointerBase<Type>::ptr;
564 public:
565 using LocalPointerBase<Type>::operator*;
566 using LocalPointerBase<Type>::operator->;
567 explicit LocalOpenPointer(Type *p=nullptr) : LocalPointerBase<Type>(p) {}
568 LocalOpenPointer(LocalOpenPointer &&src) noexcept
569 : LocalPointerBase<Type>(src.ptr) {
570 src.ptr=nullptr;
571 }
572 /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */
573 explicit LocalOpenPointer(std::unique_ptr<Type, decltype(closeFunction)> &&p)
574 : LocalPointerBase<Type>(p.release()) {}
575 ~LocalOpenPointer() { if (ptr != nullptr) { closeFunction(ptr); } }
576 LocalOpenPointer &operator=(LocalOpenPointer &&src) noexcept {
577 if (ptr != nullptr) { closeFunction(ptr); }
578 LocalPointerBase<Type>::ptr=src.ptr;
579 src.ptr=nullptr;
580 return *this;
581 }
582 /* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */
583 LocalOpenPointer &operator=(std::unique_ptr<Type, decltype(closeFunction)> &&p) {
584 adoptInstead(p.release());
585 return *this;
586 }
587 void swap(LocalOpenPointer &other) noexcept {
588 Type *temp=LocalPointerBase<Type>::ptr;
589 LocalPointerBase<Type>::ptr=other.ptr;
590 other.ptr=temp;
591 }
592 friend inline void swap(LocalOpenPointer &p1, LocalOpenPointer &p2) noexcept {
593 p1.swap(p2);
594 }
595 void adoptInstead(Type *p) {
596 if (ptr != nullptr) { 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 } // namespace internal
604 #endif
605
606 U_NAMESPACE_END
607
608 #endif /* U_SHOW_CPLUSPLUS_API */
609 #endif /* __LOCALPOINTER_H__ */
icu::LocalArray
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:366
icu::LocalArray::LocalArray
LocalArray(T *p=nullptr)
Constructor takes ownership.
Definition: localpointer.h:375
icu::LocalArray::operator=
LocalArray< T > & operator=(LocalArray< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:430
icu::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:420
icu::LocalArray::LocalArray
LocalArray(LocalArray< T > &&src) noexcept
Move constructor, leaves src with isNull().
Definition: localpointer.h:399
icu::LocalArray::swap
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) noexcept
Non-member LocalArray swap function.
Definition: localpointer.h:466
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:494
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:512
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:475
icu::LocalArray::LocalArray
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
Definition: localpointer.h:389
icu::LocalArray::swap
void swap(LocalArray< T > &other) noexcept
Swap pointers.
Definition: localpointer.h:455
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:413
icu::LocalArray::operator=
LocalArray< T > & operator=(std::unique_ptr< T[]> &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
Definition: localpointer.h:445
icu::LocalPointerBase
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:68
icu::LocalPointerBase::operator!=
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=nullptr need not be changed.
Definition: localpointer.h:116
icu::LocalPointerBase::isValid
UBool isValid() const
nullptr check.
Definition: localpointer.h:100
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:122
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition: localpointer.h:128
icu::LocalPointerBase::orphan
T * orphan()
Gives up ownership; the internal pointer becomes nullptr.
Definition: localpointer.h:141
icu::LocalPointerBase::isNull
UBool isNull() const
nullptr check.
Definition: localpointer.h:94
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition: localpointer.h:134
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition: localpointer.h:162
icu::LocalPointerBase::LocalPointerBase
LocalPointerBase(T *p=nullptr)
Constructor takes ownership.
Definition: localpointer.h:82
icu::LocalPointerBase::operator==
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==nullptr need not be changed.
Definition: localpointer.h:108
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::LocalPointerBase::~LocalPointerBase
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:88
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) noexcept
Non-member LocalPointer swap function.
Definition: localpointer.h:291
icu::LocalPointer::operator=
LocalPointer< T > & operator=(LocalPointer< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:255
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:319
icu::LocalPointer::operator=
LocalPointer< T > & operator=(std::unique_ptr< T > &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
Definition: localpointer.h:270
icu::LocalPointer::LocalPointer
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
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:238
icu::LocalPointer::LocalPointer
LocalPointer(T *p=nullptr)
Constructor takes ownership.
Definition: localpointer.h:200
icu::LocalPointer::swap
void swap(LocalPointer< T > &other) noexcept
Swap pointers.
Definition: localpointer.h:280
icu::LocalPointer::~LocalPointer
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:245
icu::LocalPointer::LocalPointer
LocalPointer(LocalPointer< T > &&src) noexcept
Move constructor, leaves src with isNull().
Definition: localpointer.h:224
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:300
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_MEMORY_ALLOCATION_ERROR
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition: utypes.h:458
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:728