summaryrefslogtreecommitdiff
path: root/AK/Retainable.h
blob: cf4308520c386773e0874fc92a47cd0407d2e818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once

#include "Assertions.h"

namespace AK {

template<typename T>
class Retainable {
public:
    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() { }
    ~Retainable()
    {
        ASSERT(!m_retainCount);
    }

private:
    int m_retainCount { 1 };
};

}

using AK::Retainable;