mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Allow having ref counted pointers to const object
We allow the ref-counting parts of an object to be mutated even when the object itself is a const. An important detail is that we allow invoking 'will_be_destroyed' and 'one_ref_left', which are not required to be const qualified, on const objects.
This commit is contained in:
parent
badbe8bc99
commit
5c1b3ce42e
Notes:
sideshowbarker
2024-07-19 07:26:52 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/5c1b3ce42ef Pull-request: https://github.com/SerenityOS/serenity/pull/1885 Reviewed-by: https://github.com/awesomekling
1 changed files with 11 additions and 11 deletions
|
@ -32,9 +32,9 @@
|
|||
namespace AK {
|
||||
|
||||
template<class T>
|
||||
constexpr auto call_will_be_destroyed_if_present(T* object) -> decltype(object->will_be_destroyed(), TrueType {})
|
||||
constexpr auto call_will_be_destroyed_if_present(const T* object) -> decltype(object->will_be_destroyed(), TrueType {})
|
||||
{
|
||||
object->will_be_destroyed();
|
||||
const_cast<T*>(object)->will_be_destroyed();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,9 @@ constexpr auto call_will_be_destroyed_if_present(...) -> FalseType
|
|||
}
|
||||
|
||||
template<class T>
|
||||
constexpr auto call_one_ref_left_if_present(T* object) -> decltype(object->one_ref_left(), TrueType {})
|
||||
constexpr auto call_one_ref_left_if_present(const T* object) -> decltype(object->one_ref_left(), TrueType {})
|
||||
{
|
||||
object->one_ref_left();
|
||||
const_cast<T*>(object)->one_ref_left();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ constexpr auto call_one_ref_left_if_present(...) -> FalseType
|
|||
|
||||
class RefCountedBase {
|
||||
public:
|
||||
void ref()
|
||||
void ref() const
|
||||
{
|
||||
ASSERT(m_ref_count);
|
||||
++m_ref_count;
|
||||
|
@ -75,26 +75,26 @@ protected:
|
|||
ASSERT(!m_ref_count);
|
||||
}
|
||||
|
||||
void deref_base()
|
||||
void deref_base() const
|
||||
{
|
||||
ASSERT(m_ref_count);
|
||||
--m_ref_count;
|
||||
}
|
||||
|
||||
int m_ref_count { 1 };
|
||||
mutable int m_ref_count { 1 };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class RefCounted : public RefCountedBase {
|
||||
public:
|
||||
void unref()
|
||||
void unref() const
|
||||
{
|
||||
deref_base();
|
||||
if (m_ref_count == 0) {
|
||||
call_will_be_destroyed_if_present(static_cast<T*>(this));
|
||||
delete static_cast<T*>(this);
|
||||
call_will_be_destroyed_if_present(static_cast<const T*>(this));
|
||||
delete static_cast<const T*>(this);
|
||||
} else if (m_ref_count == 1) {
|
||||
call_one_ref_left_if_present(static_cast<T*>(this));
|
||||
call_one_ref_left_if_present(static_cast<const T*>(this));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue