1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2022 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker #ifndef API_MAKE_REF_COUNTED_H_
11*d9f75844SAndroid Build Coastguard Worker #define API_MAKE_REF_COUNTED_H_
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <utility>
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_counted_object.h"
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker namespace rtc {
18*d9f75844SAndroid Build Coastguard Worker
19*d9f75844SAndroid Build Coastguard Worker namespace webrtc_make_ref_counted_internal {
20*d9f75844SAndroid Build Coastguard Worker // Determines if the given class has AddRef and Release methods.
21*d9f75844SAndroid Build Coastguard Worker template <typename T>
22*d9f75844SAndroid Build Coastguard Worker class HasAddRefAndRelease {
23*d9f75844SAndroid Build Coastguard Worker private:
24*d9f75844SAndroid Build Coastguard Worker template <typename C,
25*d9f75844SAndroid Build Coastguard Worker decltype(std::declval<C>().AddRef())* = nullptr,
26*d9f75844SAndroid Build Coastguard Worker decltype(std::declval<C>().Release())* = nullptr>
27*d9f75844SAndroid Build Coastguard Worker static int Test(int);
28*d9f75844SAndroid Build Coastguard Worker template <typename>
29*d9f75844SAndroid Build Coastguard Worker static char Test(...);
30*d9f75844SAndroid Build Coastguard Worker
31*d9f75844SAndroid Build Coastguard Worker public:
32*d9f75844SAndroid Build Coastguard Worker static constexpr bool value = std::is_same_v<decltype(Test<T>(0)), int>;
33*d9f75844SAndroid Build Coastguard Worker };
34*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc_make_ref_counted_internal
35*d9f75844SAndroid Build Coastguard Worker
36*d9f75844SAndroid Build Coastguard Worker // General utilities for constructing a reference counted class and the
37*d9f75844SAndroid Build Coastguard Worker // appropriate reference count implementation for that class.
38*d9f75844SAndroid Build Coastguard Worker //
39*d9f75844SAndroid Build Coastguard Worker // These utilities select either the `RefCountedObject` implementation or
40*d9f75844SAndroid Build Coastguard Worker // `FinalRefCountedObject` depending on whether the to-be-shared class is
41*d9f75844SAndroid Build Coastguard Worker // derived from the RefCountInterface interface or not (respectively).
42*d9f75844SAndroid Build Coastguard Worker
43*d9f75844SAndroid Build Coastguard Worker // `make_ref_counted`:
44*d9f75844SAndroid Build Coastguard Worker //
45*d9f75844SAndroid Build Coastguard Worker // Use this when you want to construct a reference counted object of type T and
46*d9f75844SAndroid Build Coastguard Worker // get a `scoped_refptr<>` back. Example:
47*d9f75844SAndroid Build Coastguard Worker //
48*d9f75844SAndroid Build Coastguard Worker // auto p = make_ref_counted<Foo>("bar", 123);
49*d9f75844SAndroid Build Coastguard Worker //
50*d9f75844SAndroid Build Coastguard Worker // For a class that inherits from RefCountInterface, this is equivalent to:
51*d9f75844SAndroid Build Coastguard Worker //
52*d9f75844SAndroid Build Coastguard Worker // auto p = scoped_refptr<Foo>(new RefCountedObject<Foo>("bar", 123));
53*d9f75844SAndroid Build Coastguard Worker //
54*d9f75844SAndroid Build Coastguard Worker // If the class does not inherit from RefCountInterface, but does have
55*d9f75844SAndroid Build Coastguard Worker // AddRef/Release methods (so a T* is convertible to rtc::scoped_refptr), this
56*d9f75844SAndroid Build Coastguard Worker // is equivalent to just
57*d9f75844SAndroid Build Coastguard Worker //
58*d9f75844SAndroid Build Coastguard Worker // auto p = scoped_refptr<Foo>(new Foo("bar", 123));
59*d9f75844SAndroid Build Coastguard Worker //
60*d9f75844SAndroid Build Coastguard Worker // Otherwise, the example is equivalent to:
61*d9f75844SAndroid Build Coastguard Worker //
62*d9f75844SAndroid Build Coastguard Worker // auto p = scoped_refptr<FinalRefCountedObject<Foo>>(
63*d9f75844SAndroid Build Coastguard Worker // new FinalRefCountedObject<Foo>("bar", 123));
64*d9f75844SAndroid Build Coastguard Worker //
65*d9f75844SAndroid Build Coastguard Worker // In these cases, `make_ref_counted` reduces the amount of boilerplate code but
66*d9f75844SAndroid Build Coastguard Worker // also helps with the most commonly intended usage of RefCountedObject whereby
67*d9f75844SAndroid Build Coastguard Worker // methods for reference counting, are virtual and designed to satisfy the need
68*d9f75844SAndroid Build Coastguard Worker // of an interface. When such a need does not exist, it is more efficient to use
69*d9f75844SAndroid Build Coastguard Worker // the `FinalRefCountedObject` template, which does not add the vtable overhead.
70*d9f75844SAndroid Build Coastguard Worker //
71*d9f75844SAndroid Build Coastguard Worker // Note that in some cases, using RefCountedObject directly may still be what's
72*d9f75844SAndroid Build Coastguard Worker // needed.
73*d9f75844SAndroid Build Coastguard Worker
74*d9f75844SAndroid Build Coastguard Worker // `make_ref_counted` for abstract classes that are convertible to
75*d9f75844SAndroid Build Coastguard Worker // RefCountInterface. The is_abstract requirement rejects classes that inherit
76*d9f75844SAndroid Build Coastguard Worker // both RefCountInterface and RefCounted object, which is a a discouraged
77*d9f75844SAndroid Build Coastguard Worker // pattern, and would result in double inheritance of RefCountedObject if this
78*d9f75844SAndroid Build Coastguard Worker // template was applied.
79*d9f75844SAndroid Build Coastguard Worker template <
80*d9f75844SAndroid Build Coastguard Worker typename T,
81*d9f75844SAndroid Build Coastguard Worker typename... Args,
82*d9f75844SAndroid Build Coastguard Worker typename std::enable_if<std::is_convertible_v<T*, RefCountInterface*> &&
83*d9f75844SAndroid Build Coastguard Worker std::is_abstract_v<T>,
84*d9f75844SAndroid Build Coastguard Worker T>::type* = nullptr>
make_ref_counted(Args &&...args)85*d9f75844SAndroid Build Coastguard Worker scoped_refptr<T> make_ref_counted(Args&&... args) {
86*d9f75844SAndroid Build Coastguard Worker return scoped_refptr<T>(new RefCountedObject<T>(std::forward<Args>(args)...));
87*d9f75844SAndroid Build Coastguard Worker }
88*d9f75844SAndroid Build Coastguard Worker
89*d9f75844SAndroid Build Coastguard Worker // `make_ref_counted` for complete classes that are not convertible to
90*d9f75844SAndroid Build Coastguard Worker // RefCountInterface and already carry a ref count.
91*d9f75844SAndroid Build Coastguard Worker template <
92*d9f75844SAndroid Build Coastguard Worker typename T,
93*d9f75844SAndroid Build Coastguard Worker typename... Args,
94*d9f75844SAndroid Build Coastguard Worker typename std::enable_if<
95*d9f75844SAndroid Build Coastguard Worker !std::is_convertible_v<T*, RefCountInterface*> &&
96*d9f75844SAndroid Build Coastguard Worker webrtc_make_ref_counted_internal::HasAddRefAndRelease<T>::value,
97*d9f75844SAndroid Build Coastguard Worker T>::type* = nullptr>
make_ref_counted(Args &&...args)98*d9f75844SAndroid Build Coastguard Worker scoped_refptr<T> make_ref_counted(Args&&... args) {
99*d9f75844SAndroid Build Coastguard Worker return scoped_refptr<T>(new T(std::forward<Args>(args)...));
100*d9f75844SAndroid Build Coastguard Worker }
101*d9f75844SAndroid Build Coastguard Worker
102*d9f75844SAndroid Build Coastguard Worker // `make_ref_counted` for complete classes that are not convertible to
103*d9f75844SAndroid Build Coastguard Worker // RefCountInterface and have no ref count of their own.
104*d9f75844SAndroid Build Coastguard Worker template <
105*d9f75844SAndroid Build Coastguard Worker typename T,
106*d9f75844SAndroid Build Coastguard Worker typename... Args,
107*d9f75844SAndroid Build Coastguard Worker typename std::enable_if<
108*d9f75844SAndroid Build Coastguard Worker !std::is_convertible_v<T*, RefCountInterface*> &&
109*d9f75844SAndroid Build Coastguard Worker !webrtc_make_ref_counted_internal::HasAddRefAndRelease<T>::value,
110*d9f75844SAndroid Build Coastguard Worker
111*d9f75844SAndroid Build Coastguard Worker T>::type* = nullptr>
make_ref_counted(Args &&...args)112*d9f75844SAndroid Build Coastguard Worker scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) {
113*d9f75844SAndroid Build Coastguard Worker return scoped_refptr<FinalRefCountedObject<T>>(
114*d9f75844SAndroid Build Coastguard Worker new FinalRefCountedObject<T>(std::forward<Args>(args)...));
115*d9f75844SAndroid Build Coastguard Worker }
116*d9f75844SAndroid Build Coastguard Worker
117*d9f75844SAndroid Build Coastguard Worker } // namespace rtc
118*d9f75844SAndroid Build Coastguard Worker
119*d9f75844SAndroid Build Coastguard Worker #endif // API_MAKE_REF_COUNTED_H_
120