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

Unified Diff: base/memory/ref_counted.h

Issue 510323002: Disable scoped_refptr operator T* on Linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 4 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 | 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 96329cb1f49954adb375d82617019ee067f76c30..5a6a76e9a4d85c79423b6b9b87c281e41d10c6b9 100644
--- a/base/memory/ref_counted.h
+++ b/base/memory/ref_counted.h
@@ -15,6 +15,10 @@
#endif
#include "base/threading/thread_collision_warner.h"
+#if defined(OS_LINUX)
+#define DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR
+#endif
+
namespace base {
namespace subtle {
@@ -291,9 +295,11 @@ class scoped_refptr {
T* get() const { return ptr_; }
+#if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
// Allow scoped_refptr<C> to be used in boolean expression
// and comparison operations.
operator T*() const { return ptr_; }
+#endif
T& operator*() const {
assert(ptr_ != NULL);
@@ -335,6 +341,24 @@ class scoped_refptr {
swap(&r.ptr_);
}
+#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
+ template <typename U>
+ bool operator==(const scoped_refptr<U>& rhs) const {
+ return ptr_ == rhs.get();
+ }
+
+ template <typename U>
+ bool operator!=(const scoped_refptr<U>& rhs) const {
+ return !operator==(rhs);
+ }
+
+ // STL is fun.
jamesr 2014/08/28 18:19:43 comment isn't useful
dcheng 2014/08/28 18:28:39 Done.
+ template <typename U>
+ bool operator<(const scoped_refptr<U>& rhs) const {
+ return ptr_ < rhs.get();
+ }
+#endif
+
protected:
T* ptr_;
};
@@ -346,4 +370,43 @@ scoped_refptr<T> make_scoped_refptr(T* t) {
return scoped_refptr<T>(t);
}
+#if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
+// Temporary operator overloads to facilitate the transition...
+template <typename T, typename U>
+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
+ return lhs.get() == rhs;
+}
+
+template <typename T, typename U>
+bool operator==(const T* lhs, const scoped_refptr<U>& rhs) {
+ return lhs == rhs.get();
+}
+
+template <typename T, typename U>
+bool operator!=(const scoped_refptr<T>& lhs, const U* rhs) {
+ return !operator==(lhs, rhs);
+}
+
+template <typename T, typename U>
+bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) {
+ return !operator==(lhs, rhs);
+}
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
+
+// It turns out the following compiles:
+// scoped_ptr<Foo> a;
+// Foo b;
+// CHECK_EQ(a, b);
+// which is somewhat surprising, since there's no operator<<(std::ostream&, ...)
+// defined. It turns out that this magically works because scoped_ptr is
+// testable--so MakeCheckOpString actually prints the boolean truth value of the
+// scoped_ptr. As a temporary hack, overload operator<< for scoped_refptr to
+// match the fact that scoped_refptr will eventually be testable and this will
+// "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
+template <typename T>
+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.
+ 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
+ return out << !!p.get();
+}
+#endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
+
#endif // BASE_MEMORY_REF_COUNTED_H_
« 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