2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-12-03 23:27:16 +00:00
|
|
|
#include "StdLibExtras.h"
|
2018-10-26 15:42:12 +00:00
|
|
|
#include "Traits.h"
|
2019-05-28 09:53:16 +00:00
|
|
|
#include "Types.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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<class T, class... Args>
|
|
|
|
inline OwnPtr<T>
|
2018-10-10 09:53:07 +00:00
|
|
|
make(Args&&... args)
|
|
|
|
{
|
2018-12-05 00:43:07 +00:00
|
|
|
return OwnPtr<T>(new T(AK::forward<Args>(args)...));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 15:42:12 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<OwnPtr<T>> {
|
|
|
|
static unsigned hash(const OwnPtr<T>& p) { return (unsigned)p.ptr(); }
|
|
|
|
static void dump(const OwnPtr<T>& p) { kprintf("%p", p.ptr()); }
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::make;
|
2019-05-28 09:53:16 +00:00
|
|
|
using AK::OwnPtr;
|