| 6 | #define BASE_MEMORY_SCOPED_REFPTR_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include <iosfwd> |
| 11 | #include <type_traits> |
| 12 | #include <utility> |
| 13 |
| 14 | #include "base/compiler_specific.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/macros.h" |
| 17 |
| 18 | template <class T> |
| 19 | class scoped_refptr; |
| 20 |
| 21 | namespace base { |
| 22 |
| 23 | template <class, typename> |
| 24 | class RefCounted; |
| 25 | template <class, typename> |
| 26 | class RefCountedThreadSafe; |
| 27 | class SequencedTaskRunner; |
| 28 | class WrappedPromise; |
| 29 |
| 30 | template <typename T> |
| 31 | scoped_refptr<T> AdoptRef(T* t); |
| 32 |
| 33 | namespace internal { |
| 34 |
| 35 | class BasePromise; |
| 36 |
| 37 | } // namespace internal |
| 38 |
| 39 | namespace subtle { |
| 40 |
| 41 | enum AdoptRefTag { kAdoptRefTag }; |
| 42 | enum StartRefCountFromZeroTag { kStartRefCountFromZeroTag }; |
| 43 | enum StartRefCountFromOneTag { kStartRefCountFromOneTag }; |
| 44 |
| 45 | template <typename T, typename U, typename V> |
| 46 | constexpr bool IsRefCountPreferenceOverridden(const T*, |
| 47 | const RefCounted<U, V>*) { |
| 48 | return !std::is_same<std::decay_t<decltype(T::kRefCountPreference)>, |
| 49 | std::decay_t<decltype(U::kRefCountPreference)>>::value; |
| 50 | } |
| 51 |
| 52 | template <typename T, typename U, typename V> |
| 53 | constexpr bool IsRefCountPreferenceOverridden( |
| 54 | const T*, |
| 55 | const RefCountedThreadSafe<U, V>*) { |
| 56 | return !std::is_same<std::decay_t<decltype(T::kRefCountPreference)>, |
| 57 | std::decay_t<decltype(U::kRefCountPreference)>>::value; |
| 58 | } |
| 59 |
| 60 | constexpr bool IsRefCountPreferenceOverridden(...) { |
| 61 | return false; |
| 62 | } |
| 63 |
| 64 | } // namespace subtle |
| 65 |
| 66 | // Creates a scoped_refptr from a raw pointer without incrementing the reference |
| 67 | // count. Use this only for a newly created object whose reference count starts |
| 68 | // from 1 instead of 0. |
| 69 | template <typename T> |
| 70 | scoped_refptr<T> AdoptRef(T* obj) { |
| 71 | using Tag = std::decay_t<decltype(T::kRefCountPreference)>; |
| 72 | static_assert(std::is_same<subtle::StartRefCountFromOneTag, Tag>::value, |
| 73 | "Use AdoptRef only if the reference count starts from one."); |
| 74 |
| 75 | DCHECK(obj); |
| 76 | DCHECK(obj->HasOneRef()); |
| 77 | obj->Adopted(); |
| 78 | return scoped_refptr<T>(obj, subtle::kAdoptRefTag); |
| 79 | } |
| 80 |
| 81 | namespace subtle { |
| 82 |
| 83 | template <typename T> |
| 84 | scoped_refptr<T> AdoptRefIfNeeded(T* obj, StartRefCountFromZeroTag) { |
| 85 | return scoped_refptr<T>(obj); |
| 86 | } |
| 87 |
| 88 | template <typename T> |
| 89 | scoped_refptr<T> AdoptRefIfNeeded(T* obj, StartRefCountFromOneTag) { |
| 90 | return AdoptRef(obj); |
| 91 | } |
| 92 |
| 93 | } // namespace subtle |
| 94 |
| 95 | // Constructs an instance of T, which is a ref counted type, and wraps the |
| 96 | // object into a scoped_refptr<T>. |
| 97 | template <typename T, typename... Args> |
| 98 | scoped_refptr<T> MakeRefCounted(Args&&... args) { |
| 99 | T* obj = new T(std::forward<Args>(args)...); |
| 100 | return subtle::AdoptRefIfNeeded(obj, T::kRefCountPreference); |
| 101 | } |
| 102 |
| 103 | // Takes an instance of T, which is a ref counted type, and wraps the object |
| 104 | // into a scoped_refptr<T>. |
| 105 | template <typename T> |
| 106 | scoped_refptr<T> WrapRefCounted(T* t) { |
| 107 | return scoped_refptr<T>(t); |
| 108 | } |
| 109 |
| 110 | } // namespace base |
| 111 |
| 112 | // |
| 113 | // A smart pointer class for reference counted objects. Use this class instead |
| 114 | // of calling AddRef and Release manually on a reference counted object to |
| 115 | // avoid common memory leaks caused by forgetting to Release an object |
| 116 | // reference. Sample usage: |
| 117 | // |
| 118 | // class MyFoo : public RefCounted<MyFoo> { |
| 119 | // ... |
| 120 | // private: |
| 121 | // friend class RefCounted<MyFoo>; // Allow destruction by RefCounted<>. |
| 122 | // ~MyFoo(); // Destructor must be private/protected. |
| 123 | // }; |
| 124 | // |
| 125 | // void some_function() { |
| 126 | // scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>(); |
| 127 | // foo->Method(param); |
| 128 | // // |foo| is released when this function returns |
| 129 | // } |
| 130 | // |
| 131 | // void some_other_function() { |
| 132 | // scoped_refptr<MyFoo> foo = MakeRefCounted<MyFoo>(); |
| 133 | // ... |
| 134 | // foo.reset(); // explicitly releases |foo| |
| 135 | // ... |
| 136 | // if (foo) |
| 137 | // foo->Method(param); |
| 138 | // } |
| 139 | // |
| 140 | // The above examples show how scoped_refptr<T> acts like a pointer to T. |
| 141 | // Given two scoped_refptr<T> classes, it is also possible to exchange |
| 142 | // references between the two objects, like so: |
| 143 | // |
| 144 | // { |
| 145 | // scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>(); |
| 146 | // scoped_refptr<MyFoo> b; |
| 147 | // |
| 148 | // b.swap(a); |
| 149 | // // now, |b| references the MyFoo object, and |a| references nullptr. |
| 150 | // } |
| 151 | // |
| 152 | // To make both |a| and |b| in the above example reference the same MyFoo |
| 153 | // object, simply use the assignment operator: |
| 154 | // |
| 155 | // { |
| 156 | // scoped_refptr<MyFoo> a = MakeRefCounted<MyFoo>(); |
| 157 | // scoped_refptr<MyFoo> b; |
| 158 | // |
| 159 | // b = a; |
| 160 | // // now, |a| and |b| each own a reference to the same MyFoo object. |
| 161 | // } |
| 162 | // |
| 163 | // Also see Chromium's ownership and calling conventions: |
| 164 | // https://chromium.googlesource.com/chromium/src/+/lkgr/styleguide/c++/c++.md#object-ownership-and-calling-conventions |
| 165 | // Specifically: |
| 166 | // If the function (at least sometimes) takes a ref on a refcounted object, |
| 167 | // declare the param as scoped_refptr<T>. The caller can decide whether it |
| 168 | // wishes to transfer ownership (by calling std::move(t) when passing t) or |
| 169 | // retain its ref (by simply passing t directly). |
| 170 | // In other words, use scoped_refptr like you would a std::unique_ptr except |
| 171 | // in the odd case where it's required to hold on to a ref while handing one |
| 172 | // to another component (if a component merely needs to use t on the stack |
| 173 | // without keeping a ref: pass t as a raw T*). |
| 174 | template <class T> |
| 175 | class scoped_refptr { |
| 176 | public: |
| 177 | typedef T element_type; |
| 178 |
| 179 | constexpr scoped_refptr() = default; |
| 180 |
| 181 | // Allow implicit construction from nullptr. |
| 182 | constexpr scoped_refptr(std::nullptr_t) {} |
| 183 |
| 184 | // Constructs from a raw pointer. Note that this constructor allows implicit |
| 185 | // conversion from T* to scoped_refptr<T> which is strongly discouraged. If |
| 186 | // you are creating a new ref-counted object please use |
| 187 | // base::MakeRefCounted<T>() or base::WrapRefCounted<T>(). Otherwise you |
| 188 | // should move or copy construct from an existing scoped_refptr<T> to the |
| 189 | // ref-counted object. |
| 190 | scoped_refptr(T* p) : ptr_(p) { |
| 191 | if (ptr_) |
| 192 | AddRef(ptr_); |
| 193 | } |
| 194 |
| 195 | // Copy constructor. This is required in addition to the copy conversion |
| 196 | // constructor below. |
| 197 | scoped_refptr(const scoped_refptr& r) : scoped_refptr(r.ptr_) {} |
| 198 |
| 199 | // Copy conversion constructor. |
| 200 | template <typename U, |
| 201 | typename = typename std::enable_if< |
| 202 | std::is_convertible<U*, T*>::value>::type> |
| 203 | scoped_refptr(const scoped_refptr<U>& r) : scoped_refptr(r.ptr_) {} |
| 204 |
| 205 | // Move constructor. This is required in addition to the move conversion |
| 206 | // constructor below. |
| 207 | scoped_refptr(scoped_refptr&& r) noexcept : ptr_(r.ptr_) { r.ptr_ = nullptr; } |
| 208 |
| 209 | // Move conversion constructor. |
| 210 | template <typename U, |
| 211 | typename = typename std::enable_if< |
| 212 | std::is_convertible<U*, T*>::value>::type> |
| 213 | scoped_refptr(scoped_refptr<U>&& r) noexcept : ptr_(r.ptr_) { |
| 214 | r.ptr_ = nullptr; |
| 215 | } |
| 216 |
| 217 | ~scoped_refptr() { |
| 218 | static_assert(!base::subtle::IsRefCountPreferenceOverridden( |
| 219 | static_cast<T*>(nullptr), static_cast<T*>(nullptr)), |
| 220 | "It's unsafe to override the ref count preference." |
| 221 | " Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE" |
| 222 | " from subclasses."); |
| 223 | if (ptr_) |
| 224 | Release(ptr_); |
| 225 | } |
| 226 |
| 227 | T* get() const { return ptr_; } |
| 228 |
| 229 | T& operator*() const { |
| 230 | DCHECK(ptr_); |
| 231 | return *ptr_; |
| 232 | } |
| 233 |
| 234 | T* operator->() const { |
| 235 | DCHECK(ptr_); |
| 236 | return ptr_; |
| 237 | } |
| 238 |
| 239 | scoped_refptr& operator=(std::nullptr_t) { |
| 240 | reset(); |
| 241 | return *this; |
| 242 | } |
| 243 |
| 244 | scoped_refptr& operator=(T* p) { return *this = scoped_refptr(p); } |
| 245 |
| 246 | // Unified assignment operator. |
| 247 | scoped_refptr& operator=(scoped_refptr r) noexcept { |
| 248 | swap(r); |
| 249 | return *this; |
| 250 | } |
| 251 |
| 252 | // Sets managed object to null and releases reference to the previous managed |
| 253 | // object, if it existed. |
| 254 | void reset() { scoped_refptr().swap(*this); } |
| 255 |
| 256 | void swap(scoped_refptr& r) noexcept { std::swap(ptr_, r.ptr_); } |
| 257 |
| 258 | explicit operator bool() const { return ptr_ != nullptr; } |
| 259 |
| 260 | template <typename U> |
| 261 | bool operator==(const scoped_refptr<U>& rhs) const { |
| 262 | return ptr_ == rhs.get(); |
| 263 | } |
| 264 |
| 265 | template <typename U> |
| 266 | bool operator!=(const scoped_refptr<U>& rhs) const { |
| 267 | return !operator==(rhs); |
| 268 | } |
| 269 |
| 270 | template <typename U> |
| 271 | bool operator<(const scoped_refptr<U>& rhs) const { |
| 272 | return ptr_ < rhs.get(); |
| 273 | } |
| 274 |
| 275 | protected: |
| 276 | T* ptr_ = nullptr; |
| 277 |
| 278 | private: |
| 279 | template <typename U> |
| 280 | friend scoped_refptr<U> base::AdoptRef(U*); |
| 281 | friend class ::base::SequencedTaskRunner; |
| 282 |
| 283 | // Friend access so these classes can use the constructor below as part of a |
| 284 | // binary size optimization. |
| 285 | friend class ::base::internal::BasePromise; |
| 286 | friend class ::base::WrappedPromise; |
| 287 |
| 288 | // Returns the owned pointer (if any), releasing ownership to the caller. The |
| 289 | // caller is responsible for managing the lifetime of the reference. |
| 290 | T* release(); |
| 291 |
| 292 | scoped_refptr(T* p, base::subtle::AdoptRefTag) : ptr_(p) {} |
| 293 |
| 294 | // Friend required for move constructors that set r.ptr_ to null. |
| 295 | template <typename U> |
| 296 | friend class scoped_refptr; |
| 297 |
| 298 | // Non-inline helpers to allow: |
| 299 | // class Opaque; |
| 300 | // extern template class scoped_refptr<Opaque>; |
| 301 | // Otherwise the compiler will complain that Opaque is an incomplete type. |
| 302 | static void AddRef(T* ptr); |
| 303 | static void Release(T* ptr); |
| 304 | }; |
| 305 |
| 306 | template <typename T> |
| 307 | T* scoped_refptr<T>::release() { |
| 308 | T* ptr = ptr_; |
| 309 | ptr_ = nullptr; |
| 310 | return ptr; |
| 311 | } |
| 312 |
| 313 | // static |
| 314 | template <typename T> |
| 315 | void scoped_refptr<T>::AddRef(T* ptr) { |
| 316 | ptr->AddRef(); |
| 317 | } |
| 318 |
| 319 | // static |
| 320 | template <typename T> |
| 321 | void scoped_refptr<T>::Release(T* ptr) { |
| 322 | ptr->Release(); |
| 323 | } |
| 324 |
| 325 | template <typename T, typename U> |
| 326 | bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
| 327 | return lhs.get() == rhs; |
| 328 | } |
| 329 |
| 330 | template <typename T, typename U> |
| 331 | bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { |
| 332 | return lhs == rhs.get(); |
| 333 | } |
| 334 |
| 335 | template <typename T> |
| 336 | bool operator==(const scoped_refptr<T>& lhs, std::nullptr_t null) { |
| 337 | return !static_cast<bool>(lhs); |
| 338 | } |
| 339 |
| 340 | template <typename T> |
| 341 | bool operator==(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
| 342 | return !static_cast<bool>(rhs); |
| 343 | } |
| 344 |
| 345 | template <typename T, typename U> |
| 346 | bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { |
| 347 | return !operator==(lhs, rhs); |
| 348 | } |
| 349 |
| 350 | template <typename T, typename U> |
| 351 | bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 352 | return !operator==(lhs, rhs); |
| 353 | } |
| 354 |
| 355 | template <typename T> |
| 356 | bool operator!=(const scoped_refptr<T>& lhs, std::nullptr_t null) { |
| 357 | return !operator==(lhs, null); |
| 358 | } |
| 359 |
| 360 | template <typename T> |
| 361 | bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
| 362 | return !operator==(null, rhs); |
| 363 | } |
| 364 |
| 365 | template <typename T> |
| 366 | std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 367 | return out << p.get(); |
| 368 | } |
| 369 |
| 370 | template <typename T> |
| 371 | void swap(scoped_refptr<T>& lhs, scoped_refptr<T>& rhs) noexcept { |
| 372 | lhs.swap(rhs); |
| 373 | } |
| 374 |
| 375 | #endif // BASE_MEMORY_SCOPED_REFPTR_H_ |
| 376 |