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