mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
44 lines
566 B
C
44 lines
566 B
C
|
#pragma once
|
||
|
|
||
|
#include "Assertions.h"
|
||
|
|
||
|
namespace AK {
|
||
|
|
||
|
template<typename T>
|
||
|
class Retainable {
|
||
|
public:
|
||
|
Retainable() { }
|
||
|
|
||
|
void retain()
|
||
|
{
|
||
|
ASSERT(m_retainCount);
|
||
|
++m_retainCount;
|
||
|
}
|
||
|
|
||
|
void release()
|
||
|
{
|
||
|
assert(m_retainCount);
|
||
|
if (!--m_retainCount)
|
||
|
delete static_cast<const T*>(this);
|
||
|
}
|
||
|
|
||
|
int retainCount() const
|
||
|
{
|
||
|
return m_retainCount;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
~Retainable()
|
||
|
{
|
||
|
ASSERT(!m_retainCount);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
int m_retainCount { 1 };
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
using AK::Retainable;
|
||
|
|