Chromium Code Reviews| 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 <cassert> | 8 #include <cassert> |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 | 10 |
| 11 #include "base/atomic_ref_count.h" | 11 #include "base/atomic_ref_count.h" |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #ifndef NDEBUG | 14 #ifndef NDEBUG |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #endif | 16 #endif |
| 17 #include "base/move.h" | |
| 18 #include "base/threading/thread_collision_warner.h" | 17 #include "base/threading/thread_collision_warner.h" |
| 19 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 20 | 19 |
| 21 namespace base { | 20 namespace base { |
| 22 | 21 |
| 23 namespace subtle { | 22 namespace subtle { |
| 24 | 23 |
| 25 class BASE_EXPORT RefCountedBase { | 24 class BASE_EXPORT RefCountedBase { |
| 26 public: | 25 public: |
| 27 bool HasOneRef() const { return ref_count_ == 1; } | 26 bool HasOneRef() const { return ref_count_ == 1; } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 // { | 257 // { |
| 259 // scoped_refptr<MyFoo> a = new MyFoo(); | 258 // scoped_refptr<MyFoo> a = new MyFoo(); |
| 260 // scoped_refptr<MyFoo> b; | 259 // scoped_refptr<MyFoo> b; |
| 261 // | 260 // |
| 262 // b = a; | 261 // b = a; |
| 263 // // now, |a| and |b| each own a reference to the same MyFoo object. | 262 // // now, |a| and |b| each own a reference to the same MyFoo object. |
| 264 // } | 263 // } |
| 265 // | 264 // |
| 266 template <class T> | 265 template <class T> |
| 267 class scoped_refptr { | 266 class scoped_refptr { |
| 268 TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_refptr) | |
| 269 public: | 267 public: |
| 270 typedef T element_type; | 268 typedef T element_type; |
| 271 | 269 |
| 272 scoped_refptr() : ptr_(NULL) { | 270 scoped_refptr() : ptr_(NULL) { |
| 273 } | 271 } |
| 274 | 272 |
| 275 scoped_refptr(T* p) : ptr_(p) { | 273 scoped_refptr(T* p) : ptr_(p) { |
| 276 if (ptr_) | 274 if (ptr_) |
| 277 AddRef(ptr_); | 275 AddRef(ptr_); |
| 278 } | 276 } |
| 279 | 277 |
| 280 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 278 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
| 281 if (ptr_) | 279 if (ptr_) |
| 282 AddRef(ptr_); | 280 AddRef(ptr_); |
| 283 } | 281 } |
| 284 | 282 |
| 285 template <typename U> | 283 template <typename U> |
| 286 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { | 284 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { |
| 287 if (ptr_) | 285 if (ptr_) |
| 288 AddRef(ptr_); | 286 AddRef(ptr_); |
| 289 } | 287 } |
| 290 | 288 |
| 291 template <typename U> | 289 template <typename U> |
| 292 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.get()) { | 290 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.get()) { |
|
dcheng
2015/11/25 06:28:09
Would it make sense to introduce a real move const
danakj
2015/11/25 19:35:56
Sure, done. Also for skia::RefPtr.
| |
| 293 r.ptr_ = nullptr; | 291 r.ptr_ = nullptr; |
| 294 } | 292 } |
| 295 | 293 |
| 296 ~scoped_refptr() { | 294 ~scoped_refptr() { |
| 297 if (ptr_) | 295 if (ptr_) |
| 298 Release(ptr_); | 296 Release(ptr_); |
| 299 } | 297 } |
| 300 | 298 |
| 301 T* get() const { return ptr_; } | 299 T* get() const { return ptr_; } |
| 302 | 300 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 429 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 432 return !operator==(lhs, rhs); | 430 return !operator==(lhs, rhs); |
| 433 } | 431 } |
| 434 | 432 |
| 435 template <typename T> | 433 template <typename T> |
| 436 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 434 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 437 return out << p.get(); | 435 return out << p.get(); |
| 438 } | 436 } |
| 439 | 437 |
| 440 #endif // BASE_MEMORY_REF_COUNTED_H_ | 438 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |