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" | |
| 17 #include "base/threading/thread_collision_warner.h" | 18 #include "base/threading/thread_collision_warner.h" |
| 18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 | 22 |
| 22 namespace subtle { | 23 namespace subtle { |
| 23 | 24 |
| 24 class BASE_EXPORT RefCountedBase { | 25 class BASE_EXPORT RefCountedBase { |
| 25 public: | 26 public: |
| 26 bool HasOneRef() const { return ref_count_ == 1; } | 27 bool HasOneRef() const { return ref_count_ == 1; } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 // { | 258 // { |
| 258 // scoped_refptr<MyFoo> a = new MyFoo(); | 259 // scoped_refptr<MyFoo> a = new MyFoo(); |
| 259 // scoped_refptr<MyFoo> b; | 260 // scoped_refptr<MyFoo> b; |
| 260 // | 261 // |
| 261 // b = a; | 262 // b = a; |
| 262 // // now, |a| and |b| each own a reference to the same MyFoo object. | 263 // // now, |a| and |b| each own a reference to the same MyFoo object. |
| 263 // } | 264 // } |
| 264 // | 265 // |
| 265 template <class T> | 266 template <class T> |
| 266 class scoped_refptr { | 267 class scoped_refptr { |
| 268 TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_refptr) | |
| 267 public: | 269 public: |
| 268 typedef T element_type; | 270 typedef T element_type; |
| 269 | 271 |
| 270 scoped_refptr() : ptr_(NULL) { | 272 scoped_refptr() : ptr_(NULL) { |
| 271 } | 273 } |
| 272 | 274 |
| 273 scoped_refptr(T* p) : ptr_(p) { | 275 scoped_refptr(T* p) : ptr_(p) { |
| 274 if (ptr_) | 276 if (ptr_) |
| 275 AddRef(ptr_); | 277 AddRef(ptr_); |
| 276 } | 278 } |
| 277 | 279 |
| 278 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 280 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
|
Kibeom Kim (inactive)
2015/04/10 02:42:07
random Q: why do we need this? isn't #286 sufficie
| |
| 279 if (ptr_) | 281 if (ptr_) |
| 280 AddRef(ptr_); | 282 AddRef(ptr_); |
| 281 } | 283 } |
| 282 | 284 |
| 283 template <typename U> | 285 template <typename U> |
| 284 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { | 286 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { |
| 285 if (ptr_) | 287 if (ptr_) |
| 286 AddRef(ptr_); | 288 AddRef(ptr_); |
| 287 } | 289 } |
| 288 | 290 |
| 291 template <typename U> | |
| 292 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.get()) { | |
| 293 r.ptr_ = nullptr; | |
| 294 } | |
| 295 | |
| 289 ~scoped_refptr() { | 296 ~scoped_refptr() { |
| 290 if (ptr_) | 297 if (ptr_) |
| 291 Release(ptr_); | 298 Release(ptr_); |
| 292 } | 299 } |
| 293 | 300 |
| 294 T* get() const { return ptr_; } | 301 T* get() const { return ptr_; } |
| 295 | 302 |
| 296 T& operator*() const { | 303 T& operator*() const { |
| 297 assert(ptr_ != NULL); | 304 assert(ptr_ != NULL); |
| 298 return *ptr_; | 305 return *ptr_; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 316 | 323 |
| 317 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { | 324 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
| 318 return *this = r.ptr_; | 325 return *this = r.ptr_; |
| 319 } | 326 } |
| 320 | 327 |
| 321 template <typename U> | 328 template <typename U> |
| 322 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { | 329 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { |
| 323 return *this = r.get(); | 330 return *this = r.get(); |
| 324 } | 331 } |
| 325 | 332 |
| 333 template <typename U> | |
| 334 scoped_refptr& operator=(scoped_refptr<U>&& r) { | |
| 335 if (ptr_ && ptr_ != r.ptr_) | |
| 336 Release(ptr_); | |
| 337 ptr_ = r.ptr_; | |
| 338 r.ptr_ = nullptr; | |
|
dcheng
2015/04/09 23:50:07
I think this doesn't work for a self-move assignme
Kibeom Kim (inactive)
2015/04/10 02:42:07
Done.
| |
| 339 return *this; | |
| 340 } | |
| 341 | |
| 326 void swap(T** pp) { | 342 void swap(T** pp) { |
| 327 T* p = ptr_; | 343 T* p = ptr_; |
| 328 ptr_ = *pp; | 344 ptr_ = *pp; |
| 329 *pp = p; | 345 *pp = p; |
| 330 } | 346 } |
| 331 | 347 |
| 332 void swap(scoped_refptr<T>& r) { | 348 void swap(scoped_refptr<T>& r) { |
| 333 swap(&r.ptr_); | 349 swap(&r.ptr_); |
| 334 } | 350 } |
| 335 | 351 |
| 336 private: | 352 private: |
| 353 template <typename U> friend class scoped_refptr; | |
| 354 | |
| 337 // Allow scoped_refptr<T> to be used in boolean expressions, but not | 355 // Allow scoped_refptr<T> to be used in boolean expressions, but not |
| 338 // implicitly convertible to a real bool (which is dangerous). | 356 // implicitly convertible to a real bool (which is dangerous). |
| 339 // | 357 // |
| 340 // Note that this trick is only safe when the == and != operators | 358 // Note that this trick is only safe when the == and != operators |
| 341 // are declared explicitly, as otherwise "refptr1 == refptr2" | 359 // are declared explicitly, as otherwise "refptr1 == refptr2" |
| 342 // will compile but do the wrong thing (i.e., convert to Testable | 360 // will compile but do the wrong thing (i.e., convert to Testable |
| 343 // and then do the comparison). | 361 // and then do the comparison). |
| 344 typedef T* scoped_refptr::*Testable; | 362 typedef T* scoped_refptr::*Testable; |
| 345 | 363 |
| 346 public: | 364 public: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 429 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 412 return !operator==(lhs, rhs); | 430 return !operator==(lhs, rhs); |
| 413 } | 431 } |
| 414 | 432 |
| 415 template <typename T> | 433 template <typename T> |
| 416 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 434 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 417 return out << p.get(); | 435 return out << p.get(); |
| 418 } | 436 } |
| 419 | 437 |
| 420 #endif // BASE_MEMORY_REF_COUNTED_H_ | 438 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |