blob: da3ed9ff2fda50ca3c9d62e4008095c73d9c1dd9 [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef RTC_BASE_REFCOUNTEDOBJECT_H_
11#define RTC_BASE_REFCOUNTEDOBJECT_H_
perkj0489e492016-10-20 00:24:01 -070012
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020013#include <utility>
perkj0489e492016-10-20 00:24:01 -070014
Niels Möller9155e492017-10-23 11:22:30 +020015#include "rtc_base/constructormagic.h"
Niels Möller6f72f562017-10-19 13:15:17 +020016#include "rtc_base/refcount.h"
Niels Möller9155e492017-10-23 11:22:30 +020017#include "rtc_base/refcounter.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
21template <class T>
22class 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öller9155e492017-10-23 11:22:30 +020035 virtual void AddRef() const { ref_count_.IncRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036
Niels Möller6f72f562017-10-19 13:15:17 +020037 virtual RefCountReleaseStatus Release() const {
Niels Möller9155e492017-10-23 11:22:30 +020038 const auto status = ref_count_.DecRef();
39 if (status == RefCountReleaseStatus::kDroppedLastRef) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040 delete this;
41 }
Niels Möller9155e492017-10-23 11:22:30 +020042 return status;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043 }
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öller9155e492017-10-23 11:22:30 +020051 virtual bool HasOneRef() const { return ref_count_.HasOneRef(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052
53 protected:
54 virtual ~RefCountedObject() {}
55
Niels Möller9155e492017-10-23 11:22:30 +020056 mutable webrtc::webrtc_impl::RefCounter ref_count_{0};
57
58 RTC_DISALLOW_COPY_AND_ASSIGN(RefCountedObject);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059};
60
61} // namespace rtc
perkj0489e492016-10-20 00:24:01 -070062
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#endif // RTC_BASE_REFCOUNTEDOBJECT_H_