1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_impl.hpp
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
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)
21 #include <boost/config.hpp>
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.
27 #include <boost/checked_delete.hpp>
28 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
29 #include <boost/core/addressof.hpp>
31 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
32 #include <boost/smart_ptr/detail/quick_allocator.hpp>
35 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
36 #include <memory> // std::allocator
39 #include <cstddef> // std::size_t
44 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
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 );
56 template<class D> class local_sp_deleter;
58 template<class D> D * get_local_deleter( D * /*p*/ )
63 template<class D> D * get_local_deleter( local_sp_deleter<D> * p );
67 template<class X> class sp_counted_impl_p: public sp_counted_base
73 sp_counted_impl_p( sp_counted_impl_p const & );
74 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
76 typedef sp_counted_impl_p<X> this_type;
80 explicit sp_counted_impl_p( X * px ): px_( px )
82 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
83 boost::sp_scalar_constructor_hook( px, sizeof(X), this );
87 virtual void dispose() // nothrow
89 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
90 boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
92 boost::checked_delete( px_ );
95 virtual void * get_deleter( sp_typeinfo const & )
100 virtual void * get_local_deleter( sp_typeinfo const & )
105 virtual void * get_untyped_deleter()
110 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
112 void * operator new( std::size_t )
114 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
117 void operator delete( void * p )
119 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
124 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
126 void * operator new( std::size_t )
128 return quick_allocator<this_type>::alloc();
131 void operator delete( void * p )
133 quick_allocator<this_type>::dealloc( p );
140 // Borland's Codeguard trips up over the -Vx- option here:
143 # pragma option push -Vx-
146 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
150 P ptr; // copy constructor must not throw
151 D del; // copy constructor must not throw
153 sp_counted_impl_pd( sp_counted_impl_pd const & );
154 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
156 typedef sp_counted_impl_pd<P, D> this_type;
160 // pre: d(p) must not throw
162 sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
166 sp_counted_impl_pd( P p ): ptr( p ), del()
170 virtual void dispose() // nothrow
175 virtual void * get_deleter( sp_typeinfo const & ti )
177 return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
180 virtual void * get_local_deleter( sp_typeinfo const & ti )
182 return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
185 virtual void * get_untyped_deleter()
187 return &reinterpret_cast<char&>( del );
190 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
192 void * operator new( std::size_t )
194 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
197 void operator delete( void * p )
199 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
204 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
206 void * operator new( std::size_t )
208 return quick_allocator<this_type>::alloc();
211 void operator delete( void * p )
213 quick_allocator<this_type>::dealloc( p );
219 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
223 P p_; // copy constructor must not throw
224 D d_; // copy constructor must not throw
225 A a_; // copy constructor must not throw
227 sp_counted_impl_pda( sp_counted_impl_pda const & );
228 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
230 typedef sp_counted_impl_pda<P, D, A> this_type;
234 // pre: d( p ) must not throw
236 sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
240 sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
244 virtual void dispose() // nothrow
249 virtual void destroy() // nothrow
251 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
253 typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
257 typedef typename A::template rebind< this_type >::other A2;
265 a2.deallocate( this, 1 );
268 virtual void * get_deleter( sp_typeinfo const & ti )
270 return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
273 virtual void * get_local_deleter( sp_typeinfo const & ti )
275 return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
278 virtual void * get_untyped_deleter()
280 return &reinterpret_cast<char&>( d_ );
288 } // namespace detail
292 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED