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