Flutter Engine
The 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"
16#include "flutter/fml/memory/ref_ptr_internal.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 if (ptr_) {
111 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete)
112 ptr_->Release();
113 }
114 }
115
116 T* get() const { return ptr_; }
117
118 T& operator*() const {
119 FML_DCHECK(ptr_);
120 return *ptr_;
121 }
122
123 T* operator->() const {
124 FML_DCHECK(ptr_);
125 return ptr_;
126 }
127
128 // Copy assignment.
130 // Handle self-assignment.
131 if (r.ptr_ == ptr_) {
132 return *this;
133 }
134 if (r.ptr_) {
135 r.ptr_->AddRef();
136 }
137 T* old_ptr = ptr_;
138 ptr_ = r.ptr_;
139 if (old_ptr) {
140 old_ptr->Release();
141 }
142 return *this;
143 }
144
145 template <typename U>
147 if (reinterpret_cast<T*>(r.ptr_) == ptr_) {
148 return *this;
149 }
150 if (r.ptr_) {
151 r.ptr_->AddRef();
152 }
153 T* old_ptr = ptr_;
154 ptr_ = r.ptr_;
155 if (old_ptr) {
156 old_ptr->Release();
157 }
158 return *this;
159 }
160
161 // Move assignment.
162 // Note: Like |std::shared_ptr|, we support self-move and move assignment is
163 // equivalent to |RefPtr<T>(std::move(r)).swap(*this)|.
165 RefPtr<T>(std::move(r)).swap(*this);
166 return *this;
167 }
168
169 template <typename U>
171 RefPtr<T>(std::move(r)).swap(*this);
172 return *this;
173 }
174
175 void swap(RefPtr<T>& r) {
176 T* p = ptr_;
177 ptr_ = r.ptr_;
178 r.ptr_ = p;
179 }
180
181 // Returns a new |RefPtr<T>| with the same contents as this pointer. Useful
182 // when a function takes a |RefPtr<T>&&| argument and the caller wants to
183 // retain its reference (rather than moving it).
184 RefPtr<T> Clone() const { return *this; }
185
186 explicit operator bool() const { return !!ptr_; }
187
188 template <typename U>
189 bool operator==(const RefPtr<U>& rhs) const {
190 return ptr_ == rhs.ptr_;
191 }
192
193 template <typename U>
194 bool operator!=(const RefPtr<U>& rhs) const {
195 return !operator==(rhs);
196 }
197
198 template <typename U>
199 bool operator<(const RefPtr<U>& rhs) const {
200 return ptr_ < rhs.ptr_;
201 }
202
203 private:
204 template <typename U>
205 friend class RefPtr;
206
207 friend RefPtr<T> AdoptRef<T>(T*);
208
209 enum AdoptTag { kAdopt };
210 RefPtr(T* ptr, AdoptTag) : ptr_(ptr) { FML_DCHECK(ptr_); }
211
212 T* ptr_;
213};
214
215// Adopts a newly-created |T|. Typically used in a static factory method, like:
216//
217// // static
218// RefPtr<Foo> Foo::Create() {
219// return AdoptRef(new Foo());
220// }
221template <typename T>
222inline RefPtr<T> AdoptRef(T* ptr) {
223#ifndef NDEBUG
224 ptr->Adopt();
225#endif
226 return RefPtr<T>(ptr, RefPtr<T>::kAdopt);
227}
228
229// Constructs a |RefPtr<T>| from a plain pointer (to an object that must
230// have already been adoped). Avoids having to spell out the full type name.
231//
232// Foo* foo = ...;
233// auto foo_ref = Ref(foo);
234//
235// (|foo_ref| will be of type |RefPtr<Foo>|.)
236template <typename T>
237inline RefPtr<T> Ref(T* ptr) {
238 return RefPtr<T>(ptr);
239}
240
241// Creates an intrusively reference counted |T|, producing a |RefPtr<T>| (and
242// performing the required adoption). Use like:
243//
244// auto my_foo = MakeRefCounted<Foo>(ctor_arg1, ctor_arg2);
245//
246// (|my_foo| will be of type |RefPtr<Foo>|.)
247template <typename T, typename... Args>
250 std::forward<Args>(args)...);
251}
252
253} // namespace fml
254
255// Inject custom std::hash<> function object for |RefPtr<T>|.
256namespace std {
257template <typename T>
258struct hash<fml::RefPtr<T>> {
259 using argument_type = fml::RefPtr<T>;
260 using result_type = std::size_t;
261
262 result_type operator()(const argument_type& ptr) const {
263 return std::hash<T*>()(ptr.get());
264 }
265};
266} // namespace std
267
268#endif // FLUTTER_FML_MEMORY_REF_PTR_H_
static uint32_t hash(const SkShaderBase::GradientInfo &v)
bool operator!=(const RefPtr< U > &rhs) const
Definition ref_ptr.h:194
RefPtr< T > & operator=(RefPtr< T > &&r)
Definition ref_ptr.h:164
friend class RefPtr
Definition ref_ptr.h:205
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:129
bool operator<(const RefPtr< U > &rhs) const
Definition ref_ptr.h:199
RefPtr(RefPtr< T > &&r)
Definition ref_ptr.h:99
T * operator->() const
Definition ref_ptr.h:123
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:175
bool operator==(const RefPtr< U > &rhs) const
Definition ref_ptr.h:189
T & operator*() const
Definition ref_ptr.h:118
RefPtr< T > & operator=(RefPtr< U > &&r)
Definition ref_ptr.h:170
RefPtr< T > & operator=(const RefPtr< U > &r)
Definition ref_ptr.h:146
RefPtr< T > Clone() const
Definition ref_ptr.h:184
T * get() const
Definition ref_ptr.h:116
static RefPtr< T > MakeRefCounted(Args &&... args)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_DCHECK(condition)
Definition logging.h:103
RefPtr< T > Ref(T *ptr)
Definition ref_ptr.h:237
RefPtr< T > MakeRefCounted(Args &&... args)
Definition ref_ptr.h:248
RefPtr< T > AdoptRef(T *ptr)
Definition ref_ptr.h:222
Definition ref_ptr.h:256
#define T