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