From 3ae9578f4d757dc1c126737add6a933a9ce14566 Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Thu, 26 Sep 2024 20:46:17 +0200 Subject: [PATCH] Allow scoped_refptr to be used with absl nullability annotation Bug: None Change-Id: I6529e85b69e2430b8e57d7ac5f7842a4a74307b0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/363821 Reviewed-by: Harald Alvestrand Reviewed-by: Mirko Bonadei Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#43094} --- api/BUILD.gn | 2 ++ api/make_ref_counted.h | 58 ++++++------------------------------------ api/scoped_refptr.h | 26 ++++++++++--------- 3 files changed, 24 insertions(+), 62 deletions(-) diff --git a/api/BUILD.gn b/api/BUILD.gn index 607d206842..accfb27885 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn @@ -464,6 +464,7 @@ rtc_source_set("ref_count") { rtc_source_set("scoped_refptr") { visibility = [ "*" ] sources = [ "scoped_refptr.h" ] + deps = [ "//third_party/abseil-cpp/absl/base:nullability" ] } rtc_source_set("make_ref_counted") { @@ -473,6 +474,7 @@ rtc_source_set("make_ref_counted") { ":ref_count", ":scoped_refptr", "../rtc_base:refcount", + "//third_party/abseil-cpp/absl/base:nullability", ] } diff --git a/api/make_ref_counted.h b/api/make_ref_counted.h index 311d207eb4..b5f4e99c85 100644 --- a/api/make_ref_counted.h +++ b/api/make_ref_counted.h @@ -13,6 +13,7 @@ #include #include +#include "absl/base/nullability.h" #include "api/ref_count.h" #include "api/scoped_refptr.h" #include "rtc_base/ref_counted_object.h" @@ -85,7 +86,7 @@ template < typename std::enable_if && std::is_abstract_v, T>::type* = nullptr> -scoped_refptr make_ref_counted(Args&&... args) { +absl::Nonnull> make_ref_counted(Args&&... args) { return scoped_refptr(new RefCountedObject(std::forward(args)...)); } @@ -98,7 +99,7 @@ template < !std::is_convertible_v && webrtc_make_ref_counted_internal::HasAddRefAndRelease::value, T>::type* = nullptr> -scoped_refptr make_ref_counted(Args&&... args) { +absl::Nonnull> make_ref_counted(Args&&... args) { return scoped_refptr(new T(std::forward(args)...)); } @@ -112,61 +113,18 @@ template < !webrtc_make_ref_counted_internal::HasAddRefAndRelease::value, T>::type* = nullptr> -scoped_refptr> make_ref_counted(Args&&... args) { +absl::Nonnull>> make_ref_counted( + Args&&... args) { return scoped_refptr>( new FinalRefCountedObject(std::forward(args)...)); } } // namespace webrtc -// Backwards compatibe aliases. -// TODO: https://issues.webrtc.org/42225969 - deprecate and remove. namespace rtc { -// This doesn't work: -// template -// using make_ref_counted(Args&&... args) = -// webrtc::make_ref_counted(Args&&... args); -// Instead, reproduce the templates. -template && - std::is_abstract_v, - T>::type* = nullptr> -scoped_refptr make_ref_counted(Args&&... args) { - return webrtc::scoped_refptr( - new webrtc::RefCountedObject(std::forward(args)...)); -} - -// `make_ref_counted` for complete classes that are not convertible to -// RefCountInterface and already carry a ref count. -template && - webrtc::webrtc_make_ref_counted_internal::HasAddRefAndRelease< - T>::value, - T>::type* = nullptr> -scoped_refptr make_ref_counted(Args&&... args) { - return webrtc::scoped_refptr(new T(std::forward(args)...)); -} - -// `make_ref_counted` for complete classes that are not convertible to -// RefCountInterface and have no ref count of their own. -template && - !webrtc::webrtc_make_ref_counted_internal:: - HasAddRefAndRelease::value, - - T>::type* = nullptr> -scoped_refptr> make_ref_counted( - Args&&... args) { - return webrtc::scoped_refptr>( - new webrtc::FinalRefCountedObject(std::forward(args)...)); -} - +// Backwards compatibe alias. +// TODO: bugs.webrtc.org/42225969 - deprecate and remove. +using ::webrtc::make_ref_counted; } // namespace rtc #endif // API_MAKE_REF_COUNTED_H_ diff --git a/api/scoped_refptr.h b/api/scoped_refptr.h index 35ad2fea94..c6fb5605ca 100644 --- a/api/scoped_refptr.h +++ b/api/scoped_refptr.h @@ -22,13 +22,13 @@ // }; // // void some_function() { -// scoped_refptr foo = new MyFoo(); +// scoped_refptr foo = make_ref_counted(); // foo->Method(param); // // `foo` is released when this function returns // } // // void some_other_function() { -// scoped_refptr foo = new MyFoo(); +// scoped_refptr foo = make_ref_counted(); // ... // foo = nullptr; // explicitly releases `foo` // ... @@ -41,7 +41,7 @@ // references between the two objects, like so: // // { -// scoped_refptr a = new MyFoo(); +// scoped_refptr a = make_ref_counted(); // scoped_refptr b; // // b.swap(a); @@ -52,7 +52,7 @@ // object, simply use the assignment operator: // // { -// scoped_refptr a = new MyFoo(); +// scoped_refptr a = make_ref_counted(); // scoped_refptr b; // // b = a; @@ -66,17 +66,20 @@ #include #include +#include "absl/base/nullability.h" + namespace webrtc { template -class scoped_refptr { +class ABSL_NULLABILITY_COMPATIBLE scoped_refptr { public: - typedef T element_type; + using absl_nullability_compatible = void; + using element_type = T; scoped_refptr() : ptr_(nullptr) {} scoped_refptr(std::nullptr_t) : ptr_(nullptr) {} // NOLINT(runtime/explicit) - explicit scoped_refptr(T* p) : ptr_(p) { + explicit scoped_refptr(absl::Nullable p) : ptr_(p) { if (ptr_) ptr_->AddRef(); } @@ -119,7 +122,7 @@ class scoped_refptr { return retVal; } - scoped_refptr& operator=(T* p) { + scoped_refptr& operator=(absl::Nullable p) { // AddRef first so that self assignment should work if (p) p->AddRef(); @@ -149,7 +152,7 @@ class scoped_refptr { return *this; } - void swap(T** pp) noexcept { + void swap(absl::Nonnull pp) noexcept { T* p = ptr_; ptr_ = *pp; *pp = p; @@ -219,9 +222,8 @@ bool operator<(const scoped_refptr& a, const scoped_refptr& b) { namespace rtc { // Backwards compatible alias. -// TODO(bugs.webrtc.org/15622): Deprecate and remove. -template -using scoped_refptr = webrtc::scoped_refptr; +// TODO: bugs.webrtc.org/42225969 - Deprecate and remove. +using ::webrtc::scoped_refptr; } // namespace rtc #endif // API_SCOPED_REFPTR_H_