19#ifndef __LOCALPOINTER_H__
20#define __LOCALPOINTER_H__
43#if U_SHOW_CPLUSPLUS_API
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;
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>
561template <
typename Type, auto closeFunction>
562class LocalOpenPointer :
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) {
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;
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 friend inline void 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);
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
LocalArray(T *p=nullptr)
Constructor takes ownership.
LocalArray< T > & operator=(std::unique_ptr< T[]> &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
T & operator[](ptrdiff_t i) const
Array item access (writable).
~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.
LocalArray< T > & operator=(LocalArray< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
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.
"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 * orphan()
Gives up ownership; the internal pointer becomes nullptr.
UBool isNull() const
nullptr check.
LocalPointerBase(T *p=nullptr)
Constructor takes ownership.
T & operator*() const
Access without ownership change.
T * operator->() const
Access without ownership change.
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.
T * getAlias() const
Access without ownership change.
~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=(std::unique_ptr< T > &&p) noexcept
Move-assign from an std::unique_ptr to this LocalPointer.
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
LocalPointer< T > & operator=(LocalPointer< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
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?