Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // This file is the adaptation for Interprocess of boost/detail/shared_count.hpp
|
Chris@16
|
4 //
|
Chris@16
|
5 // (C) Copyright Peter Dimov and Multi Media Ltd. 2001, 2002, 2003
|
Chris@16
|
6 // (C) Copyright Peter Dimov 2004-2005
|
Chris@16
|
7 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
|
Chris@16
|
8 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //
|
Chris@16
|
11 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
12 //
|
Chris@16
|
13 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
14 #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED
|
Chris@16
|
15 #define BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED
|
Chris@16
|
16
|
Chris@16
|
17 // MS compatible compilers support #pragma once
|
Chris@16
|
18
|
Chris@16
|
19 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
20 # pragma once
|
Chris@16
|
21 #endif
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
24 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 #include <boost/checked_delete.hpp>
|
Chris@16
|
27 #include <boost/intrusive/pointer_traits.hpp>
|
Chris@16
|
28 #include <boost/interprocess/smart_ptr/detail/bad_weak_ptr.hpp>
|
Chris@16
|
29 #include <boost/interprocess/smart_ptr/detail/sp_counted_impl.hpp>
|
Chris@16
|
30 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
31 #include <boost/container/allocator_traits.hpp>
|
Chris@16
|
32 #include <boost/detail/no_exceptions_support.hpp>
|
Chris@16
|
33 #include <functional> // std::less
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace interprocess {
|
Chris@16
|
37 namespace ipcdetail{
|
Chris@16
|
38
|
Chris@16
|
39 template<class T, class VoidAllocator, class Deleter>
|
Chris@16
|
40 class weak_count;
|
Chris@16
|
41
|
Chris@16
|
42 template<class T, class VoidAllocator, class Deleter>
|
Chris@16
|
43 class shared_count
|
Chris@16
|
44 {
|
Chris@16
|
45 public:
|
Chris@16
|
46 typedef typename boost::intrusive::
|
Chris@16
|
47 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
48 rebind_pointer<T>::type pointer;
|
Chris@16
|
49
|
Chris@16
|
50 private:
|
Chris@16
|
51 typedef sp_counted_impl_pd<VoidAllocator, Deleter> counted_impl;
|
Chris@16
|
52
|
Chris@16
|
53 typedef typename boost::intrusive::
|
Chris@16
|
54 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
55 rebind_pointer<counted_impl>::type counted_impl_ptr;
|
Chris@16
|
56 typedef typename boost::intrusive::
|
Chris@16
|
57 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
58 rebind_pointer<sp_counted_base>::type counted_base_ptr;
|
Chris@16
|
59
|
Chris@16
|
60 typedef boost::container::allocator_traits<VoidAllocator> vallocator_traits;
|
Chris@16
|
61
|
Chris@16
|
62 typedef typename vallocator_traits::template
|
Chris@16
|
63 portable_rebind_alloc<counted_impl>::type counted_impl_allocator;
|
Chris@16
|
64
|
Chris@16
|
65 typedef typename boost::intrusive::
|
Chris@16
|
66 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
67 rebind_pointer<const Deleter>::type const_deleter_pointer;
|
Chris@16
|
68
|
Chris@16
|
69 typedef typename boost::intrusive::
|
Chris@16
|
70 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
71 rebind_pointer<const VoidAllocator>::type const_allocator_pointer;
|
Chris@16
|
72
|
Chris@16
|
73 pointer m_px;
|
Chris@16
|
74 counted_impl_ptr m_pi;
|
Chris@16
|
75
|
Chris@16
|
76 template <class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
77 friend class weak_count;
|
Chris@16
|
78
|
Chris@16
|
79 template <class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
80 friend class shared_count;
|
Chris@16
|
81
|
Chris@16
|
82 public:
|
Chris@16
|
83
|
Chris@16
|
84 shared_count()
|
Chris@16
|
85 : m_px(0), m_pi(0) // nothrow
|
Chris@16
|
86 {}
|
Chris@16
|
87
|
Chris@16
|
88 template <class Ptr>
|
Chris@16
|
89 shared_count(const shared_count &other_shared_count, const Ptr &p)
|
Chris@16
|
90 : m_px(p), m_pi(other_shared_count.m_pi)
|
Chris@16
|
91 {}
|
Chris@16
|
92
|
Chris@16
|
93 template <class Ptr>
|
Chris@16
|
94 shared_count(const Ptr &p, const VoidAllocator &a, Deleter d)
|
Chris@16
|
95 : m_px(p), m_pi(0)
|
Chris@16
|
96 {
|
Chris@16
|
97 BOOST_TRY{
|
Chris@16
|
98 if(p){
|
Chris@16
|
99 counted_impl_allocator alloc(a);
|
Chris@16
|
100 m_pi = alloc.allocate(1);
|
Chris@16
|
101 //Anti-exception deallocator
|
Chris@16
|
102 scoped_ptr<counted_impl,
|
Chris@16
|
103 scoped_ptr_dealloc_functor<counted_impl_allocator> >
|
Chris@16
|
104 deallocator(m_pi, alloc);
|
Chris@16
|
105 //It's more correct to use VoidAllocator::construct but
|
Chris@16
|
106 //this needs copy constructor and we don't like it
|
Chris@16
|
107 new(ipcdetail::to_raw_pointer(m_pi))counted_impl(p, a, d);
|
Chris@16
|
108 deallocator.release();
|
Chris@16
|
109 }
|
Chris@16
|
110 }
|
Chris@16
|
111 BOOST_CATCH (...){
|
Chris@16
|
112 d(p); // delete p
|
Chris@16
|
113 BOOST_RETHROW
|
Chris@16
|
114 }
|
Chris@16
|
115 BOOST_CATCH_END
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 ~shared_count() // nothrow
|
Chris@16
|
119 {
|
Chris@16
|
120 if(m_pi)
|
Chris@16
|
121 m_pi->release();
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 shared_count(shared_count const & r)
|
Chris@16
|
125 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
|
Chris@16
|
126 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
|
Chris@16
|
127
|
Chris@16
|
128 //this is a test
|
Chris@16
|
129 template<class Y>
|
Chris@16
|
130 explicit shared_count(shared_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
131 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
|
Chris@16
|
132 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
|
Chris@16
|
133
|
Chris@16
|
134 //this is a test
|
Chris@16
|
135 template<class Y>
|
Chris@16
|
136 explicit shared_count(const pointer & ptr, shared_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
137 : m_px(ptr), m_pi(r.m_pi) // nothrow
|
Chris@16
|
138 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
|
Chris@16
|
139
|
Chris@16
|
140 /*
|
Chris@16
|
141 explicit shared_count(weak_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
142 // throws bad_weak_ptr when r.use_count() == 0
|
Chris@16
|
143 : m_pi( r.m_pi )
|
Chris@16
|
144 {
|
Chris@16
|
145 if( m_pi == 0 || !m_pi->add_ref_lock() ){
|
Chris@16
|
146 boost::throw_exception( boost::interprocess::bad_weak_ptr() );
|
Chris@16
|
147 }
|
Chris@16
|
148 }
|
Chris@16
|
149 */
|
Chris@16
|
150 template<class Y>
|
Chris@16
|
151 explicit shared_count(weak_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
152 // throws bad_weak_ptr when r.use_count() == 0
|
Chris@16
|
153 : m_px(r.m_px), m_pi( r.m_pi )
|
Chris@16
|
154 {
|
Chris@16
|
155 if( m_pi == 0 || !m_pi->add_ref_lock() ){
|
Chris@16
|
156 throw( boost::interprocess::bad_weak_ptr() );
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 const pointer &to_raw_pointer() const
|
Chris@16
|
161 { return m_px; }
|
Chris@16
|
162
|
Chris@16
|
163 pointer &to_raw_pointer()
|
Chris@16
|
164 { return m_px; }
|
Chris@16
|
165
|
Chris@16
|
166 shared_count & operator= (shared_count const & r) // nothrow
|
Chris@16
|
167 {
|
Chris@16
|
168 m_px = r.m_px;
|
Chris@16
|
169 counted_impl_ptr tmp = r.m_pi;
|
Chris@16
|
170 if( tmp != m_pi ){
|
Chris@16
|
171 if(tmp != 0) tmp->add_ref_copy();
|
Chris@16
|
172 if(m_pi != 0) m_pi->release();
|
Chris@16
|
173 m_pi = tmp;
|
Chris@16
|
174 }
|
Chris@16
|
175 return *this;
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 template<class Y>
|
Chris@16
|
179 shared_count & operator= (shared_count<Y, VoidAllocator, Deleter> const & r) // nothrow
|
Chris@16
|
180 {
|
Chris@16
|
181 m_px = r.m_px;
|
Chris@16
|
182 counted_impl_ptr tmp = r.m_pi;
|
Chris@16
|
183 if( tmp != m_pi ){
|
Chris@16
|
184 if(tmp != 0) tmp->add_ref_copy();
|
Chris@16
|
185 if(m_pi != 0) m_pi->release();
|
Chris@16
|
186 m_pi = tmp;
|
Chris@16
|
187 }
|
Chris@16
|
188 return *this;
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 void swap(shared_count & r) // nothrow
|
Chris@16
|
192 { ipcdetail::do_swap(m_px, r.m_px); ipcdetail::do_swap(m_pi, r.m_pi); }
|
Chris@16
|
193
|
Chris@16
|
194 long use_count() const // nothrow
|
Chris@16
|
195 { return m_pi != 0? m_pi->use_count(): 0; }
|
Chris@16
|
196
|
Chris@16
|
197 bool unique() const // nothrow
|
Chris@16
|
198 { return use_count() == 1; }
|
Chris@16
|
199
|
Chris@16
|
200 const_deleter_pointer get_deleter() const
|
Chris@16
|
201 { return m_pi ? m_pi->get_deleter() : 0; }
|
Chris@16
|
202
|
Chris@16
|
203 // const_allocator_pointer get_allocator() const
|
Chris@16
|
204 // { return m_pi ? m_pi->get_allocator() : 0; }
|
Chris@16
|
205
|
Chris@16
|
206 template<class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
207 bool internal_equal (shared_count<T2, VoidAllocator2, Deleter2> const & other) const
|
Chris@16
|
208 { return this->m_pi == other.m_pi; }
|
Chris@16
|
209
|
Chris@16
|
210 template<class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
211 bool internal_less (shared_count<T2, VoidAllocator2, Deleter2> const & other) const
|
Chris@16
|
212 { return std::less<counted_base_ptr>()(this->m_pi, other.m_pi); }
|
Chris@16
|
213 };
|
Chris@16
|
214
|
Chris@16
|
215 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
|
Chris@16
|
216 bool operator==(shared_count<T, VoidAllocator, Deleter> const & a, shared_count<T2, VoidAllocator2, Deleter2> const & b)
|
Chris@16
|
217 { return a.internal_equal(b); }
|
Chris@16
|
218
|
Chris@16
|
219 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
|
Chris@16
|
220 bool operator<(shared_count<T, VoidAllocator, Deleter> const & a, shared_count<T2, VoidAllocator2, Deleter2> const & b)
|
Chris@16
|
221 { return a.internal_less(b); }
|
Chris@16
|
222
|
Chris@16
|
223
|
Chris@16
|
224 template<class T, class VoidAllocator, class Deleter>
|
Chris@16
|
225 class weak_count
|
Chris@16
|
226 {
|
Chris@16
|
227 public:
|
Chris@16
|
228 typedef typename boost::intrusive::
|
Chris@16
|
229 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
230 rebind_pointer<T>::type pointer;
|
Chris@16
|
231
|
Chris@16
|
232 private:
|
Chris@16
|
233
|
Chris@16
|
234 typedef sp_counted_impl_pd<VoidAllocator, Deleter> counted_impl;
|
Chris@16
|
235
|
Chris@16
|
236 typedef typename boost::intrusive::
|
Chris@16
|
237 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
238 rebind_pointer<counted_impl>::type counted_impl_ptr;
|
Chris@16
|
239 typedef typename boost::intrusive::
|
Chris@16
|
240 pointer_traits<typename VoidAllocator::pointer>::template
|
Chris@16
|
241 rebind_pointer<sp_counted_base>::type counted_base_ptr;
|
Chris@16
|
242
|
Chris@16
|
243 pointer m_px;
|
Chris@16
|
244 counted_impl_ptr m_pi;
|
Chris@16
|
245
|
Chris@16
|
246 template <class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
247 friend class weak_count;
|
Chris@16
|
248
|
Chris@16
|
249 template <class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
250 friend class shared_count;
|
Chris@16
|
251
|
Chris@16
|
252 public:
|
Chris@16
|
253
|
Chris@16
|
254 weak_count(): m_px(0), m_pi(0) // nothrow
|
Chris@16
|
255 {}
|
Chris@16
|
256
|
Chris@16
|
257 template <class Y>
|
Chris@16
|
258 explicit weak_count(shared_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
259 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
|
Chris@16
|
260 { if(m_pi != 0) m_pi->weak_add_ref(); }
|
Chris@16
|
261
|
Chris@16
|
262 weak_count(weak_count const & r)
|
Chris@16
|
263 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
|
Chris@16
|
264 { if(m_pi != 0) m_pi->weak_add_ref(); }
|
Chris@16
|
265
|
Chris@16
|
266 template<class Y>
|
Chris@16
|
267 weak_count(weak_count<Y, VoidAllocator, Deleter> const & r)
|
Chris@16
|
268 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
|
Chris@16
|
269 { if(m_pi != 0) m_pi->weak_add_ref(); }
|
Chris@16
|
270
|
Chris@16
|
271 ~weak_count() // nothrow
|
Chris@16
|
272 { if(m_pi != 0) m_pi->weak_release(); }
|
Chris@16
|
273
|
Chris@16
|
274 template<class Y>
|
Chris@16
|
275 weak_count & operator= (shared_count<Y, VoidAllocator, Deleter> const & r) // nothrow
|
Chris@16
|
276 {
|
Chris@16
|
277 m_px = r.m_px;
|
Chris@16
|
278 counted_impl_ptr tmp = r.m_pi;
|
Chris@16
|
279 if(tmp != 0) tmp->weak_add_ref();
|
Chris@16
|
280 if(m_pi != 0) m_pi->weak_release();
|
Chris@16
|
281 m_pi = tmp;
|
Chris@16
|
282 return *this;
|
Chris@16
|
283 }
|
Chris@16
|
284
|
Chris@16
|
285 weak_count & operator= (weak_count const & r) // nothrow
|
Chris@16
|
286 {
|
Chris@16
|
287 m_px = r.m_px;
|
Chris@16
|
288 counted_impl_ptr tmp = r.m_pi;
|
Chris@16
|
289 if(tmp != 0) tmp->weak_add_ref();
|
Chris@16
|
290 if(m_pi != 0) m_pi->weak_release();
|
Chris@16
|
291 m_pi = tmp;
|
Chris@16
|
292 return *this;
|
Chris@16
|
293 }
|
Chris@16
|
294
|
Chris@16
|
295 void set_pointer(const pointer &ptr)
|
Chris@16
|
296 { m_px = ptr; }
|
Chris@16
|
297
|
Chris@16
|
298 template<class Y>
|
Chris@16
|
299 weak_count & operator= (weak_count<Y, VoidAllocator, Deleter> const& r) // nothrow
|
Chris@16
|
300 {
|
Chris@16
|
301 counted_impl_ptr tmp = r.m_pi;
|
Chris@16
|
302 if(tmp != 0) tmp->weak_add_ref();
|
Chris@16
|
303 if(m_pi != 0) m_pi->weak_release();
|
Chris@16
|
304 m_pi = tmp;
|
Chris@16
|
305 return *this;
|
Chris@16
|
306 }
|
Chris@16
|
307
|
Chris@16
|
308 void swap(weak_count & r) // nothrow
|
Chris@16
|
309 { ipcdetail::do_swap(m_px, r.m_px); ipcdetail::do_swap(m_pi, r.m_pi); }
|
Chris@16
|
310
|
Chris@16
|
311 long use_count() const // nothrow
|
Chris@16
|
312 { return m_pi != 0? m_pi->use_count() : 0; }
|
Chris@16
|
313
|
Chris@16
|
314 template<class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
315 bool internal_equal (weak_count<T2, VoidAllocator2, Deleter2> const & other) const
|
Chris@16
|
316 { return this->m_pi == other.m_pi; }
|
Chris@16
|
317
|
Chris@16
|
318 template<class T2, class VoidAllocator2, class Deleter2>
|
Chris@16
|
319 bool internal_less (weak_count<T2, VoidAllocator2, Deleter2> const & other) const
|
Chris@16
|
320 { return std::less<counted_base_ptr>()(this->m_pi, other.m_pi); }
|
Chris@16
|
321 };
|
Chris@16
|
322
|
Chris@16
|
323 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
|
Chris@16
|
324 bool operator==(weak_count<T, VoidAllocator, Deleter> const & a, weak_count<T2, VoidAllocator2, Deleter2> const & b)
|
Chris@16
|
325 { return a.internal_equal(b); }
|
Chris@16
|
326
|
Chris@16
|
327 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
|
Chris@16
|
328 bool operator<(weak_count<T, VoidAllocator, Deleter> const & a, weak_count<T2, VoidAllocator2, Deleter2> const & b)
|
Chris@16
|
329 { return a.internal_less(b); }
|
Chris@16
|
330
|
Chris@16
|
331 } // namespace ipcdetail
|
Chris@16
|
332 } // namespace interprocess
|
Chris@16
|
333 } // namespace boost
|
Chris@16
|
334
|
Chris@16
|
335
|
Chris@16
|
336 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
337
|
Chris@16
|
338
|
Chris@16
|
339 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED
|