OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <cassert> | 10 #include <cassert> |
(...skipping 13 matching lines...) Expand all Loading... |
24 namespace subtle { | 24 namespace subtle { |
25 | 25 |
26 class BASE_EXPORT RefCountedBase { | 26 class BASE_EXPORT RefCountedBase { |
27 public: | 27 public: |
28 bool HasOneRef() const { return ref_count_ == 1; } | 28 bool HasOneRef() const { return ref_count_ == 1; } |
29 | 29 |
30 protected: | 30 protected: |
31 RefCountedBase() | 31 RefCountedBase() |
32 : ref_count_(0) | 32 : ref_count_(0) |
33 #if DCHECK_IS_ON() | 33 #if DCHECK_IS_ON() |
34 , in_dtor_(false) | 34 , in_dtor_(false) |
35 #endif | 35 #endif |
36 { | 36 { |
37 } | 37 } |
38 | 38 |
39 ~RefCountedBase() { | 39 ~RefCountedBase() { |
40 #if DCHECK_IS_ON() | 40 #if DCHECK_IS_ON() |
41 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; | 41 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
42 #endif | 42 #endif |
43 } | 43 } |
44 | 44 |
45 | 45 |
46 void AddRef() const { | 46 void AddRef() const { |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 // | 262 // |
263 // b = a; | 263 // b = a; |
264 // // now, |a| and |b| each own a reference to the same MyFoo object. | 264 // // now, |a| and |b| each own a reference to the same MyFoo object. |
265 // } | 265 // } |
266 // | 266 // |
267 template <class T> | 267 template <class T> |
268 class scoped_refptr { | 268 class scoped_refptr { |
269 public: | 269 public: |
270 typedef T element_type; | 270 typedef T element_type; |
271 | 271 |
272 scoped_refptr() : ptr_(nullptr) { | 272 scoped_refptr() {} |
273 } | |
274 | 273 |
275 scoped_refptr(T* p) : ptr_(p) { | 274 scoped_refptr(T* p) : ptr_(p) { |
276 if (ptr_) | 275 if (ptr_) |
277 AddRef(ptr_); | 276 AddRef(ptr_); |
278 } | 277 } |
279 | 278 |
280 // Copy constructor. | 279 // Copy constructor. |
281 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 280 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
282 if (ptr_) | 281 if (ptr_) |
283 AddRef(ptr_); | 282 AddRef(ptr_); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 bool operator!=(const scoped_refptr<U>& rhs) const { | 372 bool operator!=(const scoped_refptr<U>& rhs) const { |
374 return !operator==(rhs); | 373 return !operator==(rhs); |
375 } | 374 } |
376 | 375 |
377 template <typename U> | 376 template <typename U> |
378 bool operator<(const scoped_refptr<U>& rhs) const { | 377 bool operator<(const scoped_refptr<U>& rhs) const { |
379 return ptr_ < rhs.get(); | 378 return ptr_ < rhs.get(); |
380 } | 379 } |
381 | 380 |
382 protected: | 381 protected: |
383 T* ptr_; | 382 T* ptr_ = nullptr; |
384 | 383 |
385 private: | 384 private: |
386 // Friend required for move constructors that set r.ptr_ to null. | 385 // Friend required for move constructors that set r.ptr_ to null. |
387 template <typename U> | 386 template <typename U> |
388 friend class scoped_refptr; | 387 friend class scoped_refptr; |
389 | 388 |
390 // Non-inline helpers to allow: | 389 // Non-inline helpers to allow: |
391 // class Opaque; | 390 // class Opaque; |
392 // extern template class scoped_refptr<Opaque>; | 391 // extern template class scoped_refptr<Opaque>; |
393 // Otherwise the compiler will complain that Opaque is an incomplete type. | 392 // Otherwise the compiler will complain that Opaque is an incomplete type. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 450 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
452 return !operator==(null, rhs); | 451 return !operator==(null, rhs); |
453 } | 452 } |
454 | 453 |
455 template <typename T> | 454 template <typename T> |
456 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 455 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
457 return out << p.get(); | 456 return out << p.get(); |
458 } | 457 } |
459 | 458 |
460 #endif // BASE_MEMORY_REF_COUNTED_H_ | 459 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |