Move rtc::make_ref_counted to api/
Bug: webrtc:12701
Change-Id: If49095b101c1a1763c2a44a0284c0d670cce953f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/265390
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37219}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 33cd57f..fb8187d 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -624,6 +624,7 @@
":checks",
":macromagic",
":refcount",
+ "../api:make_ref_counted",
"../api:refcountedbase",
"../api:scoped_refptr",
"../api:sequence_checker",
@@ -1082,6 +1083,7 @@
"../api:array_view",
"../api:field_trials_view",
"../api:function_view",
+ "../api:make_ref_counted",
"../api:refcountedbase",
"../api:scoped_refptr",
"../api:sequence_checker",
@@ -1589,6 +1591,7 @@
":timeutils",
":zero_memory",
"../api:array_view",
+ "../api:make_ref_counted",
"../api:scoped_refptr",
"../api/numerics",
"../api/units:time_delta",
@@ -1717,7 +1720,6 @@
":macromagic",
":net_helpers",
":null_socket_server",
- ":refcount",
":rtc_base",
":rtc_base_tests_utils",
":rtc_event",
@@ -1731,6 +1733,7 @@
":threading",
":timeutils",
"../api:array_view",
+ "../api:make_ref_counted",
"../api/task_queue",
"../api/task_queue:task_queue_test",
"../test:field_trial",
diff --git a/rtc_base/async_invoker.h b/rtc_base/async_invoker.h
index f1b0311..621375c 100644
--- a/rtc_base/async_invoker.h
+++ b/rtc_base/async_invoker.h
@@ -19,7 +19,6 @@
#include "api/scoped_refptr.h"
#include "rtc_base/async_invoker_inl.h"
#include "rtc_base/event.h"
-#include "rtc_base/ref_counted_object.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "rtc_base/thread.h"
diff --git a/rtc_base/async_invoker_inl.h b/rtc_base/async_invoker_inl.h
index 0ab0efb..79336eb 100644
--- a/rtc_base/async_invoker_inl.h
+++ b/rtc_base/async_invoker_inl.h
@@ -11,10 +11,10 @@
#ifndef RTC_BASE_ASYNC_INVOKER_INL_H_
#define RTC_BASE_ASYNC_INVOKER_INL_H_
+#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "rtc_base/event.h"
#include "rtc_base/message_handler.h"
-#include "rtc_base/ref_counted_object.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_annotations.h"
diff --git a/rtc_base/async_resolver.h b/rtc_base/async_resolver.h
index 0c053ee..8257f46 100644
--- a/rtc_base/async_resolver.h
+++ b/rtc_base/async_resolver.h
@@ -23,7 +23,6 @@
#include "rtc_base/async_resolver_interface.h"
#include "rtc_base/event.h"
#include "rtc_base/ip_address.h"
-#include "rtc_base/ref_counted_object.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/system/no_unique_address.h"
#include "rtc_base/system/rtc_export.h"
diff --git a/rtc_base/operations_chain.cc b/rtc_base/operations_chain.cc
index f42482b..4398bb1 100644
--- a/rtc_base/operations_chain.cc
+++ b/rtc_base/operations_chain.cc
@@ -10,6 +10,7 @@
#include "rtc_base/operations_chain.h"
+#include "api/make_ref_counted.h"
#include "rtc_base/checks.h"
namespace rtc {
diff --git a/rtc_base/ref_counted_object.h b/rtc_base/ref_counted_object.h
index d94e670..418c3d8 100644
--- a/rtc_base/ref_counted_object.h
+++ b/rtc_base/ref_counted_object.h
@@ -10,32 +10,12 @@
#ifndef RTC_BASE_REF_COUNTED_OBJECT_H_
#define RTC_BASE_REF_COUNTED_OBJECT_H_
-#include <type_traits>
-#include <utility>
-
#include "api/scoped_refptr.h"
#include "rtc_base/ref_count.h"
#include "rtc_base/ref_counter.h"
namespace rtc {
-namespace webrtc_make_ref_counted_internal {
-// Determines if the given class has AddRef and Release methods.
-template <typename T>
-class HasAddRefAndRelease {
- private:
- template <typename C,
- decltype(std::declval<C>().AddRef())* = nullptr,
- decltype(std::declval<C>().Release())* = nullptr>
- static int Test(int);
- template <typename>
- static char Test(...);
-
- public:
- static constexpr bool value = std::is_same_v<decltype(Test<T>(0)), int>;
-};
-} // namespace webrtc_make_ref_counted_internal
-
template <class T>
class RefCountedObject : public T {
public:
@@ -104,87 +84,6 @@
mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
};
-// General utilities for constructing a reference counted class and the
-// appropriate reference count implementation for that class.
-//
-// These utilities select either the `RefCountedObject` implementation or
-// `FinalRefCountedObject` depending on whether the to-be-shared class is
-// derived from the RefCountInterface interface or not (respectively).
-
-// `make_ref_counted`:
-//
-// Use this when you want to construct a reference counted object of type T and
-// get a `scoped_refptr<>` back. Example:
-//
-// auto p = make_ref_counted<Foo>("bar", 123);
-//
-// For a class that inherits from RefCountInterface, this is equivalent to:
-//
-// auto p = scoped_refptr<Foo>(new RefCountedObject<Foo>("bar", 123));
-//
-// If the class does not inherit from RefCountInterface, but does have
-// AddRef/Release methods (so a T* is convertible to rtc::scoped_refptr), this
-// is equivalent to just
-//
-// auto p = scoped_refptr<Foo>(new Foo("bar", 123));
-//
-// Otherwise, the example is equivalent to:
-//
-// auto p = scoped_refptr<FinalRefCountedObject<Foo>>(
-// new FinalRefCountedObject<Foo>("bar", 123));
-//
-// In these cases, `make_ref_counted` reduces the amount of boilerplate code but
-// also helps with the most commonly intended usage of RefCountedObject whereby
-// methods for reference counting, are virtual and designed to satisfy the need
-// of an interface. When such a need does not exist, it is more efficient to use
-// the `FinalRefCountedObject` template, which does not add the vtable overhead.
-//
-// Note that in some cases, using RefCountedObject directly may still be what's
-// needed.
-
-// `make_ref_counted` for abstract classes that are convertible to
-// RefCountInterface. The is_abstract requirement rejects classes that inherit
-// both RefCountInterface and RefCounted object, which is a a discouraged
-// pattern, and would result in double inheritance of RefCountedObject if this
-// template was applied.
-template <
- typename T,
- typename... Args,
- typename std::enable_if<std::is_convertible_v<T*, RefCountInterface*> &&
- std::is_abstract_v<T>,
- T>::type* = nullptr>
-scoped_refptr<T> make_ref_counted(Args&&... args) {
- return scoped_refptr<T>(new RefCountedObject<T>(std::forward<Args>(args)...));
-}
-
-// `make_ref_counted` for complete classes that are not convertible to
-// RefCountInterface and already carry a ref count.
-template <
- typename T,
- typename... Args,
- typename std::enable_if<
- !std::is_convertible_v<T*, RefCountInterface*> &&
- webrtc_make_ref_counted_internal::HasAddRefAndRelease<T>::value,
- T>::type* = nullptr>
-scoped_refptr<T> make_ref_counted(Args&&... args) {
- return scoped_refptr<T>(new T(std::forward<Args>(args)...));
-}
-
-// `make_ref_counted` for complete classes that are not convertible to
-// RefCountInterface and have no ref count of their own.
-template <
- typename T,
- typename... Args,
- typename std::enable_if<
- !std::is_convertible_v<T*, RefCountInterface*> &&
- !webrtc_make_ref_counted_internal::HasAddRefAndRelease<T>::value,
-
- T>::type* = nullptr>
-scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) {
- return scoped_refptr<FinalRefCountedObject<T>>(
- new FinalRefCountedObject<T>(std::forward<Args>(args)...));
-}
-
} // namespace rtc
#endif // RTC_BASE_REF_COUNTED_OBJECT_H_
diff --git a/rtc_base/ref_counted_object_unittest.cc b/rtc_base/ref_counted_object_unittest.cc
index c06a61e..abeb1e9 100644
--- a/rtc_base/ref_counted_object_unittest.cc
+++ b/rtc_base/ref_counted_object_unittest.cc
@@ -16,6 +16,7 @@
#include <utility>
#include "absl/strings/string_view.h"
+#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "rtc_base/ref_count.h"
#include "test/gtest.h"
diff --git a/rtc_base/rtc_certificate.cc b/rtc_base/rtc_certificate.cc
index e9137f4..e0b6b32 100644
--- a/rtc_base/rtc_certificate.cc
+++ b/rtc_base/rtc_certificate.cc
@@ -13,7 +13,6 @@
#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"
diff --git a/rtc_base/rtc_certificate_generator.cc b/rtc_base/rtc_certificate_generator.cc
index 09cd279..d2856f7 100644
--- a/rtc_base/rtc_certificate_generator.cc
+++ b/rtc_base/rtc_certificate_generator.cc
@@ -19,7 +19,6 @@
#include "rtc_base/checks.h"
#include "rtc_base/location.h"
#include "rtc_base/message_handler.h"
-#include "rtc_base/ref_counted_object.h"
#include "rtc_base/ssl_identity.h"
namespace rtc {
diff --git a/rtc_base/rtc_certificate_generator_unittest.cc b/rtc_base/rtc_certificate_generator_unittest.cc
index 53ae973..3d9df58 100644
--- a/rtc_base/rtc_certificate_generator_unittest.cc
+++ b/rtc_base/rtc_certificate_generator_unittest.cc
@@ -13,9 +13,9 @@
#include <memory>
#include "absl/types/optional.h"
+#include "api/make_ref_counted.h"
#include "rtc_base/checks.h"
#include "rtc_base/gunit.h"
-#include "rtc_base/ref_counted_object.h"
#include "rtc_base/thread.h"
#include "test/gtest.h"