blob: ce18379d50d9813c0dd74884f8e94dcdc40900cc [file] [log] [blame]
perkj0489e492016-10-20 00:24:01 -07001/*
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 09:11:00 -080010#ifndef RTC_BASE_REF_COUNTED_OBJECT_H_
11#define RTC_BASE_REF_COUNTED_OBJECT_H_
perkj0489e492016-10-20 00:24:01 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <utility>
perkj0489e492016-10-20 00:24:01 -070015
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/constructor_magic.h"
17#include "rtc_base/ref_count.h"
18#include "rtc_base/ref_counter.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace rtc {
21
22template <class T>
23class 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
Niels Möller9155e492017-10-23 11:22:30 +020036 virtual void AddRef() const { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
Niels Möller6f72f562017-10-19 13:15:17 +020038 virtual RefCountReleaseStatus Release() const {
Niels Möller9155e492017-10-23 11:22:30 +020039 const auto status = ref_count_.DecRef();
40 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 delete this;
42 }
Niels Möller9155e492017-10-23 11:22:30 +020043 return status;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 }
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 11:22:30 +020052 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053
54 protected:
55 virtual ~RefCountedObject() {}
56
Niels Möller9155e492017-10-23 11:22:30 +020057 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
58
59 RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060};
61
62} // namespace rtc
perkj0489e492016-10-20 00:24:01 -070063
Steve Anton10542f22019-01-11 09:11:00 -080064#endif // RTC_BASE_REF_COUNTED_OBJECT_H_