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