2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
#include <AK/LogStream.h>
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Types.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename T>
|
|
|
|
class OwnPtr;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T>
|
2019-06-21 16:37:47 +00:00
|
|
|
class RefPtr {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum AdoptTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
Adopt
|
|
|
|
};
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr() {}
|
|
|
|
RefPtr(const T* ptr)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(const_cast<T*>(ptr))
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(T* ptr)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&object)
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(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
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(AdoptTag, T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&object)
|
|
|
|
{
|
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(RefPtr&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(other.leak_ref())
|
|
|
|
{
|
|
|
|
}
|
2019-07-11 13:36:01 +00:00
|
|
|
RefPtr(const NonnullRefPtr<T>& other)
|
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
|
|
|
{
|
|
|
|
m_ptr->ref();
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
RefPtr(const NonnullRefPtr<U>& other)
|
|
|
|
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
|
|
|
{
|
|
|
|
m_ptr->ref();
|
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(NonnullRefPtr<U>&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(RefPtr<U>&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(static_cast<T*>(other.leak_ref()))
|
|
|
|
{
|
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(const RefPtr& other)
|
2019-07-11 13:45:11 +00:00
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-07-11 13:45:11 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(const RefPtr<U>& other)
|
2019-07-11 13:45:11 +00:00
|
|
|
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-07-11 13:45:11 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
~RefPtr()
|
2018-10-16 10:20:51 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
#ifdef SANITIZE_PTRS
|
2019-05-28 09:53:16 +00:00
|
|
|
if constexpr (sizeof(T*) == 8)
|
2018-10-16 10:20:51 +00:00
|
|
|
m_ptr = (T*)(0xe0e0e0e0e0e0e0e0);
|
|
|
|
else
|
|
|
|
m_ptr = (T*)(0xe0e0e0e0);
|
|
|
|
#endif
|
|
|
|
}
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr(std::nullptr_t) {}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
|
|
|
RefPtr(const OwnPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
RefPtr& operator=(const OwnPtr<U>&) = delete;
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(RefPtr&& other)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (this != &other) {
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-01-31 16:31:23 +00:00
|
|
|
m_ptr = other.leak_ref();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(RefPtr<U>&& other)
|
2018-10-10 09:53:07 +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:36:22 +00:00
|
|
|
m_ptr = static_cast<T*>(other.leak_ref());
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:36:06 +00:00
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(NonnullRefPtr<U>&& other)
|
2019-04-14 00:36:06 +00:00
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-04-14 00:36:06 +00:00
|
|
|
m_ptr = &other.leak_ref();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:36:01 +00:00
|
|
|
RefPtr& operator=(const NonnullRefPtr<T>& other)
|
|
|
|
{
|
|
|
|
if (m_ptr != other.ptr())
|
|
|
|
deref_if_not_null(m_ptr);
|
|
|
|
m_ptr = const_cast<T*>(other.ptr());
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
ref_if_not_null(m_ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:45:44 +00:00
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(const NonnullRefPtr<U>& other)
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
|
|
|
if (m_ptr != other.ptr())
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
m_ptr = const_cast<T*>(other.ptr());
|
|
|
|
ASSERT(m_ptr);
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:36:01 +00:00
|
|
|
RefPtr& operator=(const RefPtr& other)
|
|
|
|
{
|
|
|
|
if (m_ptr != other.ptr())
|
|
|
|
deref_if_not_null(m_ptr);
|
|
|
|
m_ptr = const_cast<T*>(other.ptr());
|
|
|
|
ref_if_not_null(m_ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:45:44 +00:00
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(const RefPtr<U>& other)
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
|
|
|
if (m_ptr != other.ptr())
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
m_ptr = const_cast<T*>(other.ptr());
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(const T* ptr)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (m_ptr != ptr)
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
m_ptr = const_cast<T*>(ptr);
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(const T& object)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
if (m_ptr != &object)
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2019-06-15 16:45:44 +00:00
|
|
|
m_ptr = const_cast<T*>(&object);
|
2019-06-21 13:29:31 +00:00
|
|
|
ref_if_not_null(m_ptr);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr& operator=(std::nullptr_t)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
deref_if_not_null(m_ptr);
|
2018-10-10 09:53:07 +00:00
|
|
|
m_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!() const { return !m_ptr; }
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
T* leak_ref()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
T* leakedPtr = m_ptr;
|
|
|
|
m_ptr = nullptr;
|
|
|
|
return leakedPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2019-04-14 00:36:06 +00:00
|
|
|
operator const T*() const { return m_ptr; }
|
|
|
|
operator T*() { return m_ptr; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
operator bool() { return !!m_ptr; }
|
|
|
|
|
2019-04-14 00:36:06 +00:00
|
|
|
bool operator==(std::nullptr_t) const { return !m_ptr; }
|
|
|
|
bool operator!=(std::nullptr_t) const { return m_ptr; }
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
bool operator==(const RefPtr& other) const { return m_ptr == other.m_ptr; }
|
|
|
|
bool operator!=(const RefPtr& other) const { return m_ptr != other.m_ptr; }
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
bool operator==(RefPtr& other) { return m_ptr == other.m_ptr; }
|
|
|
|
bool operator!=(RefPtr& other) { return m_ptr != other.m_ptr; }
|
2019-04-19 19:36:11 +00:00
|
|
|
|
2019-04-14 00:36:06 +00:00
|
|
|
bool operator==(const T* other) const { return m_ptr == other; }
|
|
|
|
bool operator!=(const T* other) const { return m_ptr != other; }
|
|
|
|
|
2019-04-19 19:34:47 +00:00
|
|
|
bool operator==(T* other) { return m_ptr == other; }
|
|
|
|
bool operator!=(T* other) { return m_ptr != other; }
|
|
|
|
|
2018-11-05 09:23:00 +00:00
|
|
|
bool is_null() const { return !m_ptr; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
template<typename T>
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const RefPtr<T>& value)
|
|
|
|
{
|
|
|
|
return stream << value.ptr();
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
using AK::RefPtr;
|