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