MOS Source Code
Loading...
Searching...
No Matches
refcount.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <atomic>
6#include <cstddef>
7#include <mos/allocator.hpp>
8#include <mos/type_utils.hpp>
9
10namespace mos
11{
12 struct RCCore
13 {
14 std::atomic_size_t n = 0;
15 void Ref()
16 {
17 n++;
18 }
19
20 void Unref()
21 {
22 n--;
23 }
24 };
25
27 {
28 protected:
29 explicit RefCounted(RCCore *rc_) : rc(rc_)
30 {
31 rc->Ref();
32 }
33
34 RefCounted(const RefCounted &a) : rc(a.rc)
35 {
36 rc->Ref();
37 }
38
40 {
41 a.rc = nullptr;
42 }
43
45 {
46 if (rc)
47 {
48 rc->Unref();
49 }
50 }
51
52 RefCounted operator=(const RefCounted &a) = delete;
54
55 protected:
56 size_t GetRef() const
57 {
58 return rc->n;
59 }
60
61 private:
62 RCCore *rc = nullptr;
63 };
64} // namespace mos
void Unref()
Definition refcount.hpp:20
void Ref()
Definition refcount.hpp:15
std::atomic_size_t n
Definition refcount.hpp:14
RefCounted(RefCounted &&a)
Definition refcount.hpp:39
RefCounted operator=(RefCounted &&a)=delete
RefCounted(const RefCounted &a)
Definition refcount.hpp:34
RefCounted operator=(const RefCounted &a)=delete
RefCounted(RCCore *rc_)
Definition refcount.hpp:29
size_t GetRef() const
Definition refcount.hpp:56