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/threading/thread_collision_warner.h" | 17 #include "base/threading/thread_collision_warner.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 | 19 |
20 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_AND ROID) || defined(OS_WIN) | |
21 #define DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR | |
22 #endif | |
23 | |
24 namespace base { | 20 namespace base { |
25 | 21 |
26 namespace subtle { | 22 namespace subtle { |
27 | 23 |
28 class BASE_EXPORT RefCountedBase { | 24 class BASE_EXPORT RefCountedBase { |
29 public: | 25 public: |
30 bool HasOneRef() const { return ref_count_ == 1; } | 26 bool HasOneRef() const { return ref_count_ == 1; } |
31 | 27 |
32 protected: | 28 protected: |
33 RefCountedBase() | 29 RefCountedBase() |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 AddRef(ptr_); | 286 AddRef(ptr_); |
291 } | 287 } |
292 | 288 |
293 ~scoped_refptr() { | 289 ~scoped_refptr() { |
294 if (ptr_) | 290 if (ptr_) |
295 Release(ptr_); | 291 Release(ptr_); |
296 } | 292 } |
297 | 293 |
298 T* get() const { return ptr_; } | 294 T* get() const { return ptr_; } |
299 | 295 |
300 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
301 // Allow scoped_refptr<C> to be used in boolean expression | |
302 // and comparison operations. | |
303 operator T*() const { return ptr_; } | |
304 #endif | |
305 | |
306 T& operator*() const { | 296 T& operator*() const { |
307 assert(ptr_ != NULL); | 297 assert(ptr_ != NULL); |
308 return *ptr_; | 298 return *ptr_; |
309 } | 299 } |
310 | 300 |
311 T* operator->() const { | 301 T* operator->() const { |
312 assert(ptr_ != NULL); | 302 assert(ptr_ != NULL); |
313 return ptr_; | 303 return ptr_; |
314 } | 304 } |
315 | 305 |
(...skipping 20 matching lines...) Expand all Loading... | |
336 void swap(T** pp) { | 326 void swap(T** pp) { |
337 T* p = ptr_; | 327 T* p = ptr_; |
338 ptr_ = *pp; | 328 ptr_ = *pp; |
339 *pp = p; | 329 *pp = p; |
340 } | 330 } |
341 | 331 |
342 void swap(scoped_refptr<T>& r) { | 332 void swap(scoped_refptr<T>& r) { |
343 swap(&r.ptr_); | 333 swap(&r.ptr_); |
344 } | 334 } |
345 | 335 |
346 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
347 template <typename U> | 336 template <typename U> |
348 bool operator==(const scoped_refptr<U>& rhs) const { | 337 bool operator==(const scoped_refptr<U>& rhs) const { |
349 return ptr_ == rhs.get(); | 338 return ptr_ == rhs.get(); |
350 } | 339 } |
351 | 340 |
352 template <typename U> | 341 template <typename U> |
353 bool operator!=(const scoped_refptr<U>& rhs) const { | 342 bool operator!=(const scoped_refptr<U>& rhs) const { |
354 return !operator==(rhs); | 343 return !operator==(rhs); |
355 } | 344 } |
356 | 345 |
357 template <typename U> | 346 template <typename U> |
358 bool operator<(const scoped_refptr<U>& rhs) const { | 347 bool operator<(const scoped_refptr<U>& rhs) const { |
359 return ptr_ < rhs.get(); | 348 return ptr_ < rhs.get(); |
360 } | 349 } |
361 #endif | |
362 | 350 |
363 protected: | 351 protected: |
364 T* ptr_; | 352 T* ptr_; |
365 | 353 |
366 private: | 354 private: |
367 // Non-inline helpers to allow: | 355 // Non-inline helpers to allow: |
368 // class Opaque; | 356 // class Opaque; |
369 // extern template class scoped_refptr<Opaque>; | 357 // extern template class scoped_refptr<Opaque>; |
370 // Otherwise the compiler will complain that Opaque is an incomplete type. | 358 // Otherwise the compiler will complain that Opaque is an incomplete type. |
371 static void AddRef(T* ptr); | 359 static void AddRef(T* ptr); |
(...skipping 10 matching lines...) Expand all Loading... | |
382 ptr->Release(); | 370 ptr->Release(); |
383 } | 371 } |
384 | 372 |
385 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 373 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
386 // having to retype all the template arguments | 374 // having to retype all the template arguments |
387 template <typename T> | 375 template <typename T> |
388 scoped_refptr<T> make_scoped_refptr(T* t) { | 376 scoped_refptr<T> make_scoped_refptr(T* t) { |
389 return scoped_refptr<T>(t); | 377 return scoped_refptr<T>(t); |
390 } | 378 } |
391 | 379 |
392 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
393 // Temporary operator overloads to facilitate the transition... | 380 // Temporary operator overloads to facilitate the transition... |
Nico
2014/11/25 02:59:10
Should this have a link to the bug?
dcheng
2014/11/25 03:00:47
Done.
| |
394 template <typename T, typename U> | 381 template <typename T, typename U> |
395 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { | 382 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { |
396 return lhs.get() == rhs; | 383 return lhs.get() == rhs; |
397 } | 384 } |
398 | 385 |
399 template <typename T, typename U> | 386 template <typename T, typename U> |
400 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { | 387 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { |
401 return lhs == rhs.get(); | 388 return lhs == rhs.get(); |
402 } | 389 } |
403 | 390 |
404 template <typename T, typename U> | 391 template <typename T, typename U> |
405 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { | 392 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { |
406 return !operator==(lhs, rhs); | 393 return !operator==(lhs, rhs); |
407 } | 394 } |
408 | 395 |
409 template <typename T, typename U> | 396 template <typename T, typename U> |
410 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 397 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
411 return !operator==(lhs, rhs); | 398 return !operator==(lhs, rhs); |
412 } | 399 } |
413 | 400 |
414 template <typename T> | 401 template <typename T> |
415 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 402 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
416 return out << p.get(); | 403 return out << p.get(); |
417 } | 404 } |
418 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
419 | 405 |
420 #endif // BASE_MEMORY_REF_COUNTED_H_ | 406 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |