Update rtc_base to not use implicit T* --> scoped_refptr<T> conversion

Also updated the OperationsChain and CallbackHandle classes to not use
any virtual methods.

Bug: webrtc:13464
Change-Id: I3437d1b7b043339e66411f5a46c226624b7ff9a5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/246102
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35682}
This commit is contained in:
Niels Möller 2022-01-13 11:00:05 +01:00 committed by WebRTC LUCI CQ
parent 70fe704588
commit 9512910e6e
7 changed files with 32 additions and 15 deletions

View File

@ -499,6 +499,7 @@ rtc_source_set("rtc_operations_chain") {
":checks",
":macromagic",
":refcount",
"../api:refcountedbase",
"../api:scoped_refptr",
"../api:sequence_checker",
"system:no_unique_address",

View File

@ -37,10 +37,11 @@ void OperationsChain::CallbackHandle::OnOperationComplete() {
// static
scoped_refptr<OperationsChain> OperationsChain::Create() {
return new OperationsChain();
// Explicit new, to access private constructor.
return rtc::scoped_refptr<OperationsChain>(new OperationsChain());
}
OperationsChain::OperationsChain() : RefCountedObject() {
OperationsChain::OperationsChain() {
RTC_DCHECK_RUN_ON(&sequence_checker_);
}
@ -63,8 +64,10 @@ bool OperationsChain::IsEmpty() const {
}
std::function<void()> OperationsChain::CreateOperationsChainCallback() {
return [handle = rtc::scoped_refptr<CallbackHandle>(
new CallbackHandle(this))]() { handle->OnOperationComplete(); };
return [handle = rtc::make_ref_counted<CallbackHandle>(
rtc::scoped_refptr<OperationsChain>(this))]() {
handle->OnOperationComplete();
};
}
void OperationsChain::OnOperationComplete() {

View File

@ -19,6 +19,7 @@
#include <utility>
#include "absl/types/optional.h"
#include "api/ref_counted_base.h"
#include "api/scoped_refptr.h"
#include "api/sequence_checker.h"
#include "rtc_base/checks.h"
@ -113,7 +114,7 @@ class OperationWithFunctor final : public Operation {
// The OperationsChain is kept-alive through reference counting if there are
// operations pending. This, together with the contract, guarantees that all
// operations that are chained get executed.
class OperationsChain final : public RefCountedObject<RefCountInterface> {
class OperationsChain final : public RefCountedNonVirtual<OperationsChain> {
public:
static scoped_refptr<OperationsChain> Create();
~OperationsChain();
@ -163,7 +164,7 @@ class OperationsChain final : public RefCountedObject<RefCountInterface> {
// std::function<void()>, which is a copyable type. To allow the callback to
// be copyable, it is backed up by this reference counted handle. See
// CreateOperationsChainCallback().
class CallbackHandle final : public RefCountedObject<RefCountInterface> {
class CallbackHandle final : public RefCountedNonVirtual<CallbackHandle> {
public:
explicit CallbackHandle(scoped_refptr<OperationsChain> operations_chain);
~CallbackHandle();

View File

@ -148,7 +148,7 @@ template <typename T,
typename std::enable_if<std::is_convertible_v<T*, RefCountInterface*>,
T>::type* = nullptr>
scoped_refptr<T> make_ref_counted(Args&&... args) {
return new RefCountedObject<T>(std::forward<Args>(args)...);
return scoped_refptr<T>(new RefCountedObject<T>(std::forward<Args>(args)...));
}
// `make_ref_counted` for complete classes that are not convertible to
@ -175,7 +175,8 @@ template <
T>::type* = nullptr>
scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) {
return new FinalRefCountedObject<T>(std::forward<Args>(args)...);
return scoped_refptr<FinalRefCountedObject<T>>(
new FinalRefCountedObject<T>(std::forward<Args>(args)...));
}
// `Ref<>`, `Ref<>::Type` and `Ref<>::Ptr`:

View File

@ -13,6 +13,7 @@
#include <memory>
#include "rtc_base/checks.h"
#include "rtc_base/ref_counted_object.h"
#include "rtc_base/ssl_certificate.h"
#include "rtc_base/ssl_identity.h"
#include "rtc_base/time_utils.h"
@ -21,7 +22,9 @@ namespace rtc {
scoped_refptr<RTCCertificate> RTCCertificate::Create(
std::unique_ptr<SSLIdentity> identity) {
return new RTCCertificate(identity.release());
// Explicit new to access proteced constructor.
return rtc::scoped_refptr<RTCCertificate>(
new RTCCertificate(identity.release()));
}
RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
@ -61,7 +64,7 @@ scoped_refptr<RTCCertificate> RTCCertificate::FromPEM(
SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate()));
if (!identity)
return nullptr;
return new RTCCertificate(identity.release());
return RTCCertificate::Create(std::move(identity));
}
bool RTCCertificate::operator==(const RTCCertificate& certificate) const {

View File

@ -12,23 +12,29 @@
namespace webrtc {
// static
rtc::scoped_refptr<PendingTaskSafetyFlag> PendingTaskSafetyFlag::CreateInternal(
bool alive) {
// Explicit new, to access private constructor.
return rtc::scoped_refptr<PendingTaskSafetyFlag>(
new PendingTaskSafetyFlag(alive));
}
// static
rtc::scoped_refptr<PendingTaskSafetyFlag> PendingTaskSafetyFlag::Create() {
return new PendingTaskSafetyFlag(true);
return CreateInternal(true);
}
rtc::scoped_refptr<PendingTaskSafetyFlag>
PendingTaskSafetyFlag::CreateDetached() {
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag(
new PendingTaskSafetyFlag(true));
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag = CreateInternal(true);
safety_flag->main_sequence_.Detach();
return safety_flag;
}
rtc::scoped_refptr<PendingTaskSafetyFlag>
PendingTaskSafetyFlag::CreateDetachedInactive() {
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag(
new PendingTaskSafetyFlag(false));
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag = CreateInternal(false);
safety_flag->main_sequence_.Detach();
return safety_flag;
}

View File

@ -93,6 +93,8 @@ class PendingTaskSafetyFlag final
explicit PendingTaskSafetyFlag(bool alive) : alive_(alive) {}
private:
static rtc::scoped_refptr<PendingTaskSafetyFlag> CreateInternal(bool alive);
bool alive_ = true;
RTC_NO_UNIQUE_ADDRESS SequenceChecker main_sequence_;
};