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 template <typename U> | |
334 scoped_refptr<T>& operator=(scoped_refptr<U>&& r) { | |
335 scoped_refptr<T>(r.Pass()).swap(*this); | |
danakj
2015/04/10 19:44:24
This is:
temp.ptr_ = r.ptr_;
r.ptr_ = nullptr;
T*
Kibeom Kim (inactive)
2015/04/12 07:35:14
Yeah you're right. it also needs |if (ptr_) Releas
| |
336 return *this; | |
337 } | |
338 | |
326 void swap(T** pp) { | 339 void swap(T** pp) { |
327 T* p = ptr_; | 340 T* p = ptr_; |
328 ptr_ = *pp; | 341 ptr_ = *pp; |
329 *pp = p; | 342 *pp = p; |
330 } | 343 } |
331 | 344 |
332 void swap(scoped_refptr<T>& r) { | 345 void swap(scoped_refptr<T>& r) { |
333 swap(&r.ptr_); | 346 swap(&r.ptr_); |
334 } | 347 } |
335 | 348 |
336 private: | 349 private: |
350 template <typename U> friend class scoped_refptr; | |
351 | |
337 // Allow scoped_refptr<T> to be used in boolean expressions, but not | 352 // Allow scoped_refptr<T> to be used in boolean expressions, but not |
338 // implicitly convertible to a real bool (which is dangerous). | 353 // implicitly convertible to a real bool (which is dangerous). |
339 // | 354 // |
340 // Note that this trick is only safe when the == and != operators | 355 // Note that this trick is only safe when the == and != operators |
341 // are declared explicitly, as otherwise "refptr1 == refptr2" | 356 // are declared explicitly, as otherwise "refptr1 == refptr2" |
342 // will compile but do the wrong thing (i.e., convert to Testable | 357 // will compile but do the wrong thing (i.e., convert to Testable |
343 // and then do the comparison). | 358 // and then do the comparison). |
344 typedef T* scoped_refptr::*Testable; | 359 typedef T* scoped_refptr::*Testable; |
345 | 360 |
346 public: | 361 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) { | 426 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
412 return !operator==(lhs, rhs); | 427 return !operator==(lhs, rhs); |
413 } | 428 } |
414 | 429 |
415 template <typename T> | 430 template <typename T> |
416 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 431 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
417 return out << p.get(); | 432 return out << p.get(); |
418 } | 433 } |
419 | 434 |
420 #endif // BASE_MEMORY_REF_COUNTED_H_ | 435 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |