2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-24 06:25:27 +00:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class OwnPtr {
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
OwnPtr() {}
|
|
|
|
explicit OwnPtr(T* ptr)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
OwnPtr(OwnPtr&& other)
|
|
|
|
: m_ptr(other.leak_ptr())
|
|
|
|
{
|
|
|
|
}
|
2019-07-24 06:25:27 +00:00
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr(NonnullOwnPtr<U>&& other)
|
|
|
|
: m_ptr(static_cast<T*>(other.leak_ptr()))
|
|
|
|
{
|
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename U>
|
|
|
|
OwnPtr(OwnPtr<U>&& other)
|
|
|
|
: m_ptr(static_cast<T*>(other.leak_ptr()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
OwnPtr(std::nullptr_t) {};
|
2018-10-16 10:20:51 +00:00
|
|
|
~OwnPtr()
|
|
|
|
{
|
|
|
|
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*)(0xe1e1e1e1e1e1e1e1);
|
|
|
|
else
|
|
|
|
m_ptr = (T*)(0xe1e1e1e1);
|
|
|
|
#endif
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-07-24 06:25:27 +00:00
|
|
|
OwnPtr(const OwnPtr&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr(const OwnPtr<U>&) = delete;
|
|
|
|
OwnPtr& operator=(const OwnPtr&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(const OwnPtr<U>&) = delete;
|
|
|
|
|
2019-08-01 13:46:18 +00:00
|
|
|
template<typename U>
|
|
|
|
OwnPtr(const NonnullOwnPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(const NonnullOwnPtr<U>&) = delete;
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
|
|
|
OwnPtr(const RefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr(const NonnullRefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr(const WeakPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(const RefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(const NonnullRefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(const WeakPtr<U>&) = delete;
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
OwnPtr& operator=(OwnPtr&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
delete m_ptr;
|
2019-01-31 16:31:23 +00:00
|
|
|
m_ptr = other.leak_ptr();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(OwnPtr<U>&& other)
|
|
|
|
{
|
|
|
|
if (this != static_cast<void*>(&other)) {
|
|
|
|
delete m_ptr;
|
2019-01-31 16:31:23 +00:00
|
|
|
m_ptr = other.leak_ptr();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:25:27 +00:00
|
|
|
template<typename U>
|
|
|
|
OwnPtr& operator=(NonnullOwnPtr<U>&& other)
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr != other.ptr());
|
|
|
|
delete m_ptr;
|
|
|
|
m_ptr = other.leak_ptr();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
OwnPtr& operator=(T* ptr)
|
|
|
|
{
|
|
|
|
if (m_ptr != ptr)
|
|
|
|
delete m_ptr;
|
|
|
|
m_ptr = ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr& operator=(std::nullptr_t)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
delete m_ptr;
|
|
|
|
m_ptr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!() const { return !m_ptr; }
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
T* leak_ptr()
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-07-24 06:25:27 +00:00
|
|
|
T* leaked_ptr = m_ptr;
|
2018-10-10 09:53:07 +00:00
|
|
|
m_ptr = nullptr;
|
2019-07-24 06:25:27 +00:00
|
|
|
return leaked_ptr;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 09:02:49 +00:00
|
|
|
NonnullOwnPtr<T> release_nonnull()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *leak_ptr());
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
T* ptr() { return m_ptr; }
|
|
|
|
const T* ptr() const { return m_ptr; }
|
|
|
|
|
2019-08-02 08:34:40 +00:00
|
|
|
T* operator->()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* operator->() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-08-02 08:34:40 +00:00
|
|
|
T& operator*()
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator*() const
|
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-10-26 15:42:12 +00:00
|
|
|
template<typename T>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
|
2019-08-14 09:47:38 +00:00
|
|
|
using PeekType = const T*;
|
2019-08-14 19:21:15 +00:00
|
|
|
static unsigned hash(const OwnPtr<T>& p) { return int_hash((u32)p.ptr()); }
|
2018-10-26 15:42:12 +00:00
|
|
|
static void dump(const OwnPtr<T>& p) { kprintf("%p", p.ptr()); }
|
2019-06-29 17:14:03 +00:00
|
|
|
static bool equals(const OwnPtr<T>& a, const OwnPtr<T>& b) { return a.ptr() == b.ptr(); }
|
2018-10-26 15:42:12 +00:00
|
|
|
};
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
template<typename T>
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const OwnPtr<T>& value)
|
|
|
|
{
|
|
|
|
return stream << value.ptr();
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::OwnPtr;
|