2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-07-24 09:25:27 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <AK/Format.h>
|
2020-06-12 16:30:30 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-07-24 09:25:27 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2020-09-23 19:17:43 +03:00
|
|
|
template<typename T, typename PtrTraits>
|
2019-07-24 09:25:27 +03:00
|
|
|
class RefPtr;
|
|
|
|
template<typename T>
|
|
|
|
class NonnullRefPtr;
|
|
|
|
template<typename T>
|
|
|
|
class WeakPtr;
|
|
|
|
|
|
|
|
template<typename T>
|
2020-05-16 11:51:21 +03:00
|
|
|
class NonnullOwnPtr {
|
2019-07-24 09:25:27 +03:00
|
|
|
public:
|
2020-11-12 01:21:01 +03:00
|
|
|
using ElementType = T;
|
2019-07-25 12:10:28 +03:00
|
|
|
|
2019-07-24 09:25:27 +03:00
|
|
|
enum AdoptTag { Adopt };
|
|
|
|
|
|
|
|
NonnullOwnPtr(AdoptTag, T& ptr)
|
|
|
|
: m_ptr(&ptr)
|
|
|
|
{
|
2020-11-03 17:51:56 +03:00
|
|
|
static_assert(
|
|
|
|
requires { requires typename T::AllowOwnPtr()(); } || !requires(T obj) { requires !typename T::AllowOwnPtr()(); obj.ref(); obj.unref(); },
|
|
|
|
"Use NonnullRefPtr<> for RefCounted types");
|
2019-07-24 09:25:27 +03:00
|
|
|
}
|
|
|
|
NonnullOwnPtr(NonnullOwnPtr&& other)
|
|
|
|
: m_ptr(other.leak_ptr())
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_ptr);
|
2019-07-24 09:25:27 +03:00
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr(NonnullOwnPtr<U>&& other)
|
2020-04-05 12:32:30 +03:00
|
|
|
: m_ptr(other.leak_ptr())
|
2019-07-24 09:25:27 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_ptr);
|
2019-07-24 09:25:27 +03:00
|
|
|
}
|
|
|
|
~NonnullOwnPtr()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
#ifdef SANITIZE_PTRS
|
|
|
|
if constexpr (sizeof(T*) == 8)
|
|
|
|
m_ptr = (T*)(0xe3e3e3e3e3e3e3e3);
|
|
|
|
else
|
|
|
|
m_ptr = (T*)(0xe3e3e3e3);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullOwnPtr(const NonnullOwnPtr&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr(const NonnullOwnPtr<U>&) = delete;
|
|
|
|
NonnullOwnPtr& operator=(const NonnullOwnPtr&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr& operator=(const NonnullOwnPtr<U>&) = delete;
|
|
|
|
|
2020-09-23 19:17:43 +03:00
|
|
|
template<typename U, typename PtrTraits = RefPtrTraits<U>>
|
|
|
|
NonnullOwnPtr(const RefPtr<U, PtrTraits>&) = delete;
|
2019-07-24 09:25:27 +03:00
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr(const NonnullRefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr(const WeakPtr<U>&) = delete;
|
2020-09-23 19:17:43 +03:00
|
|
|
template<typename U, typename PtrTraits = RefPtrTraits<U>>
|
|
|
|
NonnullOwnPtr& operator=(const RefPtr<U, PtrTraits>&) = delete;
|
2019-07-24 09:25:27 +03:00
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr& operator=(const NonnullRefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr& operator=(const WeakPtr<U>&) = delete;
|
|
|
|
|
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr&& other)
|
|
|
|
{
|
2020-01-19 18:03:57 +03:00
|
|
|
NonnullOwnPtr ptr(move(other));
|
|
|
|
swap(ptr);
|
2019-07-24 09:25:27 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr<U>&& other)
|
|
|
|
{
|
2020-01-19 18:03:57 +03:00
|
|
|
NonnullOwnPtr ptr(move(other));
|
|
|
|
swap(ptr);
|
2019-07-24 09:25:27 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:51:36 +03:00
|
|
|
[[nodiscard]] T* leak_ptr()
|
2019-07-24 09:25:27 +03:00
|
|
|
{
|
|
|
|
return exchange(m_ptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
T* ptr() { return m_ptr; }
|
|
|
|
const T* ptr() const { return m_ptr; }
|
|
|
|
|
|
|
|
T* operator->() { return m_ptr; }
|
|
|
|
const T* operator->() const { return m_ptr; }
|
|
|
|
|
|
|
|
T& operator*() { return *m_ptr; }
|
|
|
|
const T& operator*() const { return *m_ptr; }
|
|
|
|
|
|
|
|
operator const T*() const { return m_ptr; }
|
|
|
|
operator T*() { return m_ptr; }
|
|
|
|
|
2019-11-07 20:00:05 +03:00
|
|
|
operator bool() const = delete;
|
|
|
|
bool operator!() const = delete;
|
|
|
|
|
2020-01-19 18:03:57 +03:00
|
|
|
void swap(NonnullOwnPtr& other)
|
|
|
|
{
|
|
|
|
::swap(m_ptr, other.m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
void swap(NonnullOwnPtr<U>& other)
|
|
|
|
{
|
|
|
|
::swap(m_ptr, other.m_ptr);
|
|
|
|
}
|
|
|
|
|
2020-05-08 22:38:47 +03:00
|
|
|
template<typename U>
|
|
|
|
NonnullOwnPtr<U> release_nonnull()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_ptr);
|
2020-05-08 22:38:47 +03:00
|
|
|
return NonnullOwnPtr<U>(NonnullOwnPtr<U>::Adopt, static_cast<U&>(*leak_ptr()));
|
|
|
|
}
|
|
|
|
|
2019-07-24 09:25:27 +03:00
|
|
|
private:
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
if (!m_ptr)
|
|
|
|
return;
|
|
|
|
delete m_ptr;
|
|
|
|
m_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
2020-04-01 22:04:10 +03:00
|
|
|
template<typename T>
|
|
|
|
inline NonnullOwnPtr<T> adopt_own(T& object)
|
|
|
|
{
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, object);
|
|
|
|
}
|
|
|
|
|
2019-07-24 09:25:27 +03:00
|
|
|
template<class T, class... Args>
|
|
|
|
inline NonnullOwnPtr<T>
|
|
|
|
make(Args&&... args)
|
|
|
|
{
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T(forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Traits<NonnullOwnPtr<T>> : public GenericTraits<NonnullOwnPtr<T>> {
|
2019-08-14 12:47:38 +03:00
|
|
|
using PeekType = const T*;
|
2019-08-14 22:21:15 +03:00
|
|
|
static unsigned hash(const NonnullOwnPtr<T>& p) { return int_hash((u32)p.ptr()); }
|
2019-07-24 09:25:27 +03:00
|
|
|
static bool equals(const NonnullOwnPtr<T>& a, const NonnullOwnPtr<T>& b) { return a.ptr() == b.ptr(); }
|
|
|
|
};
|
|
|
|
|
2020-01-19 18:03:57 +03:00
|
|
|
template<typename T, typename U>
|
|
|
|
inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:24:01 +03:00
|
|
|
template<typename T>
|
|
|
|
struct Formatter<NonnullOwnPtr<T>> : Formatter<const T*> {
|
2020-12-30 14:14:15 +03:00
|
|
|
void format(FormatBuilder& builder, const NonnullOwnPtr<T>& value)
|
2020-10-15 16:24:01 +03:00
|
|
|
{
|
2020-12-30 14:14:15 +03:00
|
|
|
Formatter<const T*>::format(builder, value.ptr());
|
2020-10-15 16:24:01 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-24 09:25:27 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 22:04:10 +03:00
|
|
|
using AK::adopt_own;
|
2019-07-24 09:25:27 +03:00
|
|
|
using AK::make;
|
|
|
|
using AK::NonnullOwnPtr;
|