]> git.tdb.fi Git - ext/openal.git/blob - common/intrusive_ptr.h
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / common / intrusive_ptr.h
1 #ifndef INTRUSIVE_PTR_H
2 #define INTRUSIVE_PTR_H
3
4 #include <utility>
5
6 #include "atomic.h"
7 #include "opthelpers.h"
8
9
10 namespace al {
11
12 template<typename T>
13 class intrusive_ref {
14     RefCount mRef{1u};
15
16 public:
17     unsigned int add_ref() noexcept { return IncrementRef(mRef); }
18     unsigned int dec_ref() noexcept
19     {
20         auto ref = DecrementRef(mRef);
21         if(ref == 0) UNLIKELY
22             delete static_cast<T*>(this);
23         return ref;
24     }
25
26     /**
27      * Release only if doing so would not bring the object to 0 references and
28      * delete it. Returns false if the object could not be released.
29      *
30      * NOTE: The caller is responsible for handling a failed release, as it
31      * means the object has no other references and needs to be be deleted
32      * somehow.
33      */
34     bool releaseIfNoDelete() noexcept
35     {
36         auto val = mRef.load(std::memory_order_acquire);
37         while(val > 1 && !mRef.compare_exchange_strong(val, val-1, std::memory_order_acq_rel))
38         {
39             /* val was updated with the current value on failure, so just try
40              * again.
41              */
42         }
43
44         return val >= 2;
45     }
46 };
47
48
49 template<typename T>
50 class intrusive_ptr {
51     T *mPtr{nullptr};
52
53 public:
54     intrusive_ptr() noexcept = default;
55     intrusive_ptr(const intrusive_ptr &rhs) noexcept : mPtr{rhs.mPtr}
56     { if(mPtr) mPtr->add_ref(); }
57     intrusive_ptr(intrusive_ptr&& rhs) noexcept : mPtr{rhs.mPtr}
58     { rhs.mPtr = nullptr; }
59     intrusive_ptr(std::nullptr_t) noexcept { }
60     explicit intrusive_ptr(T *ptr) noexcept : mPtr{ptr} { }
61     ~intrusive_ptr() { if(mPtr) mPtr->dec_ref(); }
62
63     intrusive_ptr& operator=(const intrusive_ptr &rhs) noexcept
64     {
65         static_assert(noexcept(std::declval<T*>()->dec_ref()), "dec_ref must be noexcept");
66
67         if(rhs.mPtr) rhs.mPtr->add_ref();
68         if(mPtr) mPtr->dec_ref();
69         mPtr = rhs.mPtr;
70         return *this;
71     }
72     intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept
73     {
74         if(&rhs != this) LIKELY
75         {
76             if(mPtr) mPtr->dec_ref();
77             mPtr = std::exchange(rhs.mPtr, nullptr);
78         }
79         return *this;
80     }
81
82     explicit operator bool() const noexcept { return mPtr != nullptr; }
83
84     T& operator*() const noexcept { return *mPtr; }
85     T* operator->() const noexcept { return mPtr; }
86     T* get() const noexcept { return mPtr; }
87
88     void reset(T *ptr=nullptr) noexcept
89     {
90         if(mPtr)
91             mPtr->dec_ref();
92         mPtr = ptr;
93     }
94
95     T* release() noexcept { return std::exchange(mPtr, nullptr); }
96
97     void swap(intrusive_ptr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
98     void swap(intrusive_ptr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
99 };
100
101 #define AL_DECL_OP(op)                                                        \
102 template<typename T>                                                          \
103 inline bool operator op(const intrusive_ptr<T> &lhs, const T *rhs) noexcept   \
104 { return lhs.get() op rhs; }                                                  \
105 template<typename T>                                                          \
106 inline bool operator op(const T *lhs, const intrusive_ptr<T> &rhs) noexcept   \
107 { return lhs op rhs.get(); }
108
109 AL_DECL_OP(==)
110 AL_DECL_OP(!=)
111 AL_DECL_OP(<=)
112 AL_DECL_OP(>=)
113 AL_DECL_OP(<)
114 AL_DECL_OP(>)
115
116 #undef AL_DECL_OP
117
118 } // namespace al
119
120 #endif /* INTRUSIVE_PTR_H */