1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 4 ******************************************************************************* 6 * Copyright (C) 2009-2016, International Business Machines 7 * Corporation and others. All Rights Reserved. 9 ******************************************************************************* 10 * file name: localpointer.h 12 * tab size: 8 (not used) 15 * created on: 2009nov13 16 * created by: Markus W. Scherer 19 #ifndef __LOCALPOINTER_H__ 20 #define __LOCALPOINTER_H__ 43 #if U_SHOW_CPLUSPLUS_API 70 // No heap allocation. Use only on the stack. 71 staticvoid* U_EXPORT2
operatornew(size_t) =
delete;
72 staticvoid* U_EXPORT2
operatornew[](size_t) =
delete;
73 #if U_HAVE_PLACEMENT_NEW 74 staticvoid* U_EXPORT2
operatornew(size_t,
void*) =
delete;
164 // No comparison operators with other LocalPointerBases. 167 // No ownership sharing: No copy constructor, no assignment operator. 271 adoptInstead(p.release());
342 operator std::unique_ptr<T> () && {
446 adoptInstead(p.release());
525 operator std::unique_ptr<T[]> () && {
550 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \ 551 using LocalPointerClassName = internal::LocalOpenPointer<Type, closeFunction> 561 template <
typename Type, auto closeFunction>
562 classLocalOpenPointer :
public LocalPointerBase<Type> {
563 using LocalPointerBase<Type>::ptr;
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) {
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;
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());
587 void swap(LocalOpenPointer &other) noexcept {
588 Type *temp=LocalPointerBase<Type>::ptr;
589 LocalPointerBase<Type>::ptr=other.ptr;
592 friendinlinevoid swap(LocalOpenPointer &p1, LocalOpenPointer &p2) noexcept {
595 void adoptInstead(Type *p) {
596 if (ptr !=
nullptr) { closeFunction(ptr); }
599 operator std::unique_ptr<Type, decltype(closeFunction)> () && {
600 return std::unique_ptr<Type, decltype(closeFunction)>(LocalPointerBase<Type>::orphan(), closeFunction);
603 }
// namespace internal 608 #endif/* U_SHOW_CPLUSPLUS_API */ 609 #endif/* __LOCALPOINTER_H__ */ "Smart pointer" class, deletes objects via the C++ array delete[] operator.
LocalArray(T *p=nullptr)
Constructor takes ownership.
LocalArray< T > & operator=(LocalArray< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
~LocalArray()
Destructor deletes the array it owns.
LocalArray(LocalArray< T > &&src) noexcept
Move constructor, leaves src with isNull().
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) noexcept
Non-member LocalArray swap function.
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
T & operator[](ptrdiff_t i) const
Array item access (writable).
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
void swap(LocalArray< T > &other) noexcept
Swap pointers.
LocalArray(std::unique_ptr< T[]> &&p)
Constructs a LocalArray from a C++11 std::unique_ptr of an array type.
LocalArray< T > & operator=(std::unique_ptr< T[]> &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
"Smart pointer" base class; do not use directly: use LocalPointer etc.
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=nullptr need not be changed.
UBool isValid() const
nullptr check.
T * getAlias() const
Access without ownership change.
T & operator*() const
Access without ownership change.
T * orphan()
Gives up ownership; the internal pointer becomes nullptr.
UBool isNull() const
nullptr check.
T * operator->() const
Access without ownership change.
LocalPointerBase(T *p=nullptr)
Constructor takes ownership.
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==nullptr need not be changed.
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
~LocalPointerBase()
Destructor deletes the object it owns.
"Smart pointer" class, deletes objects via the standard C++ delete operator.
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) noexcept
Non-member LocalPointer swap function.
LocalPointer< T > & operator=(LocalPointer< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
LocalPointer< T > & operator=(std::unique_ptr< T > &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
LocalPointer(std::unique_ptr< T > &&p)
Constructs a LocalPointer from a C++11 std::unique_ptr.
LocalPointer(T *p=nullptr)
Constructor takes ownership.
void swap(LocalPointer< T > &other) noexcept
Swap pointers.
~LocalPointer()
Destructor deletes the object it owns.
LocalPointer(LocalPointer< T > &&src) noexcept
Move constructor, leaves src with isNull().
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
#define U_SUCCESS(x)
Does the error code indicate success?