Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: base/memory/ref_counted.h

Issue 1958823002: Fix implicit access to raw pointer of scoped_refptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <type_traits> 10 #include <type_traits>
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 void swap(T** pp) { 355 void swap(T** pp) {
356 T* p = ptr_; 356 T* p = ptr_;
357 ptr_ = *pp; 357 ptr_ = *pp;
358 *pp = p; 358 *pp = p;
359 } 359 }
360 360
361 void swap(scoped_refptr<T>& r) { 361 void swap(scoped_refptr<T>& r) {
362 swap(&r.ptr_); 362 swap(&r.ptr_);
363 } 363 }
364 364
365 explicit operator bool() const { return ptr_ != nullptr; }
366
365 private: 367 private:
danakj 2016/05/10 21:37:46 There's no reason to make a private block for this
danakj 2016/05/10 21:38:51 (I'm also wondering why it's here, we're calling .
scheib 2016/05/10 22:25:00 Done, moved friend to bottom private section and c
366 template <typename U> friend class scoped_refptr; 368 template <typename U> friend class scoped_refptr;
367 369
368 // Implement "Safe Bool Idiom"
369 // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
370 //
371 // Allow scoped_refptr<T> to be used in boolean expressions such as
372 // if (ref_ptr_instance)
373 // But do not become convertible to a real bool (which is dangerous).
374 // Implementation requires:
375 // typedef Testable
376 // operator Testable() const
377 // operator==
378 // operator!=
379 //
380 // == and != operators must be declared explicitly or dissallowed, as
381 // otherwise "ptr1 == ptr2" will compile but do the wrong thing (i.e., convert
382 // to Testable and then do the comparison).
383 //
384 // C++11 provides for "explicit operator bool()", however it is currently
385 // banned due to MSVS2013. https://chromium-cpp.appspot.com/#core-blacklist
386 typedef T* scoped_refptr::*Testable;
387 public: 370 public:
388 operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; }
389
390 template <typename U> 371 template <typename U>
391 bool operator==(const scoped_refptr<U>& rhs) const { 372 bool operator==(const scoped_refptr<U>& rhs) const {
392 return ptr_ == rhs.get(); 373 return ptr_ == rhs.get();
393 } 374 }
394 375
395 template <typename U> 376 template <typename U>
396 bool operator!=(const scoped_refptr<U>& rhs) const { 377 bool operator!=(const scoped_refptr<U>& rhs) const {
397 return !operator==(rhs); 378 return !operator==(rhs);
398 } 379 }
399 380
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { 431 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) {
451 return !operator==(lhs, rhs); 432 return !operator==(lhs, rhs);
452 } 433 }
453 434
454 template <typename T> 435 template <typename T>
455 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { 436 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) {
456 return out << p.get(); 437 return out << p.get();
457 } 438 }
458 439
459 #endif // BASE_MEMORY_REF_COUNTED_H_ 440 #endif // BASE_MEMORY_REF_COUNTED_H_
OLDNEW
« no previous file with comments | « no previous file | base/threading/thread_unittest.cc » ('j') | chrome/browser/themes/theme_service.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698