ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
refcount.h
Go to the documentation of this file.
1#ifndef BASE_CORE_REFCOUNT_H_
2#define BASE_CORE_REFCOUNT_H_
3
4#include <atomic>
5#include <memory>
6#include <iostream>
7
8namespace base {
9namespace core {
10
15 public:
20
24 void ref() const;
25
30 bool unref() const;
31
36 int_fast32_t ref_count() const;
37
42 bool ref_count_is_one() const;
43
44 protected:
49 virtual ~counted_base() {}
50
51 private:
52 mutable std::atomic_int_fast32_t ref_;
53 counted_base(const counted_base&) = delete;
54 void operator=(const counted_base&) = delete;
55};
56
65 void operator()(const counted_base* o) const {
66 o->unref();
67 }
68};
69
74template <typename T>
75class ref_count_ptr;
76
83template <typename T>
84std::unique_ptr<T, ref_count_deleter> get_new_ref(T* ptr) {
85 static_assert(std::is_base_of<counted_base, T>::value,
86 "T must be derived from counted_base");
87
88 if (ptr == nullptr) {
89 return std::unique_ptr<T, ref_count_deleter>();
90 }
91 ptr->ref();
92 return std::unique_ptr<T, ref_count_deleter>(ptr);
93}
94
99template <typename T>
100class ref_count_ptr : public std::unique_ptr<T, ref_count_deleter> {
101 public:
102 using std::unique_ptr<T, ref_count_deleter>::unique_ptr;
103
108 std::unique_ptr<T, ref_count_deleter> get_new_ref() const {
109 if (this->get() == nullptr) {
110 return std::unique_ptr<T, ref_count_deleter>();
111 }
112 this->get()->ref();
113 return std::unique_ptr<T, ref_count_deleter>(this->get());
114 }
115};
116
117} // namespace core
118} // namespace base
119
120#endif // BASE_CORE_REFCOUNT_H_
The base class for reference-counted objects.
Definition refcount.h:14
std::atomic_int_fast32_t ref_
Definition refcount.h:52
virtual ~counted_base()
Virtual destructor.
Definition refcount.h:49
bool unref() const
Decreases the reference count by one.
Definition refcount.cpp:13
bool ref_count_is_one() const
Checks if the reference count is one.
Definition refcount.cpp:25
counted_base(const counted_base &)=delete
void operator=(const counted_base &)=delete
int_fast32_t ref_count() const
Gets the current reference count.
Definition refcount.cpp:21
void ref() const
Increases the reference count by one.
Definition refcount.cpp:9
counted_base()
Default constructor. Initializes the reference count to one.
Definition refcount.cpp:7
A smart pointer that holds a reference-counted object and releases it on destruction.
Definition refcount.h:100
std::unique_ptr< T, ref_count_deleter > get_new_ref() const
Adds a new reference to the owned object.
Definition refcount.h:108
#define T
Definition exp.cpp:237
std::unique_ptr< T, ref_count_deleter > get_new_ref(T *ptr)
Adds a new reference to a counted_base pointer.
Definition refcount.h:84
Definition allocator.h:6
A deleter functor for creating std::unique_ptr that unrefs objects.
Definition refcount.h:60
void operator()(const counted_base *o) const
Calls unref on the object.
Definition refcount.h:65