1 // Copyright 2013 The Flutter 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 // Provides a smart pointer class for intrusively reference-counted objects.
6
7 #ifndef FLUTTER_FML_MEMORY_REF_PTR_H_
8 #define FLUTTER_FML_MEMORY_REF_PTR_H_
9
10 #include <cstddef>
11
12 #include <functional>
13 #include <utility>
14
15 #include "flutter/fml/logging.h"
16 #include "flutter/fml/macros.h"
17 #include "flutter/fml/memory/ref_ptr_internal.h"
18
19 namespace fml {
20
21 // A smart pointer class for intrusively reference-counted objects (e.g., those
22 // subclassing |RefCountedThreadSafe| -- see ref_counted.h).
23 //
24 // Such objects require *adoption* to obtain the first |RefPtr|, which is
25 // accomplished using |AdoptRef| (see below). (This is due to such objects being
26 // constructed with a reference count of 1. The adoption requirement is
27 // enforced, at least in Debug builds, by assertions.)
28 //
29 // E.g., if |Foo| is an intrusively reference-counted class:
30 //
31 // // The |AdoptRef| may be put in a static factory method (e.g., if |Foo|'s
32 // // constructor is private).
33 // RefPtr<Foo> my_foo_ptr(AdoptRef(new Foo()));
34 //
35 // // Now OK, since "my Foo" has been adopted ...
36 // RefPtr<Foo> another_ptr_to_my_foo(my_foo_ptr.get());
37 //
38 // // ... though this would preferable in this situation.
39 // RefPtr<Foo> yet_another_ptr_to_my_foo(my_foo_ptr);
40 //
41 // Unlike Chromium's |scoped_refptr|, |RefPtr| is only explicitly constructible
42 // from a plain pointer (and not assignable). It is however implicitly
43 // constructible from |nullptr|. So:
44 //
45 // RefPtr<Foo> foo(plain_ptr_to_adopted_foo); // OK.
46 // foo = plain_ptr_to_adopted_foo; // Not OK (doesn't compile).
47 // foo = RefPtr<Foo>(plain_ptr_to_adopted_foo); // OK.
48 // foo = nullptr; // OK.
49 //
50 // And if we have |void MyFunction(RefPtr<Foo> foo)|, calling it using
51 // |MyFunction(nullptr)| is also valid.
52 //
53 // Implementation note: For copy/move constructors/operator=s, we often have
54 // templated versions, so that the operation can be done on a |RefPtr<U>|, where
55 // |U| is a subclass of |T|. However, we also have non-templated versions with
56 // |U = T|, since the templated versions don't count as copy/move
57 // constructors/operator=s for the purposes of causing the default copy
58 // constructor/operator= to be deleted. E.g., if we didn't declare any
59 // non-templated versions, we'd get the default copy constructor/operator= (we'd
60 // only not get the default move constructor/operator= by virtue of having a
61 // destructor)! (In fact, it'd suffice to only declare a non-templated move
62 // constructor or move operator=, which would cause the copy
63 // constructor/operator= to be deleted, but for clarity we include explicit
64 // non-templated versions of everything.)
65 template <typename T>
66 class RefPtr final {
67 public:
RefPtr()68 RefPtr() : ptr_(nullptr) {}
RefPtr(std::nullptr_t)69 RefPtr(std::nullptr_t) : ptr_(nullptr) {}
70
71 // Explicit constructor from a plain pointer (to an object that must have
72 // already been adopted). (Note that in |T::T()|, references to |this| cannot
73 // be taken, since the object being constructed will not have been adopted
74 // yet.)
75 template <typename U>
RefPtr(U * p)76 explicit RefPtr(U* p) : ptr_(p) {
77 if (ptr_)
78 ptr_->AddRef();
79 }
80
81 // Copy constructor.
RefPtr(const RefPtr<T> & r)82 RefPtr(const RefPtr<T>& r) : ptr_(r.ptr_) {
83 if (ptr_)
84 ptr_->AddRef();
85 }
86
87 template <typename U>
RefPtr(const RefPtr<U> & r)88 RefPtr(const RefPtr<U>& r) : ptr_(r.ptr_) {
89 if (ptr_)
90 ptr_->AddRef();
91 }
92
93 // Move constructor.
RefPtr(RefPtr<T> && r)94 RefPtr(RefPtr<T>&& r) : ptr_(r.ptr_) { r.ptr_ = nullptr; }
95
96 template <typename U>
RefPtr(RefPtr<U> && r)97 RefPtr(RefPtr<U>&& r) : ptr_(r.ptr_) {
98 r.ptr_ = nullptr;
99 }
100
101 // Destructor.
~RefPtr()102 ~RefPtr() {
103 if (ptr_)
104 ptr_->Release();
105 }
106
get()107 T* get() const { return ptr_; }
108
109 T& operator*() const {
110 FML_DCHECK(ptr_);
111 return *ptr_;
112 }
113
114 T* operator->() const {
115 FML_DCHECK(ptr_);
116 return ptr_;
117 }
118
119 // Copy assignment.
120 RefPtr<T>& operator=(const RefPtr<T>& r) {
121 // Call |AddRef()| first so self-assignments work.
122 if (r.ptr_)
123 r.ptr_->AddRef();
124 T* old_ptr = ptr_;
125 ptr_ = r.ptr_;
126 if (old_ptr)
127 old_ptr->Release();
128 return *this;
129 }
130
131 template <typename U>
132 RefPtr<T>& operator=(const RefPtr<U>& r) {
133 // Call |AddRef()| first so self-assignments work.
134 if (r.ptr_)
135 r.ptr_->AddRef();
136 T* old_ptr = ptr_;
137 ptr_ = r.ptr_;
138 if (old_ptr)
139 old_ptr->Release();
140 return *this;
141 }
142
143 // Move assignment.
144 // Note: Like |std::shared_ptr|, we support self-move and move assignment is
145 // equivalent to |RefPtr<T>(std::move(r)).swap(*this)|.
146 RefPtr<T>& operator=(RefPtr<T>&& r) {
147 RefPtr<T>(std::move(r)).swap(*this);
148 return *this;
149 }
150
151 template <typename U>
152 RefPtr<T>& operator=(RefPtr<U>&& r) {
153 RefPtr<T>(std::move(r)).swap(*this);
154 return *this;
155 }
156
swap(RefPtr<T> & r)157 void swap(RefPtr<T>& r) {
158 T* p = ptr_;
159 ptr_ = r.ptr_;
160 r.ptr_ = p;
161 }
162
163 // Returns a new |RefPtr<T>| with the same contents as this pointer. Useful
164 // when a function takes a |RefPtr<T>&&| argument and the caller wants to
165 // retain its reference (rather than moving it).
Clone()166 RefPtr<T> Clone() const { return *this; }
167
168 explicit operator bool() const { return !!ptr_; }
169
170 template <typename U>
171 bool operator==(const RefPtr<U>& rhs) const {
172 return ptr_ == rhs.ptr_;
173 }
174
175 template <typename U>
176 bool operator!=(const RefPtr<U>& rhs) const {
177 return !operator==(rhs);
178 }
179
180 template <typename U>
181 bool operator<(const RefPtr<U>& rhs) const {
182 return ptr_ < rhs.ptr_;
183 }
184
185 private:
186 template <typename U>
187 friend class RefPtr;
188
189 friend RefPtr<T> AdoptRef<T>(T*);
190
191 enum AdoptTag { ADOPT };
RefPtr(T * ptr,AdoptTag)192 RefPtr(T* ptr, AdoptTag) : ptr_(ptr) { FML_DCHECK(ptr_); }
193
194 T* ptr_;
195 };
196
197 // Adopts a newly-created |T|. Typically used in a static factory method, like:
198 //
199 // // static
200 // RefPtr<Foo> Foo::Create() {
201 // return AdoptRef(new Foo());
202 // }
203 template <typename T>
AdoptRef(T * ptr)204 inline RefPtr<T> AdoptRef(T* ptr) {
205 #ifndef NDEBUG
206 ptr->Adopt();
207 #endif
208 return RefPtr<T>(ptr, RefPtr<T>::ADOPT);
209 }
210
211 // Constructs a |RefPtr<T>| from a plain pointer (to an object that must
212 // have already been adoped). Avoids having to spell out the full type name.
213 //
214 // Foo* foo = ...;
215 // auto foo_ref = Ref(foo);
216 //
217 // (|foo_ref| will be of type |RefPtr<Foo>|.)
218 template <typename T>
Ref(T * ptr)219 inline RefPtr<T> Ref(T* ptr) {
220 return RefPtr<T>(ptr);
221 }
222
223 // Creates an intrusively reference counted |T|, producing a |RefPtr<T>| (and
224 // performing the required adoption). Use like:
225 //
226 // auto my_foo = MakeRefCounted<Foo>(ctor_arg1, ctor_arg2);
227 //
228 // (|my_foo| will be of type |RefPtr<Foo>|.)
229 template <typename T, typename... Args>
MakeRefCounted(Args &&...args)230 RefPtr<T> MakeRefCounted(Args&&... args) {
231 return internal::MakeRefCountedHelper<T>::MakeRefCounted(
232 std::forward<Args>(args)...);
233 }
234
235 } // namespace fml
236
237 // Inject custom std::hash<> function object for |RefPtr<T>|.
238 namespace std {
239 template <typename T>
240 struct hash<fml::RefPtr<T>> {
241 using argument_type = fml::RefPtr<T>;
242 using result_type = std::size_t;
243
244 result_type operator()(const argument_type& ptr) const {
245 return std::hash<T*>()(ptr.get());
246 }
247 };
248 } // namespace std
249
250 #endif // FLUTTER_FML_MEMORY_REF_PTR_H_
251