blob: 331132c5693db0c67585ccc9a1f1012ba349a582 [file] [log] [blame]
perkj0489e492016-10-20 07:24:011/*
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 17:11:0010#ifndef RTC_BASE_REF_COUNTED_OBJECT_H_
11#define RTC_BASE_REF_COUNTED_OBJECT_H_
perkj0489e492016-10-20 07:24:0112
Yves Gerey3e707812018-11-28 15:47:4913#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <utility>
perkj0489e492016-10-20 07:24:0115
Tomas Gunnarssond7842002021-04-22 15:41:3316#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0017#include "rtc_base/constructor_magic.h"
18#include "rtc_base/ref_count.h"
19#include "rtc_base/ref_counter.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5020
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
Tomas Gunnarssone249d192021-04-26 09:46:5437 void AddRef() const override { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5038
Tomas Gunnarssone249d192021-04-26 09:46:5439 RefCountReleaseStatus Release() const override {
Niels Möller9155e492017-10-23 09:22:3040 const auto status = ref_count_.DecRef();
41 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5042 delete this;
43 }
Niels Möller9155e492017-10-23 09:22:3044 return status;
Henrik Kjellanderec78f1c2017-06-29 05:52:5045 }
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 09:22:3053 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5054
55 protected:
Tomas Gunnarssone249d192021-04-26 09:46:5456 ~RefCountedObject() override {}
Henrik Kjellanderec78f1c2017-06-29 05:52:5057
Niels Möller9155e492017-10-23 09:22:3058 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
59
60 RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject);
Henrik Kjellanderec78f1c2017-06-29 05:52:5061};
62
Danil Chapovalov8df643b2021-01-22 15:11:1063template <class T>
64class FinalRefCountedObject final : public T {
65 public:
66 using T::T;
Danil Chapovalov80b76282021-04-26 14:32:2767 // Until c++17 compilers are allowed not to inherit the default constructors.
68 // Thus the default constructors are forwarded explicitly.
Danil Chapovalov8df643b2021-01-22 15:11:1069 FinalRefCountedObject() = default;
Danil Chapovalov80b76282021-04-26 14:32:2770 explicit FinalRefCountedObject(const T& other) : T(other) {}
71 explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {}
Danil Chapovalov8df643b2021-01-22 15:11:1072 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 Chapovalovd71e5912021-03-29 20:17:3686 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Danil Chapovalov8df643b2021-01-22 15:11:1087};
88
Tomas Gunnarssond7842002021-04-22 15:41:3389// 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.
123template <
124 typename T,
125 typename... Args,
126 typename std::enable_if<std::is_convertible<T*, RefCountInterface*>::value,
127 T>::type* = nullptr>
128scoped_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.
134template <
135 typename T,
136 typename... Args,
137 typename std::enable_if<!std::is_convertible<T*, RefCountInterface*>::value,
138 T>::type* = nullptr>
139scoped_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;
186template <typename T>
187struct 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 Kjellanderec78f1c2017-06-29 05:52:50196} // namespace rtc
perkj0489e492016-10-20 07:24:01197
Steve Anton10542f22019-01-11 17:11:00198#endif // RTC_BASE_REF_COUNTED_OBJECT_H_