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>
|
|
|
|
|
|
|
|
#ifdef __clang__
|
2019-05-28 09:53:16 +00:00
|
|
|
# define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
|
|
|
|
# define CALLABLE_WHEN(...) __attribute__((callable_when(__VA_ARGS__)))
|
|
|
|
# define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
|
|
|
|
# define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
|
2019-02-25 11:43:52 +00:00
|
|
|
#else
|
2019-05-28 09:53:16 +00:00
|
|
|
# define CONSUMABLE(initial_state)
|
|
|
|
# define CALLABLE_WHEN(state)
|
|
|
|
# define SET_TYPESTATE(state)
|
|
|
|
# define RETURN_TYPESTATE(state)
|
2019-02-25 11:43:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename T>
|
|
|
|
class OwnPtr;
|
|
|
|
|
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-06-07 15:13:23 +00:00
|
|
|
enum AdoptTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
Adopt
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
if (m_ptr != other.m_ptr) {
|
|
|
|
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-02-25 11:43:52 +00:00
|
|
|
m_ptr = &other.leak_ref();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr& operator=(T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (m_ptr != &object)
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = &object;
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
SET_TYPESTATE(consumed)
|
2019-02-25 11:43:52 +00:00
|
|
|
T& leak_ref()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
T* leakedPtr = m_ptr;
|
|
|
|
m_ptr = nullptr;
|
|
|
|
return *leakedPtr;
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
T* ptr()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
CALLABLE_WHEN(unconsumed)
|
|
|
|
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;
|