| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef RTC_BASE_REF_COUNTED_OBJECT_H_ |
| 11 |
#define RTC_BASE_REF_COUNTED_OBJECT_H_ |
| 12 |
|
| 13 |
#include <type_traits> |
| 14 |
#include <utility> |
| 15 |
|
| 16 |
#include "rtc_base/constructor_magic.h" |
| 17 |
#include "rtc_base/ref_count.h" |
| 18 |
#include "rtc_base/ref_counter.h" |
| 19 |
|
| 20 |
namespace rtc { |
| 21 |
|
| 22 |
template <class T> |
| 23 |
class RefCountedObject : public T { |
| 24 |
public: |
| 25 |
RefCountedObject() {} |
| 26 |
|
| 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 |
|
| 36 |
virtual void AddRef() const { ref_count_.IncRef(); } |
| 37 |
|
| 38 |
virtual RefCountReleaseStatus Release() const { |
| 39 |
const auto status = ref_count_.DecRef(); |
| 40 |
if (status == RefCountReleaseStatus::kDroppedLastRef) { |
| 41 |
delete this; |
| 42 |
} |
| 43 |
return status; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
virtual bool HasOneRef() const { return ref_count_.HasOneRef(); } |
| 53 |
|
| 54 |
protected: |
| 55 |
virtual ~RefCountedObject() {} |
| 56 |
|
| 57 |
mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
| 58 |
|
| 59 |
RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject); |
| 60 |
}; |
| 61 |
|
| 62 |
template <class T> |
| 63 |
class FinalRefCountedObject final : public T { |
| 64 |
public: |
| 65 |
using T::T; |
| 66 |
|
| 67 |
|
| 68 |
FinalRefCountedObject() = default; |
| 69 |
FinalRefCountedObject(const FinalRefCountedObject&) = delete; |
| 70 |
FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete; |
| 71 |
|
| 72 |
void AddRef() const { ref_count_.IncRef(); } |
| 73 |
void Release() const { |
| 74 |
if (ref_count_.DecRef() == RefCountReleaseStatus::kDroppedLastRef) { |
| 75 |
delete this; |
| 76 |
} |
| 77 |
} |
| 78 |
bool HasOneRef() const { return ref_count_.HasOneRef(); } |
| 79 |
|
| 80 |
private: |
| 81 |
~FinalRefCountedObject() = default; |
| 82 |
|
| 83 |
mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; |
| 84 |
}; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
#endif |