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

Unified Diff: base/memory/weak_ptr.h

Issue 15721007: Make expressions like "if (weak_ptr)" work (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comment again Created 7 years, 6 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 | « base/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/weak_ptr.h
diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h
index 9839db298c7af4c8baa565827f6b40d9920960b9..28ae2a3f1732fde6de78b2db53d5a5806d491f1e 100644
--- a/base/memory/weak_ptr.h
+++ b/base/memory/weak_ptr.h
@@ -221,12 +221,30 @@ class WeakPtr : public internal::WeakPtrBase {
return get();
}
+ // Allow WeakPtr<element_type> 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 "weak_ptr1 == weak_ptr2"
+ // will compile but do the wrong thing (i.e., convert to Testable
+ // and then do the comparison).
+ private:
+ typedef T* WeakPtr::*Testable;
+
+ public:
+ operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; }
+
void reset() {
ref_ = internal::WeakReference();
ptr_ = NULL;
}
private:
+ // Explicitly declare comparison operators as required by the bool
+ // trick, but keep them private.
+ template <class U> bool operator==(WeakPtr<U> const&) const;
+ template <class U> bool operator!=(WeakPtr<U> const&) const;
+
friend class internal::SupportsWeakPtrBase;
template <typename U> friend class WeakPtr;
friend class SupportsWeakPtr<T>;
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698