blob: c9faefe06836de38fb1b7f7325073961fc333888 [file] [log] [blame]
perkj0489e492016-10-20 00:24:01 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080010#ifndef RTC_BASE_REF_COUNTED_OBJECT_H_
11#define RTC_BASE_REF_COUNTED_OBJECT_H_
perkj0489e492016-10-20 00:24:01 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <utility>
perkj0489e492016-10-20 00:24:01 -070015
Tomas Gunnarssond7842002021-04-22 17:41:33 +020016#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/constructor_magic.h"
18#include "rtc_base/ref_count.h"
19#include "rtc_base/ref_counter.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
23template <class T>
24class 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
Niels Möller9155e492017-10-23 11:22:30 +020037 virtual void AddRef() const { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
Niels Möller6f72f562017-10-19 13:15:17 +020039 virtual RefCountReleaseStatus Release() const {
Niels Möller9155e492017-10-23 11:22:30 +020040 const auto status = ref_count_.DecRef();
41 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042 delete this;
43 }
Niels Möller9155e492017-10-23 11:22:30 +020044 return status;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045 }
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öller9155e492017-10-23 11:22:30 +020053 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
55 protected:
56 virtual ~RefCountedObject() {}
57
Niels Möller9155e492017-10-23 11:22:30 +020058 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
59
60 RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061};
62
Danil Chapovalov8df643b2021-01-22 16:11:10 +010063template <class T>
64class FinalRefCountedObject final : public T {
65 public:
66 using T::T;
67 // Until c++17 compilers are allowed not to inherit the default constructor,
68 // and msvc doesn't. Thus the default constructor is forwarded explicitly.
69 FinalRefCountedObject() = default;
70 FinalRefCountedObject(const FinalRefCountedObject&) = delete;
71 FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete;
72
Tomas Gunnarssond7842002021-04-22 17:41:33 +020073 template <class P0>
74 explicit FinalRefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {}
75
76 template <class P0, class P1, class... Args>
77 FinalRefCountedObject(P0&& p0, P1&& p1, Args&&... args)
78 : T(std::forward<P0>(p0),
79 std::forward<P1>(p1),
80 std::forward<Args>(args)...) {}
81
Danil Chapovalov8df643b2021-01-22 16:11:10 +010082 void AddRef() const { ref_count_.IncRef(); }
83 void Release() const {
84 if (ref_count_.DecRef() == RefCountReleaseStatus::kDroppedLastRef) {
85 delete this;
86 }
87 }
88 bool HasOneRef() const { return ref_count_.HasOneRef(); }
89
90 private:
91 ~FinalRefCountedObject() = default;
92
Danil Chapovalovd71e5912021-03-29 22:17:36 +020093 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Danil Chapovalov8df643b2021-01-22 16:11:10 +010094};
95
Tomas Gunnarssond7842002021-04-22 17:41:33 +020096// General utilities for constructing a reference counted class and the
97// appropriate reference count implementation for that class.
98//
99// These utilities select either the `RefCountedObject` implementation or
100// `FinalRefCountedObject` depending on whether the to-be-shared class is
101// derived from the RefCountInterface interface or not (respectively).
102
103// `make_ref_counted`:
104//
105// Use this when you want to construct a reference counted object of type T and
106// get a `scoped_refptr<>` back. Example:
107//
108// auto p = make_ref_counted<Foo>("bar", 123);
109//
110// For a class that inherits from RefCountInterface, this is equivalent to:
111//
112// auto p = scoped_refptr<Foo>(new RefCountedObject<Foo>("bar", 123));
113//
114// If the class does not inherit from RefCountInterface, the example is
115// equivalent to:
116//
117// auto p = scoped_refptr<FinalRefCountedObject<Foo>>(
118// new FinalRefCountedObject<Foo>("bar", 123));
119//
120// In these cases, `make_ref_counted` reduces the amount of boilerplate code but
121// also helps with the most commonly intended usage of RefCountedObject whereby
122// methods for reference counting, are virtual and designed to satisfy the need
123// of an interface. When such a need does not exist, it is more efficient to use
124// the `FinalRefCountedObject` template, which does not add the vtable overhead.
125//
126// Note that in some cases, using RefCountedObject directly may still be what's
127// needed.
128
129// `make_ref_counted` for classes that are convertible to RefCountInterface.
130template <
131 typename T,
132 typename... Args,
133 typename std::enable_if<std::is_convertible<T*, RefCountInterface*>::value,
134 T>::type* = nullptr>
135scoped_refptr<T> make_ref_counted(Args&&... args) {
136 return new RefCountedObject<T>(std::forward<Args>(args)...);
137}
138
139// `make_ref_counted` for complete classes that are not convertible to
140// RefCountInterface.
141template <
142 typename T,
143 typename... Args,
144 typename std::enable_if<!std::is_convertible<T*, RefCountInterface*>::value,
145 T>::type* = nullptr>
146scoped_refptr<FinalRefCountedObject<T>> make_ref_counted(Args&&... args) {
147 return new FinalRefCountedObject<T>(std::forward<Args>(args)...);
148}
149
150// `Ref<>`, `Ref<>::Type` and `Ref<>::Ptr`:
151//
152// `Ref` is a type declaring utility that is compatible with `make_ref_counted`
153// and can be used in classes and methods where it's more convenient (or
154// readable) to have the compiler figure out the fully fleshed out type for a
155// class rather than spell it out verbatim in all places the type occurs (which
156// can mean maintenance work if the class layout changes).
157//
158// Usage examples:
159//
160// If you want to declare the parameter type that's always compatible with
161// this code:
162//
163// Bar(make_ref_counted<Foo>());
164//
165// You can use `Ref<>::Ptr` to declare a compatible scoped_refptr type:
166//
167// void Bar(Ref<Foo>::Ptr p);
168//
169// This might be more practically useful in templates though.
170//
171// In rare cases you might need to be able to declare a parameter that's fully
172// compatible with the reference counted T type - and just using T* is not
173// enough. To give a code example, we can declare a function, `Foo` that is
174// compatible with this code:
175// auto p = make_ref_counted<Foo>();
176// Foo(p.get());
177//
178// void Foo(Ref<Foo>::Type* foo_ptr);
179//
180// Alternatively this would be:
181// void Foo(Foo* foo_ptr);
182// or
183// void Foo(FinalRefCountedObject<Foo>* foo_ptr);
184
185// Declares the approprate reference counted type for T depending on whether
186// T is convertible to RefCountInterface or not.
187// For classes that are convertible, the type will simply be T.
188// For classes that cannot be converted to RefCountInterface, the type will be
189// FinalRefCountedObject<T>.
190// This is most useful for declaring a scoped_refptr<T> instance for a class
191// that may or may not implement a virtual reference counted interface:
192// * scoped_refptr<Ref<Foo>::Type> my_ptr;
193template <typename T>
194struct Ref {
195 typedef typename std::conditional<
196 std::is_convertible<T*, RefCountInterface*>::value,
197 T,
198 FinalRefCountedObject<T>>::type Type;
199
200 typedef scoped_refptr<Type> Ptr;
201};
202
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200203} // namespace rtc
perkj0489e492016-10-20 00:24:01 -0700204
Steve Anton10542f22019-01-11 09:11:00 -0800205#endif // RTC_BASE_REF_COUNTED_OBJECT_H_