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 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 13 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 14 | #include "rtc_base/ref_count.h" |
| 15 | #include "rtc_base/ref_counter.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | template <class T> |
| 20 | class RefCountedObject : public T { |
| 21 | public: |
| 22 | RefCountedObject() {} |
| 23 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 24 | RefCountedObject(const RefCountedObject&) = delete; |
| 25 | RefCountedObject& operator=(const RefCountedObject&) = delete; |
| 26 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 27 | 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 Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 36 | void AddRef() const override { ref_count_.IncRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 37 | |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 38 | RefCountReleaseStatus Release() const override { |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 39 | const auto status = ref_count_.DecRef(); |
| 40 | if (status == RefCountReleaseStatus::kDroppedLastRef) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 41 | delete this; |
| 42 | } |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 43 | return status; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 44 | } |
| 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öller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 52 | virtual bool HasOneRef() const { return ref_count_.HasOneRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 53 | |
| 54 | protected: |
Tomas Gunnarsson | e249d19 | 2021-04-26 09:46:54 | [diff] [blame] | 55 | ~RefCountedObject() override {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 56 | |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 57 | mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 58 | }; |
| 59 | |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 60 | template <class T> |
| 61 | class FinalRefCountedObject final : public T { |
| 62 | public: |
| 63 | using T::T; |
Niels Möller | bb57de2 | 2022-01-11 14:40:22 | [diff] [blame] | 64 | // Above using declaration propagates a default move constructor |
| 65 | // FinalRefCountedObject(FinalRefCountedObject&& other), but we also need |
| 66 | // move construction from T. |
Danil Chapovalov | 80b7628 | 2021-04-26 14:32:27 | [diff] [blame] | 67 | explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {} |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 68 | FinalRefCountedObject(const FinalRefCountedObject&) = delete; |
| 69 | FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete; |
| 70 | |
| 71 | void AddRef() const { ref_count_.IncRef(); } |
Niels Möller | b7aac6f5 | 2021-08-23 13:48:06 | [diff] [blame] | 72 | RefCountReleaseStatus Release() const { |
| 73 | const auto status = ref_count_.DecRef(); |
| 74 | if (status == RefCountReleaseStatus::kDroppedLastRef) { |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 75 | delete this; |
| 76 | } |
Niels Möller | b7aac6f5 | 2021-08-23 13:48:06 | [diff] [blame] | 77 | return status; |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 78 | } |
| 79 | bool HasOneRef() const { return ref_count_.HasOneRef(); } |
| 80 | |
| 81 | private: |
| 82 | ~FinalRefCountedObject() = default; |
| 83 | |
Danil Chapovalov | d71e591 | 2021-03-29 20:17:36 | [diff] [blame] | 84 | mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 85 | }; |
| 86 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 87 | } // namespace rtc |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 88 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 89 | #endif // RTC_BASE_REF_COUNTED_OBJECT_H_ |