1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-31 16:57:34 +00:00

AK: Rename Retainable => RefCounted.

(And various related renames that go along with it.)
This commit is contained in:
Andreas Kling 2019-06-21 15:29:31 +02:00
parent ef1bfcb9d8
commit 77b9fa89dd
45 changed files with 118 additions and 118 deletions

View file

@ -8,7 +8,7 @@
namespace AK {
class ByteBufferImpl : public Retainable<ByteBufferImpl> {
class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
public:
static Retained<ByteBufferImpl> create_uninitialized(int size);
static Retained<ByteBufferImpl> create_zeroed(int);

View file

@ -30,7 +30,7 @@ void JsonValue::copy_from(const JsonValue& other)
switch (m_type) {
case Type::String:
m_value.as_string = other.m_value.as_string;
AK::retain_if_not_null(m_value.as_string);
AK::ref_if_not_null(m_value.as_string);
break;
case Type::Object:
m_value.as_object = new JsonObject(*other.m_value.as_object);
@ -101,7 +101,7 @@ JsonValue::JsonValue(const String& value)
} else {
m_type = Type::String;
m_value.as_string = const_cast<StringImpl*>(value.impl());
AK::retain_if_not_null(m_value.as_string);
AK::ref_if_not_null(m_value.as_string);
}
}
@ -121,7 +121,7 @@ void JsonValue::clear()
{
switch (m_type) {
case Type::String:
AK::release_if_not_null(m_value.as_string);
AK::deref_if_not_null(m_value.as_string);
break;
case Type::Object:
delete m_value.as_object;

View file

@ -16,22 +16,22 @@ public:
RetainPtr(const T* ptr)
: m_ptr(const_cast<T*>(ptr))
{
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
}
RetainPtr(T* ptr)
: m_ptr(ptr)
{
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
}
RetainPtr(T& object)
: m_ptr(&object)
{
m_ptr->retain();
m_ptr->ref();
}
RetainPtr(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
m_ptr->ref();
}
RetainPtr(AdoptTag, T& object)
: m_ptr(&object)
@ -79,7 +79,7 @@ public:
RetainPtr& operator=(RetainPtr&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = other.leak_ref();
}
return *this;
@ -89,7 +89,7 @@ public:
RetainPtr& operator=(RetainPtr<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = other.leak_ref();
}
return *this;
@ -98,7 +98,7 @@ public:
template<typename U>
RetainPtr& operator=(Retained<U>&& other)
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
return *this;
}
@ -107,10 +107,10 @@ public:
RetainPtr& operator=(const Retained<U>& other)
{
if (m_ptr != other.ptr())
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(other.ptr());
ASSERT(m_ptr);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}
@ -118,27 +118,27 @@ public:
RetainPtr& operator=(const RetainPtr<U>& other)
{
if (m_ptr != other.ptr())
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(other.ptr());
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}
RetainPtr& operator=(const T* ptr)
{
if (m_ptr != ptr)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(ptr);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}
RetainPtr& operator=(const T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(&object);
retain_if_not_null(m_ptr);
ref_if_not_null(m_ptr);
return *this;
}
@ -155,7 +155,7 @@ public:
void clear()
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = nullptr;
}

View file

@ -18,61 +18,61 @@ constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
}
template<class T>
constexpr auto call_one_retain_left_if_present(T* object) -> decltype(object->one_retain_left(), TrueType {})
constexpr auto call_one_ref_left_if_present(T* object) -> decltype(object->one_ref_left(), TrueType {})
{
object->one_retain_left();
object->one_ref_left();
return {};
}
constexpr auto call_one_retain_left_if_present(...) -> FalseType
constexpr auto call_one_ref_left_if_present(...) -> FalseType
{
return {};
}
class RetainableBase {
class RefCountedBase {
public:
void retain()
void ref()
{
ASSERT(m_retain_count);
++m_retain_count;
ASSERT(m_ref_count);
++m_ref_count;
}
int retain_count() const
int ref_count() const
{
return m_retain_count;
return m_ref_count;
}
protected:
RetainableBase() {}
~RetainableBase()
RefCountedBase() {}
~RefCountedBase()
{
ASSERT(!m_retain_count);
ASSERT(!m_ref_count);
}
void release_base()
void deref_base()
{
ASSERT(m_retain_count);
--m_retain_count;
ASSERT(m_ref_count);
--m_ref_count;
}
int m_retain_count { 1 };
int m_ref_count { 1 };
};
template<typename T>
class Retainable : public RetainableBase {
class RefCounted : public RefCountedBase {
public:
void release()
void deref()
{
release_base();
if (m_retain_count == 0) {
deref_base();
if (m_ref_count == 0) {
call_will_be_destroyed_if_present(static_cast<T*>(this));
delete static_cast<T*>(this);
} else if (m_retain_count == 1) {
call_one_retain_left_if_present(static_cast<T*>(this));
} else if (m_ref_count == 1) {
call_one_ref_left_if_present(static_cast<T*>(this));
}
}
};
}
using AK::Retainable;
using AK::RefCounted;

View file

@ -18,17 +18,17 @@
namespace AK {
template<typename T>
inline void retain_if_not_null(T* ptr)
inline void ref_if_not_null(T* ptr)
{
if (ptr)
ptr->retain();
ptr->ref();
}
template<typename T>
inline void release_if_not_null(T* ptr)
inline void deref_if_not_null(T* ptr)
{
if (ptr)
ptr->release();
ptr->deref();
}
template<typename T>
@ -42,14 +42,14 @@ public:
Retained(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->retain();
m_ptr->ref();
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
Retained(const U& object)
: m_ptr(&const_cast<T&>(static_cast<const T&>(object)))
{
m_ptr->retain();
m_ptr->ref();
}
RETURN_TYPESTATE(unconsumed)
Retained(AdoptTag, T& object)
@ -85,7 +85,7 @@ public:
}
~Retained()
{
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = nullptr;
#ifdef SANITIZE_PTRS
if constexpr (sizeof(T*) == 8)
@ -99,7 +99,7 @@ public:
Retained& operator=(Retained&& other)
{
if (this != &other) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
@ -110,7 +110,7 @@ public:
Retained& operator=(Retained<U>&& other)
{
if (this != static_cast<void*>(&other)) {
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
@ -120,9 +120,9 @@ public:
Retained& operator=(T& object)
{
if (m_ptr != &object)
release_if_not_null(m_ptr);
deref_if_not_null(m_ptr);
m_ptr = &object;
m_ptr->retain();
m_ptr->ref();
return *this;
}

View file

@ -12,7 +12,7 @@ enum ShouldChomp {
Chomp
};
class StringImpl : public Retainable<StringImpl> {
class StringImpl : public RefCounted<StringImpl> {
public:
static Retained<StringImpl> create_uninitialized(int length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);

View file

@ -12,7 +12,7 @@ template<typename T>
class WeakPtr;
template<typename T>
class WeakLink : public Retainable<WeakLink<T>> {
class WeakLink : public RefCounted<WeakLink<T>> {
friend class Weakable<T>;
public: