perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 10 | #ifndef RTC_BASE_REF_COUNTED_OBJECT_H_ |
| 11 | #define RTC_BASE_REF_COUNTED_OBJECT_H_ |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <type_traits> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <utility> |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 15 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/constructor_magic.h" |
| 18 | #include "rtc_base/ref_count.h" |
| 19 | #include "rtc_base/ref_counter.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | template <class T> |
| 24 | class RefCountedObject : public T { |
| 25 | public: |
| 26 | RefCountedObject() {} |
| 27 | |
| 28 | template <class P0> |
| 29 | explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {} |
| 30 | |
| 31 | template <class P0, class P1, class... Args> |
| 32 | RefCountedObject(P0&& p0, P1&& p1, Args&&... args) |
| 33 | : T(std::forward<P0>(p0), |
| 34 | std::forward<P1>(p1), |
| 35 | std::forward<Args>(args)...) {} |
| 36 | |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 37 | void AddRef() const override { ref_count_.IncRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 38 | |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 39 | RefCountReleaseStatus Release() const override { |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 40 | const auto status = ref_count_.DecRef(); |
| 41 | if (status == RefCountReleaseStatus::kDroppedLastRef) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 42 | delete this; |
| 43 | } |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 44 | return status; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Return whether the reference count is one. If the reference count is used |
| 48 | // in the conventional way, a reference count of 1 implies that the current |
| 49 | // thread owns the reference and no other thread shares it. This call |
| 50 | // performs the test for a reference count of one, and performs the memory |
| 51 | // barrier needed for the owning thread to act on the object, knowing that it |
| 52 | // has exclusive access to the object. |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 53 | virtual bool HasOneRef() const { return ref_count_.HasOneRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 54 | |
| 55 | protected: |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 56 | ~RefCountedObject() override {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 57 | |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 58 | mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
| 59 | |
| 60 | RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 61 | }; |
| 62 | |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 63 | template <class T> |
| 64 | class FinalRefCountedObject final : public T { |
| 65 | public: |
| 66 | using T::T; |
Danil Chapovalov | 80b7628 | 2021-04-26 14:32:27 | [diff] [blame] | 67 | // Until c++17 compilers are allowed not to inherit the default constructors. |
| 68 | // Thus the default constructors are forwarded explicitly. |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 69 | FinalRefCountedObject() = default; |
Danil Chapovalov | 80b7628 | 2021-04-26 14:32:27 | [diff] [blame] | 70 | explicit FinalRefCountedObject(const T& other) : T(other) {} |
| 71 | explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {} |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 72 | FinalRefCountedObject(const FinalRefCountedObject&) = delete; |
| 73 | FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete; |
| 74 | |
| 75 | void AddRef() const { ref_count_.IncRef(); } |
| 76 | void Release() const { |
| 77 | if (ref_count_.DecRef() == RefCountReleaseStatus::kDroppedLastRef) { |
| 78 | delete this; |
| 79 | } |
| 80 | } |
| 81 | bool HasOneRef() const { return ref_count_.HasOneRef(); } |
| 82 | |
| 83 | private: |
| 84 | ~FinalRefCountedObject() = default; |
| 85 | |
Danil Chapovalov | d71e591 | 2021-03-29 20:17:36 | [diff] [blame] | 86 | mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 87 | }; |
| 88 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 89 | // General utilities for constructing a reference counted class and the |
| 90 | // appropriate reference count implementation for that class. |
| 91 | // |
| 92 | // These utilities select either the `RefCountedObject` implementation or |
| 93 | // `FinalRefCountedObject` depending on whether the to-be-shared class is |
| 94 | // derived from the RefCountInterface interface or not (respectively). |
| 95 | |
| 96 | // `make_ref_counted`: |
| 97 | // |
| 98 | // Use this when you want to construct a reference counted object of type T and |
| 99 | // get a `scoped_refptr<>` back. Example: |
| 100 | // |
| 101 | // auto p = make_ref_counted<Foo>("bar", 123); |
| 102 | // |
| 103 | // For a class that inherits from RefCountInterface, this is equivalent to: |
| 104 | // |
| 105 | // auto p = scoped_refptr<Foo>(new RefCountedObject<Foo>("bar", 123)); |
| 106 | // |
| 107 | // If the class does not inherit from RefCountInterface, the example is |
| 108 | // equivalent to: |
| 109 | // |
| 110 | // auto p = scoped_refptr<FinalRefCountedObject<Foo>>( |
| 111 | // new FinalRefCountedObject<Foo>("bar", 123)); |
| 112 | // |
| 113 | // In these cases, `make_ref_counted` reduces the amount of boilerplate code but |
| 114 | // also helps with the most commonly intended usage of RefCountedObject whereby |
| 115 | // methods for reference counting, are virtual and designed to satisfy the need |
| 116 | // of an interface. When such a need does not exist, it is more efficient to use |
| 117 | // the `FinalRefCountedObject` template, which does not add the vtable overhead. |
| 118 | // |
| 119 | // Note that in some cases, using RefCountedObject directly may still be what's |
| 120 | // needed. |
| 121 | |
| 122 | // `make_ref_counted` for classes that are convertible to RefCountInterface. |
| 123 | template < |
| 124 | typename T, |
| 125 | typename... Args, |
| 126 | typename std::enable_if<std::is_convertible<T*, RefCountInterface*>::value, |
| 127 | T>::type* = nullptr> |
| 128 | scoped_refptr<T> make_ref_counted(Args&&... args) { |
| 129 | return new RefCountedObject<T>(std::forward<Args>(args)...); |
| 130 | } |
| 131 | |
| 132 | // `make_ref_counted` for complete classes that are not convertible to |
| 133 | // RefCountInterface. |
| 134 | template < |
| 135 | typename T, |
| 136 | typename... Args, |
| 137 | typename std::enable_if<!std::is_convertible<T*, RefCountInterface*>::value, |
| 138 | T>::type* = nullptr> |
| 139 | scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) { |
| 140 | return new FinalRefCountedObject<T>(std::forward<Args>(args)...); |
| 141 | } |
| 142 | |
| 143 | // `Ref<>`, `Ref<>::Type` and `Ref<>::Ptr`: |
| 144 | // |
| 145 | // `Ref` is a type declaring utility that is compatible with `make_ref_counted` |
| 146 | // and can be used in classes and methods where it's more convenient (or |
| 147 | // readable) to have the compiler figure out the fully fleshed out type for a |
| 148 | // class rather than spell it out verbatim in all places the type occurs (which |
| 149 | // can mean maintenance work if the class layout changes). |
| 150 | // |
| 151 | // Usage examples: |
| 152 | // |
| 153 | // If you want to declare the parameter type that's always compatible with |
| 154 | // this code: |
| 155 | // |
| 156 | // Bar(make_ref_counted<Foo>()); |
| 157 | // |
| 158 | // You can use `Ref<>::Ptr` to declare a compatible scoped_refptr type: |
| 159 | // |
| 160 | // void Bar(Ref<Foo>::Ptr p); |
| 161 | // |
| 162 | // This might be more practically useful in templates though. |
| 163 | // |
| 164 | // In rare cases you might need to be able to declare a parameter that's fully |
| 165 | // compatible with the reference counted T type - and just using T* is not |
| 166 | // enough. To give a code example, we can declare a function, `Foo` that is |
| 167 | // compatible with this code: |
| 168 | // auto p = make_ref_counted<Foo>(); |
| 169 | // Foo(p.get()); |
| 170 | // |
| 171 | // void Foo(Ref<Foo>::Type* foo_ptr); |
| 172 | // |
| 173 | // Alternatively this would be: |
| 174 | // void Foo(Foo* foo_ptr); |
| 175 | // or |
| 176 | // void Foo(FinalRefCountedObject<Foo>* foo_ptr); |
| 177 | |
| 178 | // Declares the approprate reference counted type for T depending on whether |
| 179 | // T is convertible to RefCountInterface or not. |
| 180 | // For classes that are convertible, the type will simply be T. |
| 181 | // For classes that cannot be converted to RefCountInterface, the type will be |
| 182 | // FinalRefCountedObject<T>. |
| 183 | // This is most useful for declaring a scoped_refptr<T> instance for a class |
| 184 | // that may or may not implement a virtual reference counted interface: |
| 185 | // * scoped_refptr<Ref<Foo>::Type> my_ptr; |
| 186 | template <typename T> |
| 187 | struct Ref { |
| 188 | typedef typename std::conditional< |
| 189 | std::is_convertible<T*, RefCountInterface*>::value, |
| 190 | T, |
| 191 | FinalRefCountedObject<T>>::type Type; |
| 192 | |
| 193 | typedef scoped_refptr<Type> Ptr; |
| 194 | }; |
| 195 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 196 | } // namespace rtc |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 197 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 198 | #endif // RTC_BASE_REF_COUNTED_OBJECT_H_ |