Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: webrtc/rtc_base/refcountedobject.h

Issue 3010883002: Implement concrete class RefCountedBase.
Patch Set: Use "= default" for destructors. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #ifndef WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_ 10 #ifndef WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_
11 #define WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_ 11 #define WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_
12 12
13 #include <utility> 13 #include <utility>
14 14
15 #include "webrtc/rtc_base/atomicops.h" 15 #include "webrtc/rtc_base/atomicops.h"
16 16
17 namespace rtc { 17 namespace rtc {
18 18
19 template <class T> 19 // Concrete base class with inline and non-virtual AddRef and Release.
20 class RefCountedObject : public T { 20 class RefCountedBase {
21 public: 21 public:
22 RefCountedObject() {} 22 int AddRef() const { return AtomicOps::Increment(&ref_count_); }
23 23
24 template <class P0> 24 int Release() const {
25 explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {}
26
27 template <class P0, class P1, class... Args>
28 RefCountedObject(P0&& p0, P1&& p1, Args&&... args)
29 : T(std::forward<P0>(p0),
30 std::forward<P1>(p1),
31 std::forward<Args>(args)...) {}
32
33 virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); }
34
35 virtual int Release() const {
36 int count = AtomicOps::Decrement(&ref_count_); 25 int count = AtomicOps::Decrement(&ref_count_);
37 if (!count) { 26 if (!count) {
38 delete this; 27 delete this;
39 } 28 }
40 return count; 29 return count;
41 } 30 }
42 31
43 // Return whether the reference count is one. If the reference count is used 32 // Return whether the reference count is one. If the reference count is used
44 // in the conventional way, a reference count of 1 implies that the current 33 // in the conventional way, a reference count of 1 implies that the current
45 // thread owns the reference and no other thread shares it. This call 34 // thread owns the reference and no other thread shares it. This call
46 // performs the test for a reference count of one, and performs the memory 35 // performs the test for a reference count of one, and performs the memory
47 // barrier needed for the owning thread to act on the object, knowing that it 36 // barrier needed for the owning thread to act on the object, knowing that it
48 // has exclusive access to the object. 37 // has exclusive access to the object.
49 virtual bool HasOneRef() const { 38 bool HasOneRef() const {
50 return AtomicOps::AcquireLoad(&ref_count_) == 1; 39 return AtomicOps::AcquireLoad(&ref_count_) == 1;
51 } 40 }
52 41
53 protected: 42 protected:
54 virtual ~RefCountedObject() {} 43 virtual ~RefCountedBase() = default;
55 44
56 mutable volatile int ref_count_ = 0; 45 mutable volatile int ref_count_ = 0;
57 }; 46 };
58 47
48 template <class T>
49 class RefCountedObject : public T, protected virtual RefCountedBase {
50 public:
51 RefCountedObject() {}
52
53 template <class P0>
54 explicit RefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {}
55
56 template <class P0, class P1, class... Args>
57 RefCountedObject(P0&& p0, P1&& p1, Args&&... args)
58 : T(std::forward<P0>(p0),
59 std::forward<P1>(p1),
60 std::forward<Args>(args)...) {}
61
62 virtual int AddRef() const { return RefCountedBase::AddRef(); }
63 virtual int Release() const { return RefCountedBase::Release(); }
64 virtual bool HasOneRef() const { return RefCountedBase::HasOneRef(); }
65
66 protected:
67 virtual ~RefCountedObject() = default;
68 };
69
59 } // namespace rtc 70 } // namespace rtc
60 71
61 #endif // WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_ 72 #endif // WEBRTC_RTC_BASE_REFCOUNTEDOBJECT_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698