fix doc example typo
[boost.git] / boost / interprocess / smart_ptr / detail / sp_counted_impl.hpp
blob9d799fae0dea06abe893fe7033532085173a60ab
1 #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
11 // This file is the adaptation for shared memory memory mapped
12 // files of boost/detail/sp_counted_impl.hpp
14 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
15 // Copyright 2004-2005 Peter Dimov
16 // Copyright 2006 Ion Gaztanaga
18 // Distributed under the Boost Software License, Version 1.0. (See
19 // accompanying file LICENSE_1_0.txt or copy at
20 // http://www.boost.org/LICENSE_1_0.txt)
23 #include <boost/interprocess/detail/config_begin.hpp>
24 #include <boost/interprocess/detail/workaround.hpp>
26 #include <boost/interprocess/smart_ptr/detail/sp_counted_base.hpp>
27 #include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
28 #include <boost/interprocess/detail/utilities.hpp>
29 #include <boost/pointer_to_other.hpp>
31 namespace boost {
33 namespace interprocess {
35 namespace detail {
37 //!A deleter for scoped_ptr that deallocates the memory
38 //!allocated for an object using a STL allocator.
39 template <class Allocator>
40 struct scoped_ptr_dealloc_functor
42 typedef typename Allocator::pointer pointer;
43 typedef detail::integral_constant<unsigned,
44 boost::interprocess::version<Allocator>::value> alloc_version;
45 typedef detail::integral_constant<unsigned, 1> allocator_v1;
46 typedef detail::integral_constant<unsigned, 2> allocator_v2;
48 private:
49 void priv_deallocate(const typename Allocator::pointer &p, allocator_v1)
50 { m_alloc.deallocate(p, 1); }
52 void priv_deallocate(const typename Allocator::pointer &p, allocator_v2)
53 { m_alloc.deallocate_one(p); }
55 public:
56 Allocator& m_alloc;
58 scoped_ptr_dealloc_functor(Allocator& a)
59 : m_alloc(a) {}
61 void operator()(pointer ptr)
62 { if (ptr) priv_deallocate(ptr, alloc_version()); }
65 template<class A, class D>
66 class sp_counted_impl_pd
67 : public sp_counted_base
68 , A::template rebind< sp_counted_impl_pd<A, D> >::other
69 , D // copy constructor must not throw
71 private:
72 typedef sp_counted_impl_pd<A, D> this_type;
73 typedef typename A::template rebind
74 <this_type>::other this_allocator;
75 typedef typename A::template rebind
76 <const this_type>::other const_this_allocator;
77 typedef typename this_allocator::pointer this_pointer;
79 sp_counted_impl_pd( sp_counted_impl_pd const & );
80 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
82 typedef typename boost::pointer_to_other
83 <typename A::pointer, const D>::type const_deleter_pointer;
85 typedef typename boost::pointer_to_other
86 <typename A::pointer, const A>::type const_allocator_pointer;
88 typedef typename D::pointer pointer;
89 pointer m_ptr;
91 public:
92 // pre: d(p) must not throw
93 template<class Ptr>
94 sp_counted_impl_pd(const Ptr & p, const A &a, const D &d )
95 : this_allocator(a), D(d), m_ptr(p)
98 const_deleter_pointer get_deleter() const
99 { return const_deleter_pointer(&static_cast<const D&>(*this)); }
101 const_allocator_pointer get_allocator() const
102 { return const_allocator_pointer(&static_cast<const A&>(*this)); }
104 void dispose() // nothrow
105 { static_cast<D&>(*this)(m_ptr); }
107 void destroy() // nothrow
109 //Self destruction, so get a copy of the allocator
110 //(in the future we could move it)
111 this_allocator a_copy(*this);
112 BOOST_ASSERT(a_copy == *this);
113 this_pointer this_ptr (this);
114 //Do it now!
115 scoped_ptr< this_type, scoped_ptr_dealloc_functor<this_allocator> >
116 deleter(this_ptr, a_copy);
117 typedef typename this_allocator::value_type value_type;
118 detail::get_pointer(this_ptr)->~value_type();
121 void release() // nothrow
123 if(this->ref_release()){
124 this->dispose();
125 this->weak_release();
129 void weak_release() // nothrow
131 if(sp_counted_base::weak_release()){
132 this->destroy();
138 } // namespace detail
140 } // namespace interprocess
142 } // namespace boost
144 #include <boost/interprocess/detail/config_end.hpp>
146 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED