OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 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 | 10 |
11 #ifndef WEBRTC_BASE_REFCOUNT_H_ | 11 #ifndef WEBRTC_BASE_REFCOUNT_H_ |
12 #define WEBRTC_BASE_REFCOUNT_H_ | 12 #define WEBRTC_BASE_REFCOUNT_H_ |
13 | 13 |
14 #include <string.h> | 14 #include <string.h> |
| 15 #include <utility> |
15 | 16 |
16 #include "webrtc/base/atomicops.h" | 17 #include "webrtc/base/atomicops.h" |
17 | 18 |
18 namespace rtc { | 19 namespace rtc { |
19 | 20 |
20 // Reference count interface. | 21 // Reference count interface. |
21 class RefCountInterface { | 22 class RefCountInterface { |
22 public: | 23 public: |
23 virtual int AddRef() const = 0; | 24 virtual int AddRef() const = 0; |
24 virtual int Release() const = 0; | 25 virtual int Release() const = 0; |
| 26 |
25 protected: | 27 protected: |
26 virtual ~RefCountInterface() {} | 28 virtual ~RefCountInterface() {} |
27 }; | 29 }; |
28 | 30 |
29 template <class T> | 31 template <class T> |
30 class RefCountedObject : public T { | 32 class RefCountedObject : public T { |
31 public: | 33 public: |
32 RefCountedObject() : ref_count_(0) { | 34 RefCountedObject() {} |
33 } | |
34 | 35 |
35 template<typename P> | 36 template <typename P> |
36 explicit RefCountedObject(P p) : T(p), ref_count_(0) { | 37 explicit RefCountedObject(const P& p) : T(p) {} |
37 } | |
38 | 38 |
39 template<typename P1, typename P2> | 39 template <typename P> |
40 RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) { | 40 explicit RefCountedObject(P&& p) : T(std::move(p)) {} |
41 } | |
42 | 41 |
43 template<typename P1, typename P2, typename P3> | 42 template <typename P1, typename P2> |
44 RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) { | 43 RefCountedObject(P1 p1, P2 p2) : T(p1, p2) {} |
45 } | |
46 | 44 |
47 template<typename P1, typename P2, typename P3, typename P4> | 45 template <typename P1, typename P2, typename P3> |
48 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) | 46 RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3) {} |
49 : T(p1, p2, p3, p4), ref_count_(0) { | |
50 } | |
51 | 47 |
52 template<typename P1, typename P2, typename P3, typename P4, typename P5> | 48 template <typename P1, typename P2, typename P3, typename P4> |
53 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) | 49 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4) {} |
54 : T(p1, p2, p3, p4, p5), ref_count_(0) { | |
55 } | |
56 | 50 |
57 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 51 template <typename P1, typename P2, typename P3, typename P4, typename P5> |
58 typename P6> | 52 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) : T(p1, p2, p3, p4, p5) {} |
| 53 |
| 54 template <typename P1, |
| 55 typename P2, |
| 56 typename P3, |
| 57 typename P4, |
| 58 typename P5, |
| 59 typename P6> |
59 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) | 60 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) |
60 : T(p1, p2, p3, p4, p5, p6), ref_count_(0) { | 61 : T(p1, p2, p3, p4, p5, p6) {} |
61 } | |
62 | 62 |
63 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 63 template <typename P1, |
64 typename P6, typename P7> | 64 typename P2, |
| 65 typename P3, |
| 66 typename P4, |
| 67 typename P5, |
| 68 typename P6, |
| 69 typename P7> |
65 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) | 70 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) |
66 : T(p1, p2, p3, p4, p5, p6, p7), ref_count_(0) { | 71 : T(p1, p2, p3, p4, p5, p6, p7) {} |
67 } | |
68 | 72 |
69 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 73 template <typename P1, |
70 typename P6, typename P7, typename P8> | 74 typename P2, |
| 75 typename P3, |
| 76 typename P4, |
| 77 typename P5, |
| 78 typename P6, |
| 79 typename P7, |
| 80 typename P8> |
71 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) | 81 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) |
72 : T(p1, p2, p3, p4, p5, p6, p7, p8), ref_count_(0) { | 82 : T(p1, p2, p3, p4, p5, p6, p7, p8) {} |
73 } | |
74 | 83 |
75 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 84 template <typename P1, |
76 typename P6, typename P7, typename P8, typename P9> | 85 typename P2, |
77 RefCountedObject( | 86 typename P3, |
78 P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) | 87 typename P4, |
79 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9), ref_count_(0) { | 88 typename P5, |
80 } | 89 typename P6, |
| 90 typename P7, |
| 91 typename P8, |
| 92 typename P9> |
| 93 RefCountedObject(P1 p1, |
| 94 P2 p2, |
| 95 P3 p3, |
| 96 P4 p4, |
| 97 P5 p5, |
| 98 P6 p6, |
| 99 P7 p7, |
| 100 P8 p8, |
| 101 P9 p9) |
| 102 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9) {} |
81 | 103 |
82 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 104 template <typename P1, |
83 typename P6, typename P7, typename P8, typename P9, typename P10> | 105 typename P2, |
84 RefCountedObject( | 106 typename P3, |
85 P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) | 107 typename P4, |
86 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10), ref_count_(0) { | 108 typename P5, |
87 } | 109 typename P6, |
| 110 typename P7, |
| 111 typename P8, |
| 112 typename P9, |
| 113 typename P10> |
| 114 RefCountedObject(P1 p1, |
| 115 P2 p2, |
| 116 P3 p3, |
| 117 P4 p4, |
| 118 P5 p5, |
| 119 P6 p6, |
| 120 P7 p7, |
| 121 P8 p8, |
| 122 P9 p9, |
| 123 P10 p10) |
| 124 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) {} |
88 | 125 |
89 template<typename P1, typename P2, typename P3, typename P4, typename P5, | 126 template <typename P1, |
90 typename P6, typename P7, typename P8, typename P9, typename P10, | 127 typename P2, |
91 typename P11> | 128 typename P3, |
92 RefCountedObject( | 129 typename P4, |
93 P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, | 130 typename P5, |
94 P11 p11) | 131 typename P6, |
95 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_(0) { | 132 typename P7, |
96 } | 133 typename P8, |
| 134 typename P9, |
| 135 typename P10, |
| 136 typename P11> |
| 137 RefCountedObject(P1 p1, |
| 138 P2 p2, |
| 139 P3 p3, |
| 140 P4 p4, |
| 141 P5 p5, |
| 142 P6 p6, |
| 143 P7 p7, |
| 144 P8 p8, |
| 145 P9 p9, |
| 146 P10 p10, |
| 147 P11 p11) |
| 148 : T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) {} |
97 | 149 |
98 virtual int AddRef() const { | 150 virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); } |
99 return AtomicOps::Increment(&ref_count_); | |
100 } | |
101 | 151 |
102 virtual int Release() const { | 152 virtual int Release() const { |
103 int count = AtomicOps::Decrement(&ref_count_); | 153 int count = AtomicOps::Decrement(&ref_count_); |
104 if (!count) { | 154 if (!count) { |
105 delete this; | 155 delete this; |
106 } | 156 } |
107 return count; | 157 return count; |
108 } | 158 } |
109 | 159 |
110 // Return whether the reference count is one. If the reference count is used | 160 // Return whether the reference count is one. If the reference count is used |
111 // in the conventional way, a reference count of 1 implies that the current | 161 // in the conventional way, a reference count of 1 implies that the current |
112 // thread owns the reference and no other thread shares it. This call | 162 // thread owns the reference and no other thread shares it. This call |
113 // performs the test for a reference count of one, and performs the memory | 163 // performs the test for a reference count of one, and performs the memory |
114 // barrier needed for the owning thread to act on the object, knowing that it | 164 // barrier needed for the owning thread to act on the object, knowing that it |
115 // has exclusive access to the object. | 165 // has exclusive access to the object. |
116 virtual bool HasOneRef() const { | 166 virtual bool HasOneRef() const { |
117 return AtomicOps::AcquireLoad(&ref_count_) == 1; | 167 return AtomicOps::AcquireLoad(&ref_count_) == 1; |
118 } | 168 } |
119 | 169 |
120 protected: | 170 protected: |
121 virtual ~RefCountedObject() { | 171 virtual ~RefCountedObject() {} |
122 } | |
123 | 172 |
124 mutable volatile int ref_count_; | 173 mutable volatile int ref_count_ = 0; |
125 }; | 174 }; |
126 | 175 |
127 } // namespace rtc | 176 } // namespace rtc |
128 | 177 |
129 #endif // WEBRTC_BASE_REFCOUNT_H_ | 178 #endif // WEBRTC_BASE_REFCOUNT_H_ |
OLD | NEW |