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

Unified Diff: base/memory/ref_counted.h

Issue 758803002: Enable boolean testing of scoped_refptr<T>. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cleanup-defs
Patch Set: Created 6 years, 1 month 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 | base/memory/ref_counted_unittest.cc » ('j') | base/memory/ref_counted_unittest.cc » ('J')
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 0fb7447b999e744e39ff24336005bcbd593172eb..490ef3119b660841ab7a2813bb3f5247d4aa4359 100644
--- a/base/memory/ref_counted.h
+++ b/base/memory/ref_counted.h
@@ -333,6 +333,18 @@ class scoped_refptr {
swap(&r.ptr_);
}
+ private:
+ // Allow scoped_refptr<T> to be used in boolean expressions, but not
+ // implicitly convertible to a real bool (which is dangerous).
+ //
+ // Note that this trick is only safe when the == and != operators
+ // are declared explicitly, as otherwise "refptr1 == refptr2"
+ // will compile but do the wrong thing (i.e., convert to Testable
+ // and then do the comparison).
+ typedef T* scoped_refptr::*Testable;
Lei Zhang 2014/11/26 00:44:18 nit: blank line after.
dcheng 2014/11/26 00:45:28 Done.
+ public:
+ operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; }
+
template <typename U>
bool operator==(const scoped_refptr<U>& rhs) const {
return ptr_ == rhs.get();
« no previous file with comments | « no previous file | base/memory/ref_counted_unittest.cc » ('j') | base/memory/ref_counted_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698