1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTCOUNTEDPTR_H__ |
---|
24 | #define __ZTCOUNTEDPTR_H__ |
---|
25 | |
---|
26 | #include <algorithm> |
---|
27 | #include <cassert> |
---|
28 | |
---|
29 | #include "zthread/AtomicCount.h" |
---|
30 | |
---|
31 | #ifdef _MSC_VER |
---|
32 | # pragma warning(push) |
---|
33 | # pragma warning(disable:4786) // warning: long template symbol name |
---|
34 | # pragma warning(push) |
---|
35 | # pragma warning(disable:4284) // warning: odd return type for operator-> |
---|
36 | #endif |
---|
37 | |
---|
38 | namespace ZThread { |
---|
39 | |
---|
40 | /** |
---|
41 | * @class CountedPtr |
---|
42 | * |
---|
43 | * @author Eric Crahen <http://www.code-foo.com/> |
---|
44 | * @date <2003-07-29T06:43:48-0400> |
---|
45 | * @version 2.3.0 |
---|
46 | * |
---|
47 | */ |
---|
48 | template <typename T, typename CountT = AtomicCount> |
---|
49 | class CountedPtr { |
---|
50 | |
---|
51 | #if !defined(__MWERKS__) |
---|
52 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
53 | template <typename U, typename V> friend class CountedPtr; |
---|
54 | #endif |
---|
55 | #endif |
---|
56 | |
---|
57 | CountT* _count; |
---|
58 | T* _instance; |
---|
59 | |
---|
60 | public: |
---|
61 | |
---|
62 | CountedPtr() : _count(0), _instance(0) { } |
---|
63 | |
---|
64 | #if !defined(__MWERKS__) |
---|
65 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
66 | |
---|
67 | explicit CountedPtr(T* raw) : _count(new CountT()), _instance(raw) { |
---|
68 | (*_count)++; |
---|
69 | } |
---|
70 | |
---|
71 | #endif |
---|
72 | #endif |
---|
73 | |
---|
74 | template <typename U> |
---|
75 | explicit CountedPtr(U* raw) : _count(new CountT()), _instance(raw) { |
---|
76 | (*_count)++; |
---|
77 | } |
---|
78 | |
---|
79 | #if !defined(__MWERKS__) |
---|
80 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
81 | |
---|
82 | CountedPtr(const CountedPtr& ptr) : _count(ptr._count), _instance(ptr._instance) { |
---|
83 | |
---|
84 | if(_count) |
---|
85 | (*_count)++; |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | #endif |
---|
90 | #endif |
---|
91 | |
---|
92 | template <typename U, typename V> |
---|
93 | CountedPtr(const CountedPtr<U, V>& ptr) : _count(ptr._count), _instance(ptr._instance) { |
---|
94 | |
---|
95 | if(_count) |
---|
96 | (*_count)++; |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | ~CountedPtr() { |
---|
101 | |
---|
102 | if(_count && --(*_count) == 0) { |
---|
103 | |
---|
104 | if(_instance) |
---|
105 | delete _instance; |
---|
106 | |
---|
107 | delete _count; |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | #if !defined(__MWERKS__) |
---|
115 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
116 | |
---|
117 | const CountedPtr& operator=(const CountedPtr& ptr) { |
---|
118 | |
---|
119 | typedef CountedPtr<T, CountT> ThisT; |
---|
120 | |
---|
121 | ThisT(ptr).swap(*this); |
---|
122 | return *this; |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | #endif |
---|
127 | #endif |
---|
128 | |
---|
129 | template <typename U, typename V> |
---|
130 | const CountedPtr& operator=(const CountedPtr<U, V>& ptr) { |
---|
131 | |
---|
132 | typedef CountedPtr<T, CountT> ThisT; |
---|
133 | |
---|
134 | ThisT(ptr).swap(*this); |
---|
135 | return *this; |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | void reset() { |
---|
140 | |
---|
141 | typedef CountedPtr<T, CountT> ThisT; |
---|
142 | ThisT().swap(*this); |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | #if !defined(__MWERKS__) |
---|
147 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
148 | |
---|
149 | void swap(CountedPtr& ptr) { |
---|
150 | |
---|
151 | std::swap(_count, ptr._count); |
---|
152 | std::swap(_instance, ptr._instance); |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | #endif |
---|
157 | #endif |
---|
158 | |
---|
159 | template <typename U, typename V> |
---|
160 | void swap(CountedPtr<U, V>& ptr) { |
---|
161 | |
---|
162 | std::swap(_count, ptr._count); |
---|
163 | std::swap(_instance, ptr._instance); |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | // Convience operators |
---|
168 | |
---|
169 | #if !defined(__MWERKS__) |
---|
170 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
171 | |
---|
172 | bool less(const CountedPtr& ptr) const { |
---|
173 | return _instance < ptr._instance; |
---|
174 | } |
---|
175 | |
---|
176 | #endif |
---|
177 | #endif |
---|
178 | |
---|
179 | template <typename U, typename V> |
---|
180 | bool less(const CountedPtr<U, V>& ptr) const { |
---|
181 | return _instance < ptr._instance; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | #if !defined(__MWERKS__) |
---|
186 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
187 | |
---|
188 | bool equal(const CountedPtr& ptr) const { |
---|
189 | return _count == ptr._count; |
---|
190 | } |
---|
191 | |
---|
192 | #endif |
---|
193 | #endif |
---|
194 | |
---|
195 | template <typename U, typename V> |
---|
196 | bool equal(const CountedPtr<U, V>& ptr) const { |
---|
197 | return _count == ptr._count; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | friend inline bool operator==(const CountedPtr& lhs, const CountedPtr& rhs) { |
---|
202 | return lhs.equal(rhs); |
---|
203 | } |
---|
204 | |
---|
205 | friend inline bool operator<(const CountedPtr& lhs, const CountedPtr& rhs) { |
---|
206 | return lhs.less(rhs); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | T& operator*() { |
---|
211 | assert(_instance != 0); |
---|
212 | return *_instance; |
---|
213 | } |
---|
214 | |
---|
215 | T* operator->() { |
---|
216 | assert(_instance != 0); |
---|
217 | return _instance; |
---|
218 | } |
---|
219 | |
---|
220 | const T* operator->() const { |
---|
221 | assert(_instance != 0); |
---|
222 | return _instance; |
---|
223 | } |
---|
224 | |
---|
225 | bool operator!() const { |
---|
226 | return _instance == 0; |
---|
227 | } |
---|
228 | |
---|
229 | operator bool() const { |
---|
230 | return _instance != 0; |
---|
231 | } |
---|
232 | |
---|
233 | }; /* CountedPtr */ |
---|
234 | |
---|
235 | template<typename U, typename V, typename X, typename Y> |
---|
236 | inline bool operator<(CountedPtr<U, V> const &lhs, CountedPtr<X, Y> const &rhs) { |
---|
237 | return lhs.less(rhs); |
---|
238 | } |
---|
239 | |
---|
240 | template<typename U, typename V, typename X, typename Y> |
---|
241 | inline bool operator==(CountedPtr<U, V> const &lhs, CountedPtr<X, Y> const &rhs) { |
---|
242 | return lhs.equal(rhs.get); |
---|
243 | } |
---|
244 | |
---|
245 | template<typename U, typename V, typename X, typename Y> |
---|
246 | inline bool operator!=(CountedPtr<U, V> const &lhs, CountedPtr<X, Y> const &rhs) { |
---|
247 | return !(lhs.equal(rhs.get)); |
---|
248 | } |
---|
249 | |
---|
250 | template<typename U, typename V, typename X, typename Y> |
---|
251 | inline void swap(CountedPtr<U, V> const &lhs, CountedPtr<X, Y> const &rhs) { |
---|
252 | lhs.swap(rhs); |
---|
253 | } |
---|
254 | |
---|
255 | #if !defined(__MWERKS__) |
---|
256 | #if !defined(_MSC_VER) || (_MSC_VER > 1200) |
---|
257 | |
---|
258 | template<typename U, typename V> |
---|
259 | inline bool operator<(CountedPtr<U, V> const &lhs, CountedPtr<U, V> const &rhs) { |
---|
260 | return lhs.less(rhs); |
---|
261 | } |
---|
262 | |
---|
263 | template<typename U, typename V> |
---|
264 | inline bool operator==(CountedPtr<U, V> const &lhs, CountedPtr<U, V> const &rhs) { |
---|
265 | return lhs.equal(rhs.get); |
---|
266 | } |
---|
267 | |
---|
268 | template<typename U, typename V> |
---|
269 | inline bool operator!=(CountedPtr<U, V> const &lhs, CountedPtr<U, V> const &rhs) { |
---|
270 | return !(lhs.equal(rhs.get)); |
---|
271 | } |
---|
272 | |
---|
273 | template<typename U, typename V> |
---|
274 | inline void swap(CountedPtr<U, V> const &lhs, CountedPtr<U, V> const &rhs) { |
---|
275 | lhs.swap(rhs); |
---|
276 | } |
---|
277 | |
---|
278 | #endif |
---|
279 | #endif |
---|
280 | |
---|
281 | } // namespace ZThread |
---|
282 | |
---|
283 | #ifdef _MSC_VER |
---|
284 | # pragma warning(pop) |
---|
285 | # pragma warning(pop) |
---|
286 | #endif |
---|
287 | |
---|
288 | |
---|
289 | #endif // __ZTCOUNTEDPTR_H__ |
---|