#ifndef CORE_FXCRT_UNOWNED_PTR_H_
#define CORE_FXCRT_UNOWNED_PTR_H_
#include "build/build_config.h"
#include "core/fxcrt/compiler_specific.h"
#if defined(PDF_USE_PARTITION_ALLOC)
#include "partition_alloc/partition_alloc_buildflags.h"
#include "partition_alloc/pointers/raw_ptr.h"
#if !PA_BUILDFLAG(USE_PARTITION_ALLOC)
#error "pdf_use_partition_alloc=true requires use_partition_alloc=true"
#endif
#if PA_BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS) || \
PA_BUILDFLAG(USE_RAW_PTR_ASAN_UNOWNED_IMPL)
#define UNOWNED_PTR_DANGLING_CHECKS
#endif
static_assert …;
static_assert …;
UnownedPtr;
#else
#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>
#include "core/fxcrt/unowned_ptr_exclusion.h"
namespace fxcrt {
template <class T>
class TRIVIAL_ABI GSL_POINTER UnownedPtr {
public:
constexpr UnownedPtr() noexcept = default;
constexpr UnownedPtr(std::nullptr_t ptr) {}
explicit constexpr UnownedPtr(T* pObj) noexcept : m_pObj(pObj) {}
constexpr UnownedPtr(const UnownedPtr& that) noexcept = default;
constexpr UnownedPtr(UnownedPtr&& that) noexcept
: m_pObj(that.ExtractAsDangling()) {}
template <class U,
typename = typename std::enable_if<
std::is_convertible<U*, T*>::value>::type>
UnownedPtr(const UnownedPtr<U>& that) : m_pObj(static_cast<U*>(that)) {}
template <class U,
typename = typename std::enable_if<
std::is_convertible<U*, T*>::value>::type>
UnownedPtr(UnownedPtr<U>&& that) noexcept
: m_pObj(that.ExtractAsDangling()) {}
UnownedPtr& operator=(std::nullptr_t) noexcept {
m_pObj = nullptr;
return *this;
}
UnownedPtr& operator=(T* that) noexcept {
m_pObj = that;
return *this;
}
UnownedPtr& operator=(const UnownedPtr& that) noexcept = default;
UnownedPtr& operator=(UnownedPtr&& that) noexcept {
if (*this != that) {
m_pObj = that.ExtractAsDangling();
}
return *this;
}
template <class U,
typename = typename std::enable_if<
std::is_convertible<U*, T*>::value>::type>
UnownedPtr& operator=(const UnownedPtr<U>& that) noexcept {
if (*this != that) {
m_pObj = static_cast<U*>(that);
}
return *this;
}
template <class U,
typename = typename std::enable_if<
std::is_convertible<U*, T*>::value>::type>
UnownedPtr& operator=(UnownedPtr<U>&& that) noexcept {
if (*this != that) {
m_pObj = that.ExtractAsDangling();
}
return *this;
}
~UnownedPtr() {
m_pObj = nullptr;
}
bool operator==(std::nullptr_t ptr) const { return m_pObj == nullptr; }
bool operator==(const UnownedPtr& that) const {
return m_pObj == static_cast<T*>(that);
}
bool operator<(const UnownedPtr& that) const {
return std::less<T*>()(m_pObj, static_cast<T*>(that));
}
operator T*() const noexcept { return m_pObj; }
T* get() const noexcept { return m_pObj; }
T* ExtractAsDangling() { return std::exchange(m_pObj, nullptr); }
void ClearAndDelete() { delete std::exchange(m_pObj, nullptr); }
explicit operator bool() const { return !!m_pObj; }
T& operator*() const { return *m_pObj; }
T* operator->() const { return m_pObj; }
private:
UNOWNED_PTR_EXCLUSION T* m_pObj = nullptr;
};
}
using fxcrt::UnownedPtr;
#endif
namespace pdfium {
template <typename T>
UnownedPtr<T> WrapUnowned(T* that) { … }
}
#endif