localpointer.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2009-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: localpointer.h
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2009nov13
* created by: Markus W. Scherer
*/
#ifndef __LOCALPOINTER_H__
#define __LOCALPOINTER_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include <memory>
U_NAMESPACE_BEGIN
template<typename T>
class LocalPointerBase {
public:
// No heap allocation. Use only on the stack.
static void* U_EXPORT2 operator new(size_t) = delete;
static void* U_EXPORT2 operator new[](size_t) = delete;
#if U_HAVE_PLACEMENT_NEW
static void* U_EXPORT2 operator new(size_t, void*) = delete;
#endif
explicit LocalPointerBase(T *p=nullptr) : ptr(p) {}
~LocalPointerBase() { /* delete ptr; */ }
UBool isNull() const { return ptr==nullptr; }
UBool isValid() const { return ptr!=nullptr; }
bool operator==(const T *other) const { return ptr==other; }
bool operator!=(const T *other) const { return ptr!=other; }
T *getAlias() const { return ptr; }
T &operator*() const { return *ptr; }
T *operator->() const { return ptr; }
T *orphan() {
T *p=ptr;
ptr=nullptr;
return p;
}
void adoptInstead(T *p) {
// delete ptr;
ptr=p;
}
protected:
T *ptr;
private:
// No comparison operators with other LocalPointerBases.
bool operator==(const LocalPointerBase<T> &other) = delete;
bool operator!=(const LocalPointerBase<T> &other) = delete;
// No ownership sharing: No copy constructor, no assignment operator.
LocalPointerBase(const LocalPointerBase<T> &other) = delete;
void operator=(const LocalPointerBase<T> &other) = delete;
};
template<typename T>
class LocalPointer : public LocalPointerBase<T> {
public:
using LocalPointerBase<T>::operator*;
using LocalPointerBase<T>::operator->;
explicit LocalPointer(T *p=nullptr) : LocalPointerBase<T>(p) {}
LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
if(p==nullptr && U_SUCCESS(errorCode)) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
}
LocalPointer(LocalPointer<T> &&src) noexcept : LocalPointerBase<T>(src.ptr) {
src.ptr=nullptr;
}
explicit LocalPointer(std::unique_ptr<T> &&p)
: LocalPointerBase<T>(p.release()) {}
~LocalPointer() {
delete LocalPointerBase<T>::ptr;
}
LocalPointer<T> &operator=(LocalPointer<T> &&src) noexcept {
delete LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=src.ptr;
src.ptr=nullptr;
return *this;
}
LocalPointer<T> &operator=(std::unique_ptr<T> &&p) noexcept {
adoptInstead(p.release());
return *this;
}
void swap(LocalPointer<T> &other) noexcept {
T *temp=LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=other.ptr;
other.ptr=temp;
}
friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) noexcept {
p1.swap(p2);
}
void adoptInstead(T *p) {
delete LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=p;
}
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
if(U_SUCCESS(errorCode)) {
delete LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=p;
if(p==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
} else {
delete p;
}
}
operator std::unique_ptr<T> () && {
return std::unique_ptr<T>(LocalPointerBase<T>::orphan());
}
};
template<typename T>
class LocalArray : public LocalPointerBase<T> {
public:
using LocalPointerBase<T>::operator*;
using LocalPointerBase<T>::operator->;
explicit LocalArray(T *p=nullptr) : LocalPointerBase<T>(p) {}
LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
if(p==nullptr && U_SUCCESS(errorCode)) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
}
LocalArray(LocalArray<T> &&src) noexcept : LocalPointerBase<T>(src.ptr) {
src.ptr=nullptr;
}
explicit LocalArray(std::unique_ptr<T[]> &&p)
: LocalPointerBase<T>(p.release()) {}
~LocalArray() {
delete[] LocalPointerBase<T>::ptr;
}
LocalArray<T> &operator=(LocalArray<T> &&src) noexcept {
delete[] LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=src.ptr;
src.ptr=nullptr;
return *this;
}
LocalArray<T> &operator=(std::unique_ptr<T[]> &&p) noexcept {
adoptInstead(p.release());
return *this;
}
void swap(LocalArray<T> &other) noexcept {
T *temp=LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=other.ptr;
other.ptr=temp;
}
friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) noexcept {
p1.swap(p2);
}
void adoptInstead(T *p) {
delete[] LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=p;
}
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
if(U_SUCCESS(errorCode)) {
delete[] LocalPointerBase<T>::ptr;
LocalPointerBase<T>::ptr=p;
if(p==nullptr) {
errorCode=U_MEMORY_ALLOCATION_ERROR;
}
} else {
delete[] p;
}
}
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
operator std::unique_ptr<T[]> () && {
return std::unique_ptr<T[]>(LocalPointerBase<T>::orphan());
}
};
#define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
using LocalPointerClassName = internal::LocalOpenPointer<Type, closeFunction>
#ifndef U_IN_DOXYGEN
namespace internal {
template <typename Type, auto closeFunction>
class LocalOpenPointer : public LocalPointerBase<Type> {
using LocalPointerBase<Type>::ptr;
public:
using LocalPointerBase<Type>::operator*;
using LocalPointerBase<Type>::operator->;
explicit LocalOpenPointer(Type *p=nullptr) : LocalPointerBase<Type>(p) {}
LocalOpenPointer(LocalOpenPointer &&src) noexcept
: LocalPointerBase<Type>(src.ptr) {
src.ptr=nullptr;
}
/* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */
explicit LocalOpenPointer(std::unique_ptr<Type, decltype(closeFunction)> &&p)
: LocalPointerBase<Type>(p.release()) {}
~LocalOpenPointer() { if (ptr != nullptr) { closeFunction(ptr); } }
LocalOpenPointer &operator=(LocalOpenPointer &&src) noexcept {
if (ptr != nullptr) { closeFunction(ptr); }
LocalPointerBase<Type>::ptr=src.ptr;
src.ptr=nullptr;
return *this;
}
/* TODO: Be agnostic of the deleter function signature from the user-provided std::unique_ptr? */
LocalOpenPointer &operator=(std::unique_ptr<Type, decltype(closeFunction)> &&p) {
adoptInstead(p.release());
return *this;
}
void swap(LocalOpenPointer &other) noexcept {
Type *temp=LocalPointerBase<Type>::ptr;
LocalPointerBase<Type>::ptr=other.ptr;
other.ptr=temp;
}
friend inline void swap(LocalOpenPointer &p1, LocalOpenPointer &p2) noexcept {
p1.swap(p2);
}
void adoptInstead(Type *p) {
if (ptr != nullptr) { closeFunction(ptr); }
ptr=p;
}
operator std::unique_ptr<Type, decltype(closeFunction)> () && {
return std::unique_ptr<Type, decltype(closeFunction)>(LocalPointerBase<Type>::orphan(), closeFunction);
}
};
} // namespace internal
#endif
U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */
#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