1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 04:27:45 +00:00

AK: Add Retained<T>, like RetainPtr, but never null.

Also use some Clang attribute wizardry to get a warning for use-after-move.
This commit is contained in:
Andreas Kling 2019-02-25 12:43:52 +01:00
parent 0b957ed2b1
commit 2cfcbdc735
31 changed files with 214 additions and 104 deletions

View file

@ -1,23 +1,10 @@
#pragma once
#include "Types.h"
#include <AK/Types.h>
#include <AK/Retained.h>
namespace AK {
template<typename T>
inline void retain_if_not_null(T* ptr)
{
if (ptr)
ptr->retain();
}
template<typename T>
inline void release_if_not_null(T* ptr)
{
if (ptr)
ptr->release();
}
template<typename T>
class RetainPtr {
public:
@ -30,6 +17,7 @@ public:
RetainPtr(AdoptTag, T& object) : m_ptr(&object) { }
RetainPtr(RetainPtr& other) : m_ptr(other.copy_ref().leak_ref()) { }
RetainPtr(RetainPtr&& other) : m_ptr(other.leak_ref()) { }
template<typename U> RetainPtr(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
template<typename U> RetainPtr(RetainPtr<U>&& other) : m_ptr(static_cast<T*>(other.leak_ref())) { }
RetainPtr(const RetainPtr& other) : m_ptr(const_cast<RetainPtr&>(other).copy_ref().leak_ref()) { }
template<typename U> RetainPtr(const RetainPtr<U>& other) : m_ptr(const_cast<RetainPtr<U>&>(other).copy_ref().leak_ref()) { }
@ -128,14 +116,7 @@ private:
T* m_ptr = nullptr;
};
template<typename T>
inline RetainPtr<T> adopt(T& object)
{
return RetainPtr<T>(RetainPtr<T>::Adopt, object);
}
}
using AK::RetainPtr;
using AK::adopt;