1 // Copyright 2016 PDFium Authors. All rights reserved.
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 <functional>
9 #include <memory>
10 #include <utility>
11 
12 #include "core/fxcrt/fx_system.h"
13 #include "core/fxcrt/unowned_ptr.h"
14 
15 namespace fxcrt {
16 
17 // Used with std::unique_ptr to Release() objects that can't be deleted.
18 template <class T>
19 struct ReleaseDeleter {
operatorReleaseDeleter20   inline void operator()(T* ptr) const { ptr->Release(); }
21 };
22 
23 // Analogous to base's scoped_refptr.
24 template <class T>
25 class RetainPtr {
26  public:
RetainPtr(T * pObj)27   explicit RetainPtr(T* pObj) : m_pObj(pObj) {
28     if (m_pObj)
29       m_pObj->Retain();
30   }
31 
32   RetainPtr() = default;
RetainPtr(const RetainPtr & that)33   RetainPtr(const RetainPtr& that) : RetainPtr(that.Get()) {}
34 
35   // Move-construct a RetainPtr. After construction, |that| will be NULL.
RetainPtr(RetainPtr && that)36   RetainPtr(RetainPtr&& that) noexcept { Swap(that); }
37 
38   // Deliberately implicit to allow returning nullptrs.
39   // NOLINTNEXTLINE(runtime/explicit)
RetainPtr(std::nullptr_t ptr)40   RetainPtr(std::nullptr_t ptr) {}
41 
42   template <class U>
RetainPtr(const RetainPtr<U> & that)43   RetainPtr(const RetainPtr<U>& that) : RetainPtr(that.Get()) {}
44 
45   template <class U>
As()46   RetainPtr<U> As() const {
47     return RetainPtr<U>(static_cast<U*>(Get()));
48   }
49 
50   void Reset(T* obj = nullptr) {
51     if (obj)
52       obj->Retain();
53     m_pObj.reset(obj);
54   }
55 
Get()56   T* Get() const { return m_pObj.get(); }
BackPointer()57   UnownedPtr<T> BackPointer() const { return UnownedPtr<T>(Get()); }
Swap(RetainPtr & that)58   void Swap(RetainPtr& that) { m_pObj.swap(that.m_pObj); }
59 
60   // Useful for passing notion of object ownership across a C API.
Leak()61   T* Leak() { return m_pObj.release(); }
Unleak(T * ptr)62   void Unleak(T* ptr) { m_pObj.reset(ptr); }
63 
64   RetainPtr& operator=(const RetainPtr& that) {
65     if (*this != that)
66       Reset(that.Get());
67     return *this;
68   }
69 
70   // Move-assign a RetainPtr. After assignment, |that| will be NULL.
71   RetainPtr& operator=(RetainPtr&& that) {
72     m_pObj.reset(that.Leak());
73     return *this;
74   }
75 
76   // Assigment from raw pointers is intentially not provided to make
77   // reference count churn more visible where possible.
78 
79   bool operator==(const RetainPtr& that) const { return Get() == that.Get(); }
80   bool operator!=(const RetainPtr& that) const { return !(*this == that); }
81 
82   template <typename U>
83   bool operator==(const U& that) const {
84     return Get() == that;
85   }
86 
87   template <typename U>
88   bool operator!=(const U& that) const {
89     return !(*this == that);
90   }
91 
92   bool operator<(const RetainPtr& that) const {
93     return std::less<T*>()(Get(), that.Get());
94   }
95 
96   explicit operator bool() const { return !!m_pObj; }
97   T& operator*() const { return *m_pObj; }
98   T* operator->() const { return m_pObj.get(); }
99 
100  private:
101   std::unique_ptr<T, ReleaseDeleter<T>> m_pObj;
102 };
103 
104 // Trivial implementation - internal ref count with virtual destructor.
105 class Retainable {
106  public:
107   Retainable() = default;
108 
HasOneRef()109   bool HasOneRef() const { return m_nRefCount == 1; }
110 
111  protected:
112   virtual ~Retainable() = default;
113 
114  private:
115   template <typename U>
116   friend struct ReleaseDeleter;
117 
118   template <typename U>
119   friend class RetainPtr;
120 
121   Retainable(const Retainable& that) = delete;
122   Retainable& operator=(const Retainable& that) = delete;
123 
Retain()124   void Retain() const { ++m_nRefCount; }
Release()125   void Release() const {
126     ASSERT(m_nRefCount > 0);
127     if (--m_nRefCount == 0)
128       delete this;
129   }
130 
131   mutable intptr_t m_nRefCount = 0;
132 };
133 
134 template <typename T, typename U>
135 inline bool operator==(const U* lhs, const RetainPtr<T>& rhs) {
136   return rhs == lhs;
137 }
138 
139 template <typename T, typename U>
140 inline bool operator!=(const U* lhs, const RetainPtr<T>& rhs) {
141   return rhs != lhs;
142 }
143 
144 }  // namespace fxcrt
145 
146 using fxcrt::ReleaseDeleter;
147 using fxcrt::Retainable;
148 using fxcrt::RetainPtr;
149 
150 namespace pdfium {
151 
152 // Helper to make a RetainPtr along the lines of std::make_unique<>(),
153 // or pdfium::MakeUnique<>(). Arguments are forwarded to T's constructor.
154 // Classes managed by RetainPtr should have protected (or private)
155 // constructors, and should friend this function.
156 template <typename T, typename... Args>
MakeRetain(Args &&...args)157 RetainPtr<T> MakeRetain(Args&&... args) {
158   return RetainPtr<T>(new T(std::forward<Args>(args)...));
159 }
160 
161 // Type-deducing wrapper to make a RetainPtr from an ordinary pointer.
162 template <typename T>
WrapRetain(T * that)163 RetainPtr<T> WrapRetain(T* that) {
164   return RetainPtr<T>(that);
165 }
166 
167 }  // namespace pdfium
168 
169 #endif  // CORE_FXCRT_RETAIN_PTR_H_
170