Loading...
Searching...
No Matches
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;
static void* U_EXPORT2 operator new(size_t, void*) = delete;
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::LocalArray
LocalArray(T *p=nullptr)
Constructor takes ownership.
Definition localpointer.h:373
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:443
icu::LocalArray::operator[]
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition localpointer.h:510
icu::LocalArray::~LocalArray
~LocalArray()
Destructor deletes the array it owns.
Definition localpointer.h:418
icu::LocalArray::LocalArray
LocalArray(LocalArray< T > &&src) noexcept
Move constructor, leaves src with isNull().
Definition localpointer.h:397
icu::LocalArray::swap
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) noexcept
Non-member LocalArray swap function.
Definition localpointer.h:464
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:492
icu::LocalArray::operator=
LocalArray< T > & operator=(LocalArray< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
Definition localpointer.h:428
icu::LocalArray::adoptInstead
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition localpointer.h:473
icu::LocalArray::LocalArray
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
Definition localpointer.h:387
icu::LocalArray::swap
void swap(LocalArray< T > &other) noexcept
Swap pointers.
Definition localpointer.h:453
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:411
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:114
icu::LocalPointerBase::isValid
UBool isValid() const
nullptr check.
Definition localpointer.h:98
icu::LocalPointerBase::orphan
T * orphan()
Gives up ownership; the internal pointer becomes nullptr.
Definition localpointer.h:139
icu::LocalPointerBase::isNull
UBool isNull() const
nullptr check.
Definition localpointer.h:92
icu::LocalPointerBase::ptr
T * ptr
Actual pointer.
Definition localpointer.h:160
icu::LocalPointerBase::LocalPointerBase
LocalPointerBase(T *p=nullptr)
Constructor takes ownership.
Definition localpointer.h:80
icu::LocalPointerBase::operator*
T & operator*() const
Access without ownership change.
Definition localpointer.h:126
icu::LocalPointerBase::operator->
T * operator->() const
Access without ownership change.
Definition localpointer.h:132
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:106
icu::LocalPointerBase::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition localpointer.h:151
icu::LocalPointerBase::getAlias
T * getAlias() const
Access without ownership change.
Definition localpointer.h:120
icu::LocalPointerBase::~LocalPointerBase
~LocalPointerBase()
Destructor deletes the object it owns.
Definition localpointer.h:86
icu::LocalPointer::swap
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) noexcept
Non-member LocalPointer swap function.
Definition localpointer.h:289
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:268
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:317
icu::LocalPointer::operator=
LocalPointer< T > & operator=(LocalPointer< T > &&src) noexcept
Move assignment operator, leaves src with isNull().
Definition localpointer.h:253
icu::LocalPointer::LocalPointer
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if nullptr.
Definition localpointer.h:212
icu::LocalPointer::LocalPointer
LocalPointer(std::unique_ptr< T > &&p)
Constructs a LocalPointer from a C++11 std::unique_ptr.
Definition localpointer.h:236
icu::LocalPointer::LocalPointer
LocalPointer(T *p=nullptr)
Constructor takes ownership.
Definition localpointer.h:198
icu::LocalPointer::swap
void swap(LocalPointer< T > &other) noexcept
Swap pointers.
Definition localpointer.h:278
icu::LocalPointer::~LocalPointer
~LocalPointer()
Destructor deletes the object it owns.
Definition localpointer.h:243
icu::LocalPointer::LocalPointer
LocalPointer(LocalPointer< T > &&src) noexcept
Move constructor, leaves src with isNull().
Definition localpointer.h:222
icu::LocalPointer::adoptInstead
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition localpointer.h:298
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
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:509
U_MEMORY_ALLOCATION_ERROR
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition utypes.h:552
U_SUCCESS
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:822