From 9432768024b0397f2dccfec0cab30f33dde87b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Thu, 28 Apr 2022 13:20:29 +0200 Subject: [PATCH] Prepare for deletion of implicit conversion from rtc::scoped_refptr to T* Bug: webrtc:13464 Change-Id: I4c7095d3a1c7c1a9ab609f5f1595545f6cad18db Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249087 Reviewed-by: Tomas Gunnarsson Reviewed-by: Harald Alvestrand Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/main@{#36693} --- api/scoped_refptr.h | 38 ++++++++++++++++++++++++ pc/peer_connection_histogram_unittest.cc | 2 +- rtc_base/BUILD.gn | 1 + rtc_base/checks.h | 7 +++++ rtc_base/gunit.h | 4 +-- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/api/scoped_refptr.h b/api/scoped_refptr.h index a96796074c..b1c90fcbbe 100644 --- a/api/scoped_refptr.h +++ b/api/scoped_refptr.h @@ -105,6 +105,7 @@ class scoped_refptr { T* get() const { return ptr_; } explicit operator bool() const { return ptr_ != nullptr; } + // TODO(bugs.webrtc.org/13464): Delete this conversion operator. operator T*() const { return ptr_; } T& operator*() const { return *ptr_; } T* operator->() const { return ptr_; } @@ -162,6 +163,43 @@ class scoped_refptr { T* ptr_; }; +template +bool operator==(const rtc::scoped_refptr& a, + const rtc::scoped_refptr& b) { + return a.get() == b.get(); +} +template +bool operator!=(const rtc::scoped_refptr& a, + const rtc::scoped_refptr& b) { + return !(a == b); +} + +template +bool operator==(const rtc::scoped_refptr& a, std::nullptr_t) { + return a.get() == nullptr; +} + +template +bool operator!=(const rtc::scoped_refptr& a, std::nullptr_t) { + return !(a == nullptr); +} + +template +bool operator==(std::nullptr_t, const rtc::scoped_refptr& a) { + return a.get() == nullptr; +} + +template +bool operator!=(std::nullptr_t, const rtc::scoped_refptr& a) { + return !(a == nullptr); +} + +// Ordered comparison, needed for use as a std::map key. +template +bool operator<(const rtc::scoped_refptr& a, const rtc::scoped_refptr& b) { + return a.get() < b.get(); +} + } // namespace rtc #endif // API_SCOPED_REFPTR_H_ diff --git a/pc/peer_connection_histogram_unittest.cc b/pc/peer_connection_histogram_unittest.cc index 84525b3634..4a3c6794c4 100644 --- a/pc/peer_connection_histogram_unittest.cc +++ b/pc/peer_connection_histogram_unittest.cc @@ -107,7 +107,7 @@ class ObserverForUsageHistogramTest : public MockPeerConnectionObserver { candidate_target_ = other; } - bool HaveDataChannel() { return last_datachannel_; } + bool HaveDataChannel() { return last_datachannel_ != nullptr; } absl::optional interesting_usage_detected() { return interesting_usage_detected_; diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 9bc8cf1c3a..07137aae91 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -587,6 +587,7 @@ rtc_library("checks") { ] deps = [ ":safe_compare", + "../api:scoped_refptr", "system:inline", "system:rtc_export", ] diff --git a/rtc_base/checks.h b/rtc_base/checks.h index 863e39d651..6ffa30da59 100644 --- a/rtc_base/checks.h +++ b/rtc_base/checks.h @@ -56,6 +56,7 @@ RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg); #include "absl/meta/type_traits.h" #include "absl/strings/string_view.h" +#include "api/scoped_refptr.h" #include "rtc_base/numerics/safe_compare.h" #include "rtc_base/system/inline.h" #include "rtc_base/system/rtc_export.h" @@ -192,6 +193,12 @@ inline Val MakeVal(const void* x) { return {x}; } +template +inline Val MakeVal( + const rtc::scoped_refptr& p) { + return {p.get()}; +} + // The enum class types are not implicitly convertible to arithmetic types. template ::value && diff --git a/rtc_base/gunit.h b/rtc_base/gunit.h index ac99c7ac4d..6bc1419729 100644 --- a/rtc_base/gunit.h +++ b/rtc_base/gunit.h @@ -31,11 +31,11 @@ #define WAIT_(ex, timeout, res) \ do { \ int64_t start = rtc::SystemTimeMillis(); \ - res = (ex); \ + res = (ex) && true; \ while (!res && rtc::SystemTimeMillis() < start + (timeout)) { \ rtc::Thread::Current()->ProcessMessages(0); \ rtc::Thread::Current()->SleepMs(1); \ - res = (ex); \ + res = (ex) && true; \ } \ } while (0)