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