summaryrefslogtreecommitdiff
path: root/Kernel/RefCounted.h
blob: 88a611cdcd529a40eafb1b5d97dcf250a9f6d998 (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
43
44
45
46
47
48
#pragma once

#include "Assertions.h"
#include "VGA.h"

#define DEBUG_REFCOUNTED

class RefCountedBase {

protected:
    bool derefBase() const
    {
        return !--m_refCount;
    }
    mutable size_t m_refCount { 1 };
#ifdef DEBUG_REFCOUNTED
    //mutable bool m_adopted { false };
#endif
};

template<typename T>
class RefCounted : public RefCountedBase {
public:
    size_t refCount() const { return m_refCount; }

    void ref() const
    {
#ifdef DEBUG_REFCOUNTED
        ASSERT(m_refCount);
        //ASSERT(m_adopted);
#endif
        ++m_refCount;
    }

    void deref() const
    {
#ifdef DEBUG_REFCOUNTED
        ASSERT(m_refCount);
        //ASSERT(m_adopted);
#endif
        if (derefBase())
            delete static_cast<const T*>(this);
    }

protected:
    RefCounted() { }
    ~RefCounted() { }
};