-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_counted.hpp
49 lines (32 loc) · 856 Bytes
/
ref_counted.hpp
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
49
#ifndef REF_COUNTED_HPP
#define REF_COUNTED_HPP
#include <atomic>
#include <cstddef>
namespace std {
class ref_counted {
public:
~ref_counted();
ref_counted();
ref_counted(const ref_counted &);
ref_counted &operator=(const ref_counted &);
inline void ref() noexcept {
rc_.fetch_add(1, std::memory_order_relaxed);
}
void deref() noexcept;
inline bool unique() const noexcept {
return rc_ == 1;
}
inline size_t get_reference_count() const noexcept {
return rc_;
}
protected:
std::atomic<size_t> rc_;
};
inline void intrusive_ptr_add_ref(ref_counted *p) {
p->ref();
}
inline void intrusive_ptr_release(ref_counted *p) {
p->deref();
}
}
#endif // REF_COUNTED_HPP