blob: 418c3d80cc71adeb369d73bb61043bced8968a83 [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
Tomas Gunnarssond7842002021-04-22 17:41:33 +020013#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080014#include "rtc_base/ref_count.h"
15#include "rtc_base/ref_counter.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016
17namespace rtc {
18
19template <class T>
20class RefCountedObject : public T {
21 public:
22 RefCountedObject() {}
23
Byoungchan Lee14af7622022-01-12 05:24:58 +090024 RefCountedObject(const RefCountedObject&) = delete;
25 RefCountedObject& operator=(const RefCountedObject&) = delete;
26
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027 template <class P0>
28 explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {}
29
30 template <class P0, class P1, class... Args>
31 RefCountedObject(P0&& p0, P1&& p1, Args&&... args)
32 : T(std::forward<P0>(p0),
33 std::forward<P1>(p1),
34 std::forward<Args>(args)...) {}
35
Tomas Gunnarssone249d192021-04-26 11:46:54 +020036 void AddRef() const override { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
Tomas Gunnarssone249d192021-04-26 11:46:54 +020038 RefCountReleaseStatus Release() const override {
Niels Möller9155e492017-10-23 11:22:30 +020039 const auto status = ref_count_.DecRef();
40 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 delete this;
42 }
Niels Möller9155e492017-10-23 11:22:30 +020043 return status;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 }
45
46 // Return whether the reference count is one. If the reference count is used
47 // in the conventional way, a reference count of 1 implies that the current
48 // thread owns the reference and no other thread shares it. This call
49 // performs the test for a reference count of one, and performs the memory
50 // barrier needed for the owning thread to act on the object, knowing that it
51 // has exclusive access to the object.
Niels Möller9155e492017-10-23 11:22:30 +020052 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053
54 protected:
Tomas Gunnarssone249d192021-04-26 11:46:54 +020055 ~RefCountedObject() override {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056
Niels Möller9155e492017-10-23 11:22:30 +020057 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058};
59
Danil Chapovalov8df643b2021-01-22 16:11:10 +010060template <class T>
61class FinalRefCountedObject final : public T {
62 public:
63 using T::T;
Niels Möllerbb57de22022-01-11 15:40:22 +010064 // Above using declaration propagates a default move constructor
65 // FinalRefCountedObject(FinalRefCountedObject&& other), but we also need
66 // move construction from T.
Danil Chapovalov80b76282021-04-26 16:32:27 +020067 explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {}
Danil Chapovalov8df643b2021-01-22 16:11:10 +010068 FinalRefCountedObject(const FinalRefCountedObject&) = delete;
69 FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete;
70
71 void AddRef() const { ref_count_.IncRef(); }
Niels Möllerb7aac6f2021-08-23 15:48:06 +020072 RefCountReleaseStatus Release() const {
73 const auto status = ref_count_.DecRef();
74 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Danil Chapovalov8df643b2021-01-22 16:11:10 +010075 delete this;
76 }
Niels Möllerb7aac6f2021-08-23 15:48:06 +020077 return status;
Danil Chapovalov8df643b2021-01-22 16:11:10 +010078 }
79 bool HasOneRef() const { return ref_count_.HasOneRef(); }
80
81 private:
82 ~FinalRefCountedObject() = default;
83
Danil Chapovalovd71e5912021-03-29 22:17:36 +020084 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Danil Chapovalov8df643b2021-01-22 16:11:10 +010085};
86
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087} // namespace rtc
perkj0489e492016-10-20 00:24:01 -070088
Steve Anton10542f22019-01-11 09:11:00 -080089#endif // RTC_BASE_REF_COUNTED_OBJECT_H_