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_) { |
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 scoped_refptr<T>& operator=(scoped_refptr<T>&& r) { |
| 334 scoped_refptr<T>(r.Pass()).swap(*this); |
| 335 return *this; |
| 336 } |
| 337 |
| 338 template <typename U> |
| 339 scoped_refptr<T>& operator=(scoped_refptr<U>&& r) { |
| 340 scoped_refptr<T>(r.Pass()).swap(*this); |
| 341 return *this; |
| 342 } |
| 343 |
326 void swap(T** pp) { | 344 void swap(T** pp) { |
327 T* p = ptr_; | 345 T* p = ptr_; |
328 ptr_ = *pp; | 346 ptr_ = *pp; |
329 *pp = p; | 347 *pp = p; |
330 } | 348 } |
331 | 349 |
332 void swap(scoped_refptr<T>& r) { | 350 void swap(scoped_refptr<T>& r) { |
333 swap(&r.ptr_); | 351 swap(&r.ptr_); |
334 } | 352 } |
335 | 353 |
336 private: | 354 private: |
| 355 template <typename U> friend class scoped_refptr; |
| 356 |
337 // Allow scoped_refptr<T> to be used in boolean expressions, but not | 357 // Allow scoped_refptr<T> to be used in boolean expressions, but not |
338 // implicitly convertible to a real bool (which is dangerous). | 358 // implicitly convertible to a real bool (which is dangerous). |
339 // | 359 // |
340 // Note that this trick is only safe when the == and != operators | 360 // Note that this trick is only safe when the == and != operators |
341 // are declared explicitly, as otherwise "refptr1 == refptr2" | 361 // are declared explicitly, as otherwise "refptr1 == refptr2" |
342 // will compile but do the wrong thing (i.e., convert to Testable | 362 // will compile but do the wrong thing (i.e., convert to Testable |
343 // and then do the comparison). | 363 // and then do the comparison). |
344 typedef T* scoped_refptr::*Testable; | 364 typedef T* scoped_refptr::*Testable; |
345 | 365 |
346 public: | 366 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) { | 431 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
412 return !operator==(lhs, rhs); | 432 return !operator==(lhs, rhs); |
413 } | 433 } |
414 | 434 |
415 template <typename T> | 435 template <typename T> |
416 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 436 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
417 return out << p.get(); | 437 return out << p.get(); |
418 } | 438 } |
419 | 439 |
420 #endif // BASE_MEMORY_REF_COUNTED_H_ | 440 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |