perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef RTC_BASE_REFCOUNTEDOBJECT_H_ |
| 11 | #define RTC_BASE_REFCOUNTEDOBJECT_H_ |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 12 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 13 | #include <utility> |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 14 | |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 15 | #include "rtc_base/constructormagic.h" |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 16 | #include "rtc_base/refcount.h" |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 17 | #include "rtc_base/refcounter.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | template <class T> |
| 22 | class RefCountedObject : public T { |
| 23 | public: |
| 24 | RefCountedObject() {} |
| 25 | |
| 26 | template <class P0> |
| 27 | explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {} |
| 28 | |
| 29 | template <class P0, class P1, class... Args> |
| 30 | RefCountedObject(P0&& p0, P1&& p1, Args&&... args) |
| 31 | : T(std::forward<P0>(p0), |
| 32 | std::forward<P1>(p1), |
| 33 | std::forward<Args>(args)...) {} |
| 34 | |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 35 | virtual void AddRef() const { ref_count_.IncRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 36 | |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 37 | virtual RefCountReleaseStatus Release() const { |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 38 | const auto status = ref_count_.DecRef(); |
| 39 | if (status == RefCountReleaseStatus::kDroppedLastRef) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | delete this; |
| 41 | } |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 42 | return status; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Return whether the reference count is one. If the reference count is used |
| 46 | // in the conventional way, a reference count of 1 implies that the current |
| 47 | // thread owns the reference and no other thread shares it. This call |
| 48 | // performs the test for a reference count of one, and performs the memory |
| 49 | // barrier needed for the owning thread to act on the object, knowing that it |
| 50 | // has exclusive access to the object. |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 51 | virtual bool HasOneRef() const { return ref_count_.HasOneRef(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 52 | |
| 53 | protected: |
| 54 | virtual ~RefCountedObject() {} |
| 55 | |
Niels Möller | 9155e49 | 2017-10-23 11:22:30 +0200 | [diff] [blame^] | 56 | mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
| 57 | |
| 58 | RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace rtc |
perkj | 0489e49 | 2016-10-20 00:24:01 -0700 | [diff] [blame] | 62 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 63 | #endif // RTC_BASE_REFCOUNTEDOBJECT_H_ |