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 | |
11 #ifndef WEBRTC_BASE_REFCOUNT_H_ | 10 #ifndef WEBRTC_BASE_REFCOUNT_H_ |
12 #define WEBRTC_BASE_REFCOUNT_H_ | 11 #define WEBRTC_BASE_REFCOUNT_H_ |
13 | 12 |
14 #include <string.h> | 13 #include "webrtc/base/refcountedobject.h" |
15 #include <utility> | |
16 | |
17 #include "webrtc/base/atomicops.h" | |
18 | 14 |
19 namespace rtc { | 15 namespace rtc { |
20 | 16 |
21 // Reference count interface. | 17 // Reference count interface. |
22 class RefCountInterface { | 18 class RefCountInterface { |
23 public: | 19 public: |
24 virtual int AddRef() const = 0; | 20 virtual int AddRef() const = 0; |
25 virtual int Release() const = 0; | 21 virtual int Release() const = 0; |
26 | 22 |
27 protected: | 23 protected: |
28 virtual ~RefCountInterface() {} | 24 virtual ~RefCountInterface() {} |
29 }; | 25 }; |
30 | 26 |
31 template <class T> | |
32 class RefCountedObject : public T { | |
33 public: | |
34 RefCountedObject() {} | |
35 | |
36 template <typename P> | |
37 explicit RefCountedObject(const P& p) : T(p) {} | |
38 | |
39 template <typename P> | |
40 explicit RefCountedObject(P&& p) : T(std::move(p)) {} | |
41 | |
42 template <typename P1, typename P2> | |
43 RefCountedObject(P1 p1, P2 p2) : T(p1, p2) {} | |
44 | |
45 template <typename P1, typename P2, typename P3> | |
46 RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3) {} | |
47 | |
48 template <typename P1, typename P2, typename P3, typename P4> | |
49 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4) {} | |
50 | |
51 template <typename P1, typename P2, typename P3, typename P4, typename P5> | |
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> | |
60 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) | |
61 : T(p1, p2, p3, p4, p5, p6) {} | |
62 | |
63 template <typename P1, | |
64 typename P2, | |
65 typename P3, | |
66 typename P4, | |
67 typename P5, | |
68 typename P6, | |
69 typename P7> | |
70 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) | |
71 : T(p1, p2, p3, p4, p5, p6, p7) {} | |
72 | |
73 template <typename P1, | |
74 typename P2, | |
75 typename P3, | |
76 typename P4, | |
77 typename P5, | |
78 typename P6, | |
79 typename P7, | |
80 typename P8> | |
81 RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) | |
82 : T(p1, p2, p3, p4, p5, p6, p7, p8) {} | |
83 | |
84 template <typename P1, | |
85 typename P2, | |
86 typename P3, | |
87 typename P4, | |
88 typename P5, | |
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) {} | |
103 | |
104 template <typename P1, | |
105 typename P2, | |
106 typename P3, | |
107 typename P4, | |
108 typename P5, | |
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) {} | |
125 | |
126 template <typename P1, | |
127 typename P2, | |
128 typename P3, | |
129 typename P4, | |
130 typename P5, | |
131 typename P6, | |
132 typename P7, | |
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) {} | |
149 | |
150 virtual int AddRef() const { return AtomicOps::Increment(&ref_count_); } | |
151 | |
152 virtual int Release() const { | |
153 int count = AtomicOps::Decrement(&ref_count_); | |
154 if (!count) { | |
155 delete this; | |
156 } | |
157 return count; | |
158 } | |
159 | |
160 // Return whether the reference count is one. If the reference count is used | |
161 // in the conventional way, a reference count of 1 implies that the current | |
162 // thread owns the reference and no other thread shares it. This call | |
163 // performs the test for a reference count of one, and performs the memory | |
164 // barrier needed for the owning thread to act on the object, knowing that it | |
165 // has exclusive access to the object. | |
166 virtual bool HasOneRef() const { | |
167 return AtomicOps::AcquireLoad(&ref_count_) == 1; | |
168 } | |
169 | |
170 protected: | |
171 virtual ~RefCountedObject() {} | |
172 | |
173 mutable volatile int ref_count_ = 0; | |
174 }; | |
175 | |
176 } // namespace rtc | 27 } // namespace rtc |
177 | 28 |
178 #endif // WEBRTC_BASE_REFCOUNT_H_ | 29 #endif // WEBRTC_BASE_REFCOUNT_H_ |
OLD | NEW |