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 |
| 278 // Copy constructor. |
280 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 279 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { |
281 if (ptr_) | 280 if (ptr_) |
282 AddRef(ptr_); | 281 AddRef(ptr_); |
283 } | 282 } |
284 | 283 |
| 284 // Copy conversion constructor. |
285 template <typename U> | 285 template <typename U> |
286 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { | 286 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { |
287 if (ptr_) | 287 if (ptr_) |
288 AddRef(ptr_); | 288 AddRef(ptr_); |
289 } | 289 } |
290 | 290 |
| 291 // Move constructor. This is required in addition to the conversion |
| 292 // constructor below in order for clang to warn about pessimizing moves. |
| 293 scoped_refptr(scoped_refptr&& r) : ptr_(r.get()) { r.ptr_ = nullptr; } |
| 294 |
| 295 // Move conversion constructor. |
291 template <typename U> | 296 template <typename U> |
292 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.get()) { | 297 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.get()) { |
293 r.ptr_ = nullptr; | 298 r.ptr_ = nullptr; |
294 } | 299 } |
295 | 300 |
296 ~scoped_refptr() { | 301 ~scoped_refptr() { |
297 if (ptr_) | 302 if (ptr_) |
298 Release(ptr_); | 303 Release(ptr_); |
299 } | 304 } |
300 | 305 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 436 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
432 return !operator==(lhs, rhs); | 437 return !operator==(lhs, rhs); |
433 } | 438 } |
434 | 439 |
435 template <typename T> | 440 template <typename T> |
436 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 441 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
437 return out << p.get(); | 442 return out << p.get(); |
438 } | 443 } |
439 | 444 |
440 #endif // BASE_MEMORY_REF_COUNTED_H_ | 445 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |