2019-02-25 11:43:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-24 17:32:46 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-07-04 05:05:58 +00:00
|
|
|
#include <AK/LogStream.h>
|
2019-02-25 11:43:52 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename T>
|
|
|
|
class OwnPtr;
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename T>
|
|
|
|
class RefPtr;
|
2019-07-11 14:43:20 +00:00
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
template<typename T>
|
2019-06-21 13:29:31 +00:00
|
|
|
inline void ref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2019-06-21 13:29:31 +00:00
|
|
|
ptr->ref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-21 13:29:31 +00:00
|
|
|
inline void deref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2019-06-21 13:29:31 +00:00
|
|
|
ptr->deref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-21 16:37:47 +00:00
|
|
|
class CONSUMABLE(unconsumed) NonnullRefPtr {
|
2019-02-25 11:43:52 +00:00
|
|
|
public:
|
2019-07-25 09:10:28 +00:00
|
|
|
typedef T ElementType;
|
|
|
|
|
|
|
|
enum AdoptTag { Adopt };
|
2019-05-28 09:53:16 +00:00
|
|
|
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(const T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(const_cast<T*>(&object))
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(const U& object)
|
2019-06-15 16:45:44 +00:00
|
|
|
: m_ptr(&const_cast<T&>(static_cast<const T&>(object)))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(AdoptTag, T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(NonnullRefPtr&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&other.leak_ref())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(NonnullRefPtr<U>&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(const NonnullRefPtr& other)
|
2019-06-24 08:23:59 +00:00
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-24 08:23:59 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
RETURN_TYPESTATE(unconsumed)
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr(const NonnullRefPtr<U>& other)
|
2019-06-24 08:23:59 +00:00
|
|
|
: m_ptr(const_cast<T*>(static_cast<const T*>((other.ptr()))))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-24 08:23:59 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
~NonnullRefPtr()
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = nullptr;
|
|
|
|
#ifdef SANITIZE_PTRS
|
2019-05-28 09:53:16 +00:00
|
|
|
if constexpr (sizeof(T*) == 8)
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
|
|
|
|
else
|
|
|
|
m_ptr = (T*)(0xb0b0b0b0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr(const OwnPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const OwnPtr<U>&) = delete;
|
|
|
|
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr(const RefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const RefPtr<U>&) = delete;
|
|
|
|
NonnullRefPtr(const RefPtr<T>&) = delete;
|
|
|
|
NonnullRefPtr& operator=(const RefPtr<T>&) = delete;
|
|
|
|
|
2019-06-24 07:58:21 +00:00
|
|
|
NonnullRefPtr& operator=(const NonnullRefPtr& other)
|
|
|
|
{
|
|
|
|
if (m_ptr != other.m_ptr) {
|
|
|
|
deref_if_not_null(m_ptr);
|
|
|
|
m_ptr = const_cast<T*>(other.ptr());
|
|
|
|
m_ptr->ref();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const NonnullRefPtr<U>& other)
|
|
|
|
{
|
2019-07-26 06:04:33 +00:00
|
|
|
if (m_ptr != other.ptr()) {
|
2019-06-24 07:58:21 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
|
|
|
m_ptr = const_cast<T*>(static_cast<const T*>(other.ptr()));
|
|
|
|
m_ptr->ref();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr&& other)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = &other.leak_ref();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (this != static_cast<void*>(&other)) {
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-07-21 09:44:31 +00:00
|
|
|
m_ptr = static_cast<T*>(&other.leak_ref());
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-02 09:51:28 +00:00
|
|
|
NonnullRefPtr& operator=(const T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2019-08-02 09:35:05 +00:00
|
|
|
if (m_ptr != &object) {
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-08-02 09:51:28 +00:00
|
|
|
m_ptr = const_cast<T*>(&object);
|
2019-08-02 09:35:05 +00:00
|
|
|
m_ptr->ref();
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-02 10:05:09 +00:00
|
|
|
[[nodiscard]] CALLABLE_WHEN(unconsumed)
|
2019-05-28 09:53:16 +00:00
|
|
|
SET_TYPESTATE(consumed)
|
2019-02-25 11:43:52 +00:00
|
|
|
T& leak_ref()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
2019-08-02 10:05:09 +00:00
|
|
|
return *exchange(m_ptr, nullptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:51:24 +00:00
|
|
|
CALLABLE_WHEN("unconsumed","unknown")
|
2019-05-28 09:53:16 +00:00
|
|
|
T* ptr()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-07-25 09:51:24 +00:00
|
|
|
CALLABLE_WHEN("unconsumed","unknown")
|
2019-05-28 09:53:16 +00:00
|
|
|
const T* ptr() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
T* operator->()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
const T* operator->() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
T& operator*()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
const T& operator*() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
operator T*()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
operator const T*() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2019-06-15 16:45:44 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
operator T&()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
operator const T&() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
private:
|
2019-06-24 07:58:21 +00:00
|
|
|
NonnullRefPtr() = delete;
|
2019-02-25 11:43:52 +00:00
|
|
|
|
|
|
|
T* m_ptr { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-21 16:37:47 +00:00
|
|
|
inline NonnullRefPtr<T> adopt(T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
template<typename T>
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr<T>& value)
|
|
|
|
{
|
|
|
|
return stream << value.ptr();
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::adopt;
|
2019-06-21 16:37:47 +00:00
|
|
|
using AK::NonnullRefPtr;
|