summaryrefslogtreecommitdiff
path: root/AK/Retainable.h
blob: 32c4a9b7a0832b6f0b7d8ad4829a11d59bae3618 (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<T*>(this);
    }

    int retainCount() const
    {
        return m_retainCount;
    }

protected:
    Retainable() { }
    ~Retainable()
    {
        ASSERT(!m_retainCount);
    }

private:
    int m_retainCount { 1 };
};

}

using AK::Retainable;