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

Unified Diff: base/memory/ref_counted.h

Issue 11189146: Eliminate implicit conversion from scoped_refptr<T> to T* (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/browser/device_orientation/message_filter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/ref_counted.h
diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h
index b7cb5e064175466d747e5ae66125262d8f1558cf..e1d958d1a7960deecfdcbdcac4bf01d8dda39816 100644
--- a/base/memory/ref_counted.h
+++ b/base/memory/ref_counted.h
@@ -242,9 +242,30 @@ class scoped_refptr {
}
T* get() const { return ptr_; }
- operator T*() const { return ptr_; }
T* operator->() const { return ptr_; }
+ // This operator allows a scoped_refptr to be safely evaluated as bool, using
+ // the "Safe bool" idiom. By "safely", we mean that we are opening up as few
+ // unsafe operations as possible. Simply using operator bool() can allow
+ // silliness like:
+ // int x = my_scoped_refptr;
+ // return (my_scoped_refptr < 5);
+ // See, e.g., http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
+ // for more information on this idiom.
+ typedef void (scoped_refptr<T>::*BoolType)() const;
+ void this_type_does_not_support_comparisons() const {}
+ operator BoolType() const {
+ return ptr_ ? &scoped_refptr<T>::this_type_does_not_support_comparisons :
+ NULL;
+ }
+
+ // Allow scoped_refptr<T> to be used in tree-based containers. Note that
+ // operator< on pointers is technically implementation-defined, but it works
+ // on all our platforms and is a common idiom in Chromium anyway.
+ bool operator<(const scoped_refptr<T>& right) const {
+ return ptr_ < right.ptr_;
+ }
+
// Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
« no previous file with comments | « no previous file | content/browser/device_orientation/message_filter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698