2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <AK/Format.h>
|
2020-06-12 10:30:30 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-07-24 03:25:27 -03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class WeakPtr;
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2021-12-01 18:05:13 -03:00
|
|
|
class [[nodiscard]] NonnullOwnPtr {
|
2019-07-24 03:25:27 -03:00
|
|
|
public:
|
2020-11-11 19:21:01 -03:00
|
|
|
using ElementType = T;
|
2019-07-25 06:10:28 -03:00
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
enum AdoptTag { Adopt };
|
|
|
|
|
|
|
|
|
|
NonnullOwnPtr(AdoptTag, T& ptr)
|
|
|
|
|
: m_ptr(&ptr)
|
|
|
|
|
{
|
2020-11-03 11:51:56 -03:00
|
|
|
static_assert(
|
2021-06-25 02:33:15 -03:00
|
|
|
requires { requires typename T::AllowOwnPtr()(); } || !requires { requires !typename T::AllowOwnPtr()(); declval<T>().ref(); declval<T>().unref(); },
|
2020-11-03 11:51:56 -03:00
|
|
|
"Use NonnullRefPtr<> for RefCounted types");
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
NonnullOwnPtr(NonnullOwnPtr&& other)
|
|
|
|
|
: m_ptr(other.leak_ptr())
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_ptr);
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
template<typename U>
|
|
|
|
|
NonnullOwnPtr(NonnullOwnPtr<U>&& other)
|
2020-04-05 06:32:30 -03:00
|
|
|
: m_ptr(other.leak_ptr())
|
2019-07-24 03:25:27 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_ptr);
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
~NonnullOwnPtr()
|
|
|
|
|
{
|
2026-06-13 15:17:02 -03:00
|
|
|
delete m_ptr;
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr(NonnullOwnPtr const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr(NonnullOwnPtr<U> const&) = delete;
|
|
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
|
2022-05-07 07:50:54 -03:00
|
|
|
template<typename U>
|
|
|
|
|
NonnullOwnPtr(RefPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr(NonnullRefPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr(WeakPtr<U> const&) = delete;
|
2022-05-07 07:50:54 -03:00
|
|
|
template<typename U>
|
|
|
|
|
NonnullOwnPtr& operator=(RefPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr& operator=(NonnullRefPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename U>
|
2022-04-01 14:58:27 -03:00
|
|
|
NonnullOwnPtr& operator=(WeakPtr<U> const&) = delete;
|
2019-07-24 03:25:27 -03:00
|
|
|
|
|
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr&& other)
|
|
|
|
|
{
|
2020-01-19 12:03:57 -03:00
|
|
|
NonnullOwnPtr ptr(move(other));
|
|
|
|
|
swap(ptr);
|
2019-07-24 03:25:27 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
|
NonnullOwnPtr& operator=(NonnullOwnPtr<U>&& other)
|
|
|
|
|
{
|
2020-01-19 12:03:57 -03:00
|
|
|
NonnullOwnPtr ptr(move(other));
|
|
|
|
|
swap(ptr);
|
2019-07-24 03:25:27 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 10:51:36 -03:00
|
|
|
[[nodiscard]] T* leak_ptr()
|
2019-07-24 03:25:27 -03:00
|
|
|
{
|
|
|
|
|
return exchange(m_ptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 22:03:48 -03:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T* ptr() const
|
2021-06-29 10:45:24 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(m_ptr);
|
|
|
|
|
return m_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 22:03:48 -03:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL T* operator->() const { return ptr(); }
|
2019-07-24 03:25:27 -03:00
|
|
|
|
2024-10-24 17:36:28 -03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& operator*() const { return *ptr(); }
|
2019-07-24 03:25:27 -03:00
|
|
|
|
2022-11-18 22:03:48 -03:00
|
|
|
ALWAYS_INLINE RETURNS_NONNULL operator T*() const { return ptr(); }
|
2019-07-24 03:25:27 -03:00
|
|
|
|
2019-11-07 14:00:05 -03:00
|
|
|
operator bool() const = delete;
|
|
|
|
|
bool operator!() const = delete;
|
|
|
|
|
|
2020-01-19 12:03:57 -03:00
|
|
|
void swap(NonnullOwnPtr& other)
|
|
|
|
|
{
|
2023-04-27 22:01:15 -03:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2020-01-19 12:03:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
|
void swap(NonnullOwnPtr<U>& other)
|
|
|
|
|
{
|
2023-04-27 22:01:15 -03:00
|
|
|
AK::swap(m_ptr, other.m_ptr);
|
2020-01-19 12:03:57 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-08 16:38:47 -03:00
|
|
|
template<typename U>
|
|
|
|
|
NonnullOwnPtr<U> release_nonnull()
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_ptr);
|
2020-05-08 16:38:47 -03:00
|
|
|
return NonnullOwnPtr<U>(NonnullOwnPtr<U>::Adopt, static_cast<U&>(*leak_ptr()));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
private:
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
2023-04-21 08:36:32 -03:00
|
|
|
auto* ptr = exchange(m_ptr, nullptr);
|
|
|
|
|
delete ptr;
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-01 16:04:10 -03:00
|
|
|
template<typename T>
|
|
|
|
|
inline NonnullOwnPtr<T> adopt_own(T& object)
|
|
|
|
|
{
|
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, object);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
template<class T, class... Args>
|
2021-07-01 05:21:14 -03:00
|
|
|
requires(IsConstructible<T, Args...>) inline NonnullOwnPtr<T> make(Args&&... args)
|
2019-07-24 03:25:27 -03:00
|
|
|
{
|
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T(forward<Args>(args)...));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 21:40:35 -03:00
|
|
|
// Use like `adopt_nonnull_own_or_enomem(new (nothrow) T(args...))`.
|
|
|
|
|
template<typename T>
|
|
|
|
|
inline ErrorOr<NonnullOwnPtr<T>> adopt_nonnull_own_or_enomem(T* object)
|
|
|
|
|
{
|
|
|
|
|
if (!object)
|
|
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *object);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 08:36:18 -03:00
|
|
|
template<typename T, class... Args>
|
|
|
|
|
requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullOwnPtr<T>> try_make(Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) T(forward<Args>(args)...));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
template<typename T>
|
2023-11-08 16:29:12 -03:00
|
|
|
struct Traits<NonnullOwnPtr<T>> : public DefaultTraits<NonnullOwnPtr<T>> {
|
2021-05-08 06:11:37 -03:00
|
|
|
using PeekType = T*;
|
2022-10-16 19:06:11 -03:00
|
|
|
using ConstPeekType = T const*;
|
2023-03-07 11:28:21 -03:00
|
|
|
static unsigned hash(NonnullOwnPtr<T> const& p) { return ptr_hash(p.ptr()); }
|
2022-04-01 14:58:27 -03:00
|
|
|
static bool equals(NonnullOwnPtr<T> const& a, NonnullOwnPtr<T> const& b) { return a.ptr() == b.ptr(); }
|
2025-09-18 13:28:54 -03:00
|
|
|
static constexpr bool may_have_slow_equality_check() { return false; }
|
2019-07-24 03:25:27 -03:00
|
|
|
};
|
|
|
|
|
|
2020-01-19 12:03:57 -03:00
|
|
|
template<typename T, typename U>
|
|
|
|
|
inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b)
|
|
|
|
|
{
|
|
|
|
|
a.swap(b);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 12:39:54 -03:00
|
|
|
template<Formattable T>
|
|
|
|
|
struct Formatter<NonnullOwnPtr<T>> : Formatter<T> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
|
|
|
|
{
|
|
|
|
|
return Formatter<T>::format(builder, *value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-15 10:24:01 -03:00
|
|
|
template<typename T>
|
2023-08-25 12:39:54 -03:00
|
|
|
requires(!HasFormatter<T>)
|
2022-10-16 19:06:11 -03:00
|
|
|
struct Formatter<NonnullOwnPtr<T>> : Formatter<T const*> {
|
2021-11-15 21:15:21 -03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
2020-10-15 10:24:01 -03:00
|
|
|
{
|
2022-10-16 19:06:11 -03:00
|
|
|
return Formatter<T const*>::format(builder, value.ptr());
|
2020-10-15 10:24:01 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-24 03:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2024-06-17 19:12:53 -03:00
|
|
|
using AK::adopt_nonnull_own_or_enomem;
|
2020-04-01 16:04:10 -03:00
|
|
|
using AK::adopt_own;
|
2019-07-24 03:25:27 -03:00
|
|
|
using AK::make;
|
|
|
|
|
using AK::NonnullOwnPtr;
|
2023-02-11 08:36:18 -03:00
|
|
|
using AK::try_make;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|