1 //////////////////////////////////////////////////////////////////////////////
3 // This file is the adaptation for Interprocess of boost/detail/shared_count.hpp
5 // (C) Copyright Peter Dimov and Multi Media Ltd. 2001, 2002, 2003
6 // (C) Copyright Peter Dimov 2004-2005
7 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
8 // Software License, Version 1.0. (See accompanying file
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // See http://www.boost.org/libs/interprocess for documentation.
13 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED
15 #define BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED
17 #ifndef BOOST_CONFIG_HPP
18 # include <boost/config.hpp>
21 #if defined(BOOST_HAS_PRAGMA_ONCE)
25 #include <boost/interprocess/detail/config_begin.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
28 #include <boost/intrusive/pointer_traits.hpp>
29 #include <boost/interprocess/smart_ptr/detail/bad_weak_ptr.hpp>
30 #include <boost/interprocess/smart_ptr/detail/sp_counted_impl.hpp>
31 #include <boost/interprocess/detail/utilities.hpp>
32 #include <boost/container/allocator_traits.hpp>
33 #include <boost/core/no_exceptions_support.hpp>
34 #include <boost/move/adl_move_swap.hpp>
35 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
36 #include <boost/container/detail/placement_new.hpp>
39 namespace interprocess {
42 template<class T, class VoidAllocator, class Deleter>
45 template<class T, class VoidAllocator, class Deleter>
50 typedef typename boost::container::
51 allocator_traits<VoidAllocator>::pointer void_ptr;
52 typedef typename boost::intrusive::
53 pointer_traits<void_ptr>::template
54 rebind_pointer<T>::type pointer;
57 typedef sp_counted_impl_pd<VoidAllocator, Deleter> counted_impl;
59 typedef typename boost::intrusive::
60 pointer_traits<void_ptr>::template
61 rebind_pointer<counted_impl>::type counted_impl_ptr;
62 typedef typename boost::intrusive::
63 pointer_traits<void_ptr>::template
64 rebind_pointer<sp_counted_base>::type counted_base_ptr;
66 typedef boost::container::allocator_traits<VoidAllocator> vallocator_traits;
68 typedef typename vallocator_traits::template
69 portable_rebind_alloc<counted_impl>::type counted_impl_allocator;
71 typedef typename boost::intrusive::
72 pointer_traits<void_ptr>::template
73 rebind_pointer<const Deleter>::type const_deleter_pointer;
75 typedef typename boost::intrusive::
76 pointer_traits<void_ptr>::template
77 rebind_pointer<const VoidAllocator>::type const_allocator_pointer;
80 counted_impl_ptr m_pi;
82 template <class T2, class VoidAllocator2, class Deleter2>
83 friend class weak_count;
85 template <class T2, class VoidAllocator2, class Deleter2>
86 friend class shared_count;
91 : m_px(0), m_pi(0) // nothrow
95 shared_count(const shared_count &other_shared_count, const Ptr &p)
96 : m_px(p), m_pi(other_shared_count.m_pi)
100 shared_count(const Ptr &p, const VoidAllocator &a, Deleter d)
105 counted_impl_allocator alloc(a);
106 m_pi = alloc.allocate(1);
107 //Anti-exception deallocator
108 scoped_ptr<counted_impl,
109 scoped_ptr_dealloc_functor<counted_impl_allocator> >
110 deallocator(m_pi, alloc);
111 //It's more correct to use VoidAllocator::construct but
112 //this needs copy constructor and we don't like it
113 ::new(ipcdetail::to_raw_pointer(m_pi), boost_container_new_t())counted_impl(p, a, d);
114 deallocator.release();
124 ~shared_count() // nothrow
130 shared_count(shared_count const & r)
131 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
132 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
136 explicit shared_count(shared_count<Y, VoidAllocator, Deleter> const & r)
137 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
138 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
142 explicit shared_count(const pointer & ptr, shared_count<Y, VoidAllocator, Deleter> const & r)
143 : m_px(ptr), m_pi(r.m_pi) // nothrow
144 { if( m_pi != 0 ) m_pi->add_ref_copy(); }
147 explicit shared_count(weak_count<Y, VoidAllocator, Deleter> const & r)
148 // throws bad_weak_ptr when r.use_count() == 0
151 if( m_pi == 0 || !m_pi->add_ref_lock() ){
152 boost::throw_exception( boost::interprocess::bad_weak_ptr() );
157 explicit shared_count(weak_count<Y, VoidAllocator, Deleter> const & r)
158 // throws bad_weak_ptr when r.use_count() == 0
159 : m_px(r.m_px), m_pi( r.m_pi )
161 if( m_pi == 0 || !m_pi->add_ref_lock() ){
162 throw( boost::interprocess::bad_weak_ptr() );
166 const pointer &to_raw_pointer() const
169 pointer &to_raw_pointer()
172 shared_count & operator= (shared_count const & r) // nothrow
175 counted_impl_ptr tmp = r.m_pi;
177 if(tmp != 0) tmp->add_ref_copy();
178 if(m_pi != 0) m_pi->release();
185 shared_count & operator= (shared_count<Y, VoidAllocator, Deleter> const & r) // nothrow
188 counted_impl_ptr tmp = r.m_pi;
190 if(tmp != 0) tmp->add_ref_copy();
191 if(m_pi != 0) m_pi->release();
197 void swap(shared_count & r) // nothrow
198 { ::boost::adl_move_swap(m_px, r.m_px); ::boost::adl_move_swap(m_pi, r.m_pi); }
200 long use_count() const // nothrow
201 { return m_pi != 0? m_pi->use_count(): 0; }
203 bool unique() const // nothrow
204 { return use_count() == 1; }
206 const_deleter_pointer get_deleter() const
207 { return m_pi ? m_pi->get_deleter() : 0; }
209 // const_allocator_pointer get_allocator() const
210 // { return m_pi ? m_pi->get_allocator() : 0; }
212 template<class T2, class VoidAllocator2, class Deleter2>
213 bool internal_equal (shared_count<T2, VoidAllocator2, Deleter2> const & other) const
214 { return this->m_pi == other.m_pi; }
216 template<class T2, class VoidAllocator2, class Deleter2>
217 bool internal_less (shared_count<T2, VoidAllocator2, Deleter2> const & other) const
218 { return std::less<counted_base_ptr>()(this->m_pi, other.m_pi); }
221 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
222 bool operator==(shared_count<T, VoidAllocator, Deleter> const & a, shared_count<T2, VoidAllocator2, Deleter2> const & b)
223 { return a.internal_equal(b); }
225 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
226 bool operator<(shared_count<T, VoidAllocator, Deleter> const & a, shared_count<T2, VoidAllocator2, Deleter2> const & b)
227 { return a.internal_less(b); }
230 template<class T, class VoidAllocator, class Deleter>
234 typedef typename boost::container::
235 allocator_traits<VoidAllocator>::pointer void_ptr;
236 typedef typename boost::intrusive::
237 pointer_traits<void_ptr>::template
238 rebind_pointer<T>::type pointer;
242 typedef sp_counted_impl_pd<VoidAllocator, Deleter> counted_impl;
244 typedef typename boost::intrusive::
245 pointer_traits<void_ptr>::template
246 rebind_pointer<counted_impl>::type counted_impl_ptr;
247 typedef typename boost::intrusive::
248 pointer_traits<void_ptr>::template
249 rebind_pointer<sp_counted_base>::type counted_base_ptr;
252 counted_impl_ptr m_pi;
254 template <class T2, class VoidAllocator2, class Deleter2>
255 friend class weak_count;
257 template <class T2, class VoidAllocator2, class Deleter2>
258 friend class shared_count;
262 weak_count(): m_px(0), m_pi(0) // nothrow
266 explicit weak_count(shared_count<Y, VoidAllocator, Deleter> const & r)
267 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
268 { if(m_pi != 0) m_pi->weak_add_ref(); }
270 weak_count(weak_count const & r)
271 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
272 { if(m_pi != 0) m_pi->weak_add_ref(); }
275 weak_count(weak_count<Y, VoidAllocator, Deleter> const & r)
276 : m_px(r.m_px), m_pi(r.m_pi) // nothrow
277 { if(m_pi != 0) m_pi->weak_add_ref(); }
279 ~weak_count() // nothrow
280 { if(m_pi != 0) m_pi->weak_release(); }
283 weak_count & operator= (shared_count<Y, VoidAllocator, Deleter> const & r) // nothrow
286 counted_impl_ptr tmp = r.m_pi;
287 if(tmp != 0) tmp->weak_add_ref();
288 if(m_pi != 0) m_pi->weak_release();
293 weak_count & operator= (weak_count const & r) // nothrow
296 counted_impl_ptr tmp = r.m_pi;
297 if(tmp != 0) tmp->weak_add_ref();
298 if(m_pi != 0) m_pi->weak_release();
303 void set_pointer(const pointer &ptr)
307 weak_count & operator= (weak_count<Y, VoidAllocator, Deleter> const& r) // nothrow
309 counted_impl_ptr tmp = r.m_pi;
310 if(tmp != 0) tmp->weak_add_ref();
311 if(m_pi != 0) m_pi->weak_release();
316 void swap(weak_count & r) // nothrow
317 { ::boost::adl_move_swap(m_px, r.m_px); ::boost::adl_move_swap(m_pi, r.m_pi); }
319 long use_count() const // nothrow
320 { return m_pi != 0? m_pi->use_count() : 0; }
322 template<class T2, class VoidAllocator2, class Deleter2>
323 bool internal_equal (weak_count<T2, VoidAllocator2, Deleter2> const & other) const
324 { return this->m_pi == other.m_pi; }
326 template<class T2, class VoidAllocator2, class Deleter2>
327 bool internal_less (weak_count<T2, VoidAllocator2, Deleter2> const & other) const
328 { return std::less<counted_base_ptr>()(this->m_pi, other.m_pi); }
331 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
332 bool operator==(weak_count<T, VoidAllocator, Deleter> const & a, weak_count<T2, VoidAllocator2, Deleter2> const & b)
333 { return a.internal_equal(b); }
335 template<class T, class VoidAllocator, class Deleter, class T2, class VoidAllocator2, class Deleter2> inline
336 bool operator<(weak_count<T, VoidAllocator, Deleter> const & a, weak_count<T2, VoidAllocator2, Deleter2> const & b)
337 { return a.internal_less(b); }
339 } // namespace ipcdetail
340 } // namespace interprocess
344 #include <boost/interprocess/detail/config_end.hpp>
347 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_COUNT_HPP_INCLUDED