1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
3 
4 // MS compatible compilers support #pragma once
5 
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9 
10 //
11 //  detail/sp_counted_impl.hpp
12 //
13 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 //  Copyright 2004-2005 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20 
21 #include <boost/config.hpp>
22 
23 #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
24 # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
25 #endif
26 
27 #include <boost/checked_delete.hpp>
28 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
29 #include <boost/core/addressof.hpp>
30 
31 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
32 #include <boost/smart_ptr/detail/quick_allocator.hpp>
33 #endif
34 
35 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
36 #include <memory>           // std::allocator
37 #endif
38 
39 #include <cstddef>          // std::size_t
40 
41 namespace boost
42 {
43 
44 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
45 
46 void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
47 void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
48 
49 #endif
50 
51 namespace detail
52 {
53 
54 // get_local_deleter
55 
56 template<class D> class local_sp_deleter;
57 
get_local_deleter(D *)58 template<class D> D * get_local_deleter( D * /*p*/ )
59 {
60     return 0;
61 }
62 
63 template<class D> D * get_local_deleter( local_sp_deleter<D> * p );
64 
65 //
66 
67 template<class X> class sp_counted_impl_p: public sp_counted_base
68 {
69 private:
70 
71     X * px_;
72 
73     sp_counted_impl_p( sp_counted_impl_p const & );
74     sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
75 
76     typedef sp_counted_impl_p<X> this_type;
77 
78 public:
79 
sp_counted_impl_p(X * px)80     explicit sp_counted_impl_p( X * px ): px_( px )
81     {
82 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
83         boost::sp_scalar_constructor_hook( px, sizeof(X), this );
84 #endif
85     }
86 
dispose()87     virtual void dispose() // nothrow
88     {
89 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
90         boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
91 #endif
92         boost::checked_delete( px_ );
93     }
94 
get_deleter(sp_typeinfo const &)95     virtual void * get_deleter( sp_typeinfo const & )
96     {
97         return 0;
98     }
99 
get_local_deleter(sp_typeinfo const &)100     virtual void * get_local_deleter( sp_typeinfo const & )
101     {
102         return 0;
103     }
104 
get_untyped_deleter()105     virtual void * get_untyped_deleter()
106     {
107         return 0;
108     }
109 
110 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
111 
operator new(std::size_t)112     void * operator new( std::size_t )
113     {
114         return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
115     }
116 
operator delete(void * p)117     void operator delete( void * p )
118     {
119         std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
120     }
121 
122 #endif
123 
124 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
125 
operator new(std::size_t)126     void * operator new( std::size_t )
127     {
128         return quick_allocator<this_type>::alloc();
129     }
130 
operator delete(void * p)131     void operator delete( void * p )
132     {
133         quick_allocator<this_type>::dealloc( p );
134     }
135 
136 #endif
137 };
138 
139 //
140 // Borland's Codeguard trips up over the -Vx- option here:
141 //
142 #ifdef __CODEGUARD__
143 # pragma option push -Vx-
144 #endif
145 
146 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
147 {
148 private:
149 
150     P ptr; // copy constructor must not throw
151     D del; // copy constructor must not throw
152 
153     sp_counted_impl_pd( sp_counted_impl_pd const & );
154     sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
155 
156     typedef sp_counted_impl_pd<P, D> this_type;
157 
158 public:
159 
160     // pre: d(p) must not throw
161 
sp_counted_impl_pd(P p,D & d)162     sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
163     {
164     }
165 
sp_counted_impl_pd(P p)166     sp_counted_impl_pd( P p ): ptr( p ), del()
167     {
168     }
169 
dispose()170     virtual void dispose() // nothrow
171     {
172         del( ptr );
173     }
174 
get_deleter(sp_typeinfo const & ti)175     virtual void * get_deleter( sp_typeinfo const & ti )
176     {
177         return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
178     }
179 
get_local_deleter(sp_typeinfo const & ti)180     virtual void * get_local_deleter( sp_typeinfo const & ti )
181     {
182         return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
183     }
184 
get_untyped_deleter()185     virtual void * get_untyped_deleter()
186     {
187         return &reinterpret_cast<char&>( del );
188     }
189 
190 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
191 
operator new(std::size_t)192     void * operator new( std::size_t )
193     {
194         return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
195     }
196 
operator delete(void * p)197     void operator delete( void * p )
198     {
199         std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
200     }
201 
202 #endif
203 
204 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
205 
operator new(std::size_t)206     void * operator new( std::size_t )
207     {
208         return quick_allocator<this_type>::alloc();
209     }
210 
operator delete(void * p)211     void operator delete( void * p )
212     {
213         quick_allocator<this_type>::dealloc( p );
214     }
215 
216 #endif
217 };
218 
219 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
220 {
221 private:
222 
223     P p_; // copy constructor must not throw
224     D d_; // copy constructor must not throw
225     A a_; // copy constructor must not throw
226 
227     sp_counted_impl_pda( sp_counted_impl_pda const & );
228     sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
229 
230     typedef sp_counted_impl_pda<P, D, A> this_type;
231 
232 public:
233 
234     // pre: d( p ) must not throw
235 
sp_counted_impl_pda(P p,D & d,A a)236     sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
237     {
238     }
239 
sp_counted_impl_pda(P p,A a)240     sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
241     {
242     }
243 
dispose()244     virtual void dispose() // nothrow
245     {
246         d_( p_ );
247     }
248 
destroy()249     virtual void destroy() // nothrow
250     {
251 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
252 
253         typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
254 
255 #else
256 
257         typedef typename A::template rebind< this_type >::other A2;
258 
259 #endif
260 
261         A2 a2( a_ );
262 
263         this->~this_type();
264 
265         a2.deallocate( this, 1 );
266     }
267 
get_deleter(sp_typeinfo const & ti)268     virtual void * get_deleter( sp_typeinfo const & ti )
269     {
270         return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
271     }
272 
get_local_deleter(sp_typeinfo const & ti)273     virtual void * get_local_deleter( sp_typeinfo const & ti )
274     {
275         return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
276     }
277 
get_untyped_deleter()278     virtual void * get_untyped_deleter()
279     {
280         return &reinterpret_cast<char&>( d_ );
281     }
282 };
283 
284 #ifdef __CODEGUARD__
285 # pragma option pop
286 #endif
287 
288 } // namespace detail
289 
290 } // namespace boost
291 
292 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
293