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 | 9 |
10 #include "base/atomic_ref_count.h" | 10 #include "base/atomic_ref_count.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #ifndef NDEBUG | 13 #ifndef NDEBUG |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #endif | 15 #endif |
16 #include "base/threading/thread_collision_warner.h" | 16 #include "base/threading/thread_collision_warner.h" |
17 | 17 |
18 #if defined(OS_LINUX) | |
19 #define DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR | |
20 #endif | |
21 | |
18 namespace base { | 22 namespace base { |
19 | 23 |
20 namespace subtle { | 24 namespace subtle { |
21 | 25 |
22 class BASE_EXPORT RefCountedBase { | 26 class BASE_EXPORT RefCountedBase { |
23 public: | 27 public: |
24 bool HasOneRef() const { return ref_count_ == 1; } | 28 bool HasOneRef() const { return ref_count_ == 1; } |
25 | 29 |
26 protected: | 30 protected: |
27 RefCountedBase() | 31 RefCountedBase() |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 ptr_->AddRef(); | 288 ptr_->AddRef(); |
285 } | 289 } |
286 | 290 |
287 ~scoped_refptr() { | 291 ~scoped_refptr() { |
288 if (ptr_) | 292 if (ptr_) |
289 ptr_->Release(); | 293 ptr_->Release(); |
290 } | 294 } |
291 | 295 |
292 T* get() const { return ptr_; } | 296 T* get() const { return ptr_; } |
293 | 297 |
298 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
294 // Allow scoped_refptr<C> to be used in boolean expression | 299 // Allow scoped_refptr<C> to be used in boolean expression |
295 // and comparison operations. | 300 // and comparison operations. |
296 operator T*() const { return ptr_; } | 301 operator T*() const { return ptr_; } |
302 #endif | |
297 | 303 |
298 T& operator*() const { | 304 T& operator*() const { |
299 assert(ptr_ != NULL); | 305 assert(ptr_ != NULL); |
300 return *ptr_; | 306 return *ptr_; |
301 } | 307 } |
302 | 308 |
303 T* operator->() const { | 309 T* operator->() const { |
304 assert(ptr_ != NULL); | 310 assert(ptr_ != NULL); |
305 return ptr_; | 311 return ptr_; |
306 } | 312 } |
(...skipping 21 matching lines...) Expand all Loading... | |
328 void swap(T** pp) { | 334 void swap(T** pp) { |
329 T* p = ptr_; | 335 T* p = ptr_; |
330 ptr_ = *pp; | 336 ptr_ = *pp; |
331 *pp = p; | 337 *pp = p; |
332 } | 338 } |
333 | 339 |
334 void swap(scoped_refptr<T>& r) { | 340 void swap(scoped_refptr<T>& r) { |
335 swap(&r.ptr_); | 341 swap(&r.ptr_); |
336 } | 342 } |
337 | 343 |
344 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
jamesr
2014/08/28 18:19:42
c++11 have these operators defined for shared_ptr<
dcheng
2014/08/28 18:28:39
I need to wrap them in this guard, because otherwi
| |
345 template <typename U> | |
346 bool operator==(const scoped_refptr<U>& rhs) const { | |
347 return ptr_ == rhs.get(); | |
348 } | |
349 | |
350 template <typename U> | |
351 bool operator!=(const scoped_refptr<U>& rhs) const { | |
352 return !operator==(rhs); | |
353 } | |
354 | |
355 // STL is fun. | |
jamesr
2014/08/28 18:19:43
comment isn't useful
dcheng
2014/08/28 18:28:39
Done.
| |
356 template <typename U> | |
357 bool operator<(const scoped_refptr<U>& rhs) const { | |
358 return ptr_ < rhs.get(); | |
359 } | |
360 #endif | |
361 | |
338 protected: | 362 protected: |
339 T* ptr_; | 363 T* ptr_; |
340 }; | 364 }; |
341 | 365 |
342 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without | 366 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without |
343 // having to retype all the template arguments | 367 // having to retype all the template arguments |
344 template <typename T> | 368 template <typename T> |
345 scoped_refptr<T> make_scoped_refptr(T* t) { | 369 scoped_refptr<T> make_scoped_refptr(T* t) { |
346 return scoped_refptr<T>(t); | 370 return scoped_refptr<T>(t); |
347 } | 371 } |
348 | 372 |
373 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
374 // Temporary operator overloads to facilitate the transition... | |
375 template <typename T, typename U> | |
376 bool operator==(const scoped_refptr<T>& lhs, const U* rhs) { | |
jamesr
2014/08/28 18:19:42
i'd really prefer not to add this and instead inse
dcheng
2014/08/28 18:28:40
I'll try to get a more accurate count once there a
| |
377 return lhs.get() == rhs; | |
378 } | |
379 | |
380 template <typename T, typename U> | |
381 bool operator==(const T* lhs, const scoped_refptr<U>& rhs) { | |
382 return lhs == rhs.get(); | |
383 } | |
384 | |
385 template <typename T, typename U> | |
386 bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) { | |
387 return !operator==(lhs, rhs); | |
388 } | |
389 | |
390 template <typename T, typename U> | |
391 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | |
392 return !operator==(lhs, rhs); | |
393 } | |
Ryan Sleevi
2014/08/28 17:32:20
For comparison, shared_ptr (C++11) defines
==
!=
<
dcheng
2014/08/28 17:37:15
It wasn't needed for things to compile. I think on
| |
394 | |
395 // It turns out the following compiles: | |
396 // scoped_ptr<Foo> a; | |
397 // Foo b; | |
398 // CHECK_EQ(a, b); | |
399 // which is somewhat surprising, since there's no operator<<(std::ostream&, ...) | |
400 // defined. It turns out that this magically works because scoped_ptr is | |
401 // testable--so MakeCheckOpString actually prints the boolean truth value of the | |
402 // scoped_ptr. As a temporary hack, overload operator<< for scoped_refptr to | |
403 // match the fact that scoped_refptr will eventually be testable and this will | |
404 // "Just Work" for some very twisted definiton of "Just Working". | |
jamesr
2014/08/28 18:19:42
this comment isn't terribly useful
dcheng
2014/08/28 18:28:39
I've removed the comment (it was to try to documen
| |
405 template <typename T> | |
406 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | |
jamesr
2014/08/28 18:19:42
EXPORT on an inline function makes no sense. there
dcheng
2014/08/28 18:28:39
Done.
| |
407 const scoped_refptr<T>& p) { | |
Ryan Sleevi
2014/08/28 17:32:20
shared_ptr defines in 20.7.2.2.11, so seems reason
dcheng
2014/08/28 17:37:15
In that case, I won't bother matching scoped_ptr's
jamesr
2014/08/28 18:19:43
Can't you just rely on the fallback byte-by-byte p
dcheng
2014/08/28 18:28:40
This is for the logging macros (CHECK/DCHECK) and
| |
408 return out << !!p.get(); | |
409 } | |
410 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) | |
411 | |
349 #endif // BASE_MEMORY_REF_COUNTED_H_ | 412 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |