xref: /aosp_15_r20/external/pdfium/core/fxcrt/retain_ptr.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FXCRT_RETAIN_PTR_H_
6 #define CORE_FXCRT_RETAIN_PTR_H_
7 
8 #include <stdint.h>
9 
10 #include <functional>
11 #include <memory>
12 #include <type_traits>
13 #include <utility>
14 
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "third_party/base/check.h"
17 #include "third_party/base/compiler_specific.h"
18 
19 namespace fxcrt {
20 
21 // Used with std::unique_ptr to Release() objects that can't be deleted.
22 template <class T>
23 struct ReleaseDeleter {
operatorReleaseDeleter24   inline void operator()(T* ptr) const { ptr->Release(); }
25 };
26 
27 // Analogous to base's scoped_refptr.
28 template <class T>
29 class TRIVIAL_ABI RetainPtr {
30  public:
31   RetainPtr() noexcept = default;
32 
33   // Deliberately implicit to allow returning nullptrs.
34   // NOLINTNEXTLINE(runtime/explicit)
RetainPtr(std::nullptr_t ptr)35   RetainPtr(std::nullptr_t ptr) {}
36 
RetainPtr(T * pObj)37   explicit RetainPtr(T* pObj) noexcept : m_pObj(pObj) {
38     if (m_pObj)
39       m_pObj->Retain();
40   }
41 
42   // Copy-construct a RetainPtr.
43   // Required in addition to copy conversion constructor below.
RetainPtr(const RetainPtr & that)44   RetainPtr(const RetainPtr& that) noexcept : RetainPtr(that.Get()) {}
45 
46   // Move-construct a RetainPtr. After construction, |that| will be NULL.
47   // Required in addition to move conversion constructor below.
RetainPtr(RetainPtr && that)48   RetainPtr(RetainPtr&& that) noexcept { Unleak(that.Leak()); }
49 
50   // Copy conversion constructor.
51   template <class U,
52             typename = typename std::enable_if<
53                 std::is_convertible<U*, T*>::value>::type>
RetainPtr(const RetainPtr<U> & that)54   RetainPtr(const RetainPtr<U>& that) : RetainPtr(that.Get()) {}
55 
56   // Move-conversion constructor.
57   template <class U,
58             typename = typename std::enable_if<
59                 std::is_convertible<U*, T*>::value>::type>
RetainPtr(RetainPtr<U> && that)60   RetainPtr(RetainPtr<U>&& that) noexcept {
61     Unleak(that.Leak());
62   }
63 
64   // Assign a RetainPtr from nullptr;
65   RetainPtr& operator=(std::nullptr_t) noexcept {
66     Reset();
67     return *this;
68   }
69 
70   // Copy-assign a RetainPtr.
71   // Required in addition to copy conversion assignment below.
72   RetainPtr& operator=(const RetainPtr& that) {
73     if (*this != that)
74       Reset(that.Get());
75     return *this;
76   }
77 
78   // Move-assign a RetainPtr. After assignment, |that| will be NULL.
79   // Required in addition to move conversion assignment below.
80   RetainPtr& operator=(RetainPtr&& that) noexcept {
81     Unleak(that.Leak());
82     return *this;
83   }
84 
85   // Copy-convert assign a RetainPtr.
86   template <class U,
87             typename = typename std::enable_if<
88                 std::is_convertible<U*, T*>::value>::type>
89   RetainPtr& operator=(const RetainPtr<U>& that) {
90     if (*this != that)
91       Reset(that.Get());
92     return *this;
93   }
94 
95   // Move-convert assign a RetainPtr. After assignment, |that| will be NULL.
96   template <class U,
97             typename = typename std::enable_if<
98                 std::is_convertible<U*, T*>::value>::type>
99   RetainPtr& operator=(RetainPtr<U>&& that) noexcept {
100     Unleak(that.Leak());
101     return *this;
102   }
103 
104   ~RetainPtr() = default;
105 
106   template <class U>
AsRaw()107   U* AsRaw() const {
108     return static_cast<U*>(Get());
109   }
110 
111   template <class U>
As()112   RetainPtr<U> As() const {
113     return RetainPtr<U>(AsRaw<U>());
114   }
115 
116   void Reset(T* obj = nullptr) {
117     if (obj)
118       obj->Retain();
119     m_pObj.reset(obj);
120   }
121 
122   operator T*() const noexcept { return Get(); }
Get()123   T* Get() const noexcept { return m_pObj.get(); }
124 
BackPointer()125   UnownedPtr<T> BackPointer() const { return UnownedPtr<T>(Get()); }
Swap(RetainPtr & that)126   void Swap(RetainPtr& that) { m_pObj.swap(that.m_pObj); }
127 
128   // Useful for passing notion of object ownership across a C API.
Leak()129   T* Leak() { return m_pObj.release(); }
Unleak(T * ptr)130   void Unleak(T* ptr) { m_pObj.reset(ptr); }
131 
132   bool operator==(const RetainPtr& that) const { return Get() == that.Get(); }
133   bool operator!=(const RetainPtr& that) const { return !(*this == that); }
134 
135   template <typename U>
136   bool operator==(const U& that) const {
137     return Get() == that;
138   }
139 
140   template <typename U>
141   bool operator!=(const U& that) const {
142     return !(*this == that);
143   }
144 
145   bool operator<(const RetainPtr& that) const {
146     return std::less<T*>()(Get(), that.Get());
147   }
148 
149   explicit operator bool() const { return !!m_pObj; }
150   T& operator*() const { return *m_pObj; }
151   T* operator->() const { return m_pObj.get(); }
152 
153  private:
154   std::unique_ptr<T, ReleaseDeleter<T>> m_pObj;
155 };
156 
157 // Trivial implementation - internal ref count with virtual destructor.
158 class Retainable {
159  public:
160   Retainable() = default;
161 
HasOneRef()162   bool HasOneRef() const { return m_nRefCount == 1; }
163 
164  protected:
165   virtual ~Retainable() = default;
166 
167  private:
168   template <typename U>
169   friend struct ReleaseDeleter;
170 
171   template <typename U>
172   friend class RetainPtr;
173 
174   Retainable(const Retainable& that) = delete;
175   Retainable& operator=(const Retainable& that) = delete;
176 
177   // These need to be const methods operating on a mutable member so that
178   // RetainPtr<const T> can be used for an object that is otherwise const
179   // apart from the internal ref-counting.
Retain()180   void Retain() const {
181     ++m_nRefCount;
182     CHECK(m_nRefCount > 0);
183   }
Release()184   void Release() const {
185     CHECK(m_nRefCount > 0);
186     if (--m_nRefCount == 0)
187       delete this;
188   }
189 
190   mutable uintptr_t m_nRefCount = 0;
191   static_assert(std::is_unsigned<decltype(m_nRefCount)>::value,
192                 "m_nRefCount must be an unsigned type for overflow check"
193                 "to work properly in Retain()");
194 };
195 
196 }  // namespace fxcrt
197 
198 using fxcrt::ReleaseDeleter;
199 using fxcrt::Retainable;
200 using fxcrt::RetainPtr;
201 
202 namespace pdfium {
203 
204 // Helper to make a RetainPtr along the lines of std::make_unique<>().
205 // Arguments are forwarded to T's constructor. Classes managed by RetainPtr
206 // should have protected (or private) constructors, and should friend this
207 // function.
208 template <typename T, typename... Args>
MakeRetain(Args &&...args)209 RetainPtr<T> MakeRetain(Args&&... args) {
210   return RetainPtr<T>(new T(std::forward<Args>(args)...));
211 }
212 
213 // Type-deducing wrapper to make a RetainPtr from an ordinary pointer,
214 // since equivalent constructor is explicit.
215 template <typename T>
WrapRetain(T * that)216 RetainPtr<T> WrapRetain(T* that) {
217   return RetainPtr<T>(that);
218 }
219 
220 }  // namespace pdfium
221 
222 // Macro to allow construction via MakeRetain<>() only, when used
223 // with a private constructor in a class.
224 #define CONSTRUCT_VIA_MAKE_RETAIN         \
225   template <typename T, typename... Args> \
226   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args)
227 
228 #endif  // CORE_FXCRT_RETAIN_PTR_H_
229