2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 11:26:07 -04:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Checked.h>
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/Platform.h>
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-21 09:29:31 -04:00
|
|
|
class RefCountedBase {
|
2020-08-26 15:52:24 -04:00
|
|
|
AK_MAKE_NONCOPYABLE(RefCountedBase);
|
|
|
|
AK_MAKE_NONMOVABLE(RefCountedBase);
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
public:
|
2020-11-11 17:21:01 -05:00
|
|
|
using RefCountType = unsigned int;
|
2020-11-03 09:51:56 -05:00
|
|
|
using AllowOwnPtr = FalseType;
|
2020-06-12 09:33:38 -04:00
|
|
|
|
2021-10-08 16:11:39 -04:00
|
|
|
ALWAYS_INLINE void ref() const
|
2021-06-02 19:12:09 -04:00
|
|
|
{
|
2021-10-07 13:12:37 -04:00
|
|
|
VERIFY(m_ref_count > 0);
|
|
|
|
VERIFY(!Checked<RefCountType>::addition_would_overflow(m_ref_count, 1));
|
|
|
|
++m_ref_count;
|
2021-06-02 19:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool try_ref() const
|
|
|
|
{
|
2021-10-07 13:12:37 -04:00
|
|
|
if (m_ref_count == 0)
|
|
|
|
return false;
|
|
|
|
ref();
|
|
|
|
return true;
|
2021-06-02 19:12:09 -04:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:12:37 -04:00
|
|
|
[[nodiscard]] RefCountType ref_count() const { return m_ref_count; }
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
protected:
|
2021-01-10 18:29:28 -05:00
|
|
|
RefCountedBase() = default;
|
2021-10-07 13:12:37 -04:00
|
|
|
~RefCountedBase() { VERIFY(!m_ref_count); }
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2021-10-08 16:11:39 -04:00
|
|
|
ALWAYS_INLINE RefCountType deref_base() const
|
2021-06-02 19:12:09 -04:00
|
|
|
{
|
2021-10-07 13:12:37 -04:00
|
|
|
VERIFY(m_ref_count);
|
|
|
|
return --m_ref_count;
|
2021-06-02 19:12:09 -04:00
|
|
|
}
|
2019-03-16 08:48:56 -04:00
|
|
|
|
2021-10-07 13:12:37 -04:00
|
|
|
RefCountType mutable m_ref_count { 1 };
|
2018-10-10 05:53:07 -04:00
|
|
|
};
|
|
|
|
|
2019-03-16 07:47:19 -04:00
|
|
|
template<typename T>
|
2019-06-21 09:29:31 -04:00
|
|
|
class RefCounted : public RefCountedBase {
|
2019-03-16 07:47:19 -04:00
|
|
|
public:
|
2020-12-29 15:14:21 -05:00
|
|
|
bool unref() const
|
2019-03-16 07:47:19 -04:00
|
|
|
{
|
2022-01-08 13:35:43 -05:00
|
|
|
auto* that = const_cast<T*>(static_cast<T const*>(this));
|
2022-01-08 09:15:55 -05:00
|
|
|
|
2020-06-12 09:38:24 -04:00
|
|
|
auto new_ref_count = deref_base();
|
|
|
|
if (new_ref_count == 0) {
|
2022-01-08 09:15:55 -05:00
|
|
|
if constexpr (requires { that->will_be_destroyed(); })
|
|
|
|
that->will_be_destroyed();
|
2022-08-19 11:26:07 -04:00
|
|
|
delete static_cast<T const*>(this);
|
2020-12-29 15:14:21 -05:00
|
|
|
return true;
|
2019-03-16 07:47:19 -04:00
|
|
|
}
|
2020-12-29 15:14:21 -05:00
|
|
|
return false;
|
2019-03-16 07:47:19 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
}
|
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-21 09:29:31 -04:00
|
|
|
using AK::RefCounted;
|
2021-08-15 06:26:22 -04:00
|
|
|
using AK::RefCountedBase;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|