Merge some features from gerbert into OwnPtr and RetainPtr.

This commit is contained in:
Andreas Kling 2018-10-16 12:20:51 +02:00
parent fd708a4cb1
commit 1203c327c7
Notes: sideshowbarker 2024-07-19 18:47:29 +09:00
3 changed files with 46 additions and 8 deletions

View file

@ -1,7 +1,7 @@
#pragma once
#include <cstddef>
#include <utility>
#include "StdLib.h"
#include "Types.h"
namespace AK {
@ -12,8 +12,17 @@ public:
explicit OwnPtr(T* ptr) : m_ptr(ptr) { }
OwnPtr(OwnPtr&& other) : m_ptr(other.leakPtr()) { }
template<typename U> OwnPtr(OwnPtr<U>&& other) : m_ptr(static_cast<T*>(other.leakPtr())) { }
~OwnPtr() { clear(); }
OwnPtr(std::nullptr_t) { };
~OwnPtr()
{
clear();
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
m_ptr = (T*)(0xe1e1e1e1e1e1e1e1);
else
m_ptr = (T*)(0xe1e1e1e1);
#endif
}
OwnPtr& operator=(OwnPtr&& other)
{
@ -84,7 +93,7 @@ private:
template<class T, class... Args> inline OwnPtr<T>
make(Args&&... args)
{
return OwnPtr<T>(new T(std::forward<Args>(args)...));
return OwnPtr<T>(new T(forward<Args>(args)...));
}
}

View file

@ -1,8 +1,6 @@
#pragma once
namespace std {
typedef decltype(nullptr) nullptr_t;
}
#include "Types.h"
namespace AK {
@ -32,7 +30,16 @@ public:
RetainPtr(AdoptTag, T& object) : m_ptr(&object) { }
RetainPtr(RetainPtr&& other) : m_ptr(other.leakRef()) { }
template<typename U> RetainPtr(RetainPtr<U>&& other) : m_ptr(static_cast<T*>(other.leakRef())) { }
~RetainPtr() { clear(); }
~RetainPtr()
{
clear();
#ifdef SANITIZE_PTRS
if constexpr(sizeof(T*) == 8)
m_ptr = (T*)(0xe0e0e0e0e0e0e0e0);
else
m_ptr = (T*)(0xe0e0e0e0);
#endif
}
RetainPtr(std::nullptr_t) { }
RetainPtr& operator=(RetainPtr&& other)

View file

@ -30,9 +30,31 @@ T&& move(T& arg)
return static_cast<T&&>(arg);
}
template<typename T>
struct identity {
typedef T type;
};
template<typename T>
T&& forward(typename identity<T>::type&& param)
{
return static_cast<typename identity<T>::type&&>(param);
}
template<typename T, typename U>
T exchange(T& a, U&& b)
{
T tmp = move(a);
a = move(b);
return tmp;
}
}
using AK::min;
using AK::max;
using AK::move;
using AK::forward;
using AK::exchange;
using AK::ceilDiv;