blob: 418c3d80cc71adeb369d73bb61043bced8968a83 [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
Tomas Gunnarssond7842002021-04-22 15:41:3313#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0014#include "rtc_base/ref_count.h"
15#include "rtc_base/ref_counter.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5016
17namespace rtc {
18
19template <class T>
20class RefCountedObject : public T {
21 public:
22 RefCountedObject() {}
23
Byoungchan Lee14af7622022-01-11 20:24:5824 RefCountedObject(const RefCountedObject&) = delete;
25 RefCountedObject& operator=(const RefCountedObject&) = delete;
26
Henrik Kjellanderec78f1c2017-06-29 05:52:5027 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 09:46:5436 void AddRef() const override { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5037
Tomas Gunnarssone249d192021-04-26 09:46:5438 RefCountReleaseStatus Release() const override {
Niels Möller9155e492017-10-23 09:22:3039 const auto status = ref_count_.DecRef();
40 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5041 delete this;
42 }
Niels Möller9155e492017-10-23 09:22:3043 return status;
Henrik Kjellanderec78f1c2017-06-29 05:52:5044 }
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 09:22:3052 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5053
54 protected:
Tomas Gunnarssone249d192021-04-26 09:46:5455 ~RefCountedObject() override {}
Henrik Kjellanderec78f1c2017-06-29 05:52:5056
Niels Möller9155e492017-10-23 09:22:3057 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Henrik Kjellanderec78f1c2017-06-29 05:52:5058};
59
Danil Chapovalov8df643b2021-01-22 15:11:1060template <class T>
61class FinalRefCountedObject final : public T {
62 public:
63 using T::T;
Niels Möllerbb57de22022-01-11 14:40:2264 // 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 14:32:2767 explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {}
Danil Chapovalov8df643b2021-01-22 15:11:1068 FinalRefCountedObject(const FinalRefCountedObject&) = delete;
69 FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete;
70
71 void AddRef() const { ref_count_.IncRef(); }
Niels Möllerb7aac6f52021-08-23 13:48:0672 RefCountReleaseStatus Release() const {
73 const auto status = ref_count_.DecRef();
74 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Danil Chapovalov8df643b2021-01-22 15:11:1075 delete this;
76 }
Niels Möllerb7aac6f52021-08-23 13:48:0677 return status;
Danil Chapovalov8df643b2021-01-22 15:11:1078 }
79 bool HasOneRef() const { return ref_count_.HasOneRef(); }
80
81 private:
82 ~FinalRefCountedObject() = default;
83
Danil Chapovalovd71e5912021-03-29 20:17:3684 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
Danil Chapovalov8df643b2021-01-22 15:11:1085};
86
Henrik Kjellanderec78f1c2017-06-29 05:52:5087} // namespace rtc
perkj0489e492016-10-20 07:24:0188
Steve Anton10542f22019-01-11 17:11:0089#endif // RTC_BASE_REF_COUNTED_OBJECT_H_