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/smart_ptr/detail/sp_noexcept.hpp>
30 #include <boost/core/addressof.hpp>
32 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
33 #include <boost/smart_ptr/detail/quick_allocator.hpp>
36 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
37 #include <memory> // std::allocator
40 #include <cstddef> // std::size_t
45 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
47 void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
48 void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
57 template<class D> class local_sp_deleter;
59 template<class D> D * get_local_deleter( D * /*p*/ ) BOOST_SP_NOEXCEPT
64 template<class D> D * get_local_deleter( local_sp_deleter<D> * p ) BOOST_SP_NOEXCEPT;
68 template<class X> class BOOST_SYMBOL_VISIBLE sp_counted_impl_p: public sp_counted_base
74 sp_counted_impl_p( sp_counted_impl_p const & );
75 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
77 typedef sp_counted_impl_p<X> this_type;
81 explicit sp_counted_impl_p( X * px ): px_( px )
83 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
84 boost::sp_scalar_constructor_hook( px, sizeof(X), this );
88 virtual void dispose() BOOST_SP_NOEXCEPT
90 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
91 boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
93 boost::checked_delete( px_ );
96 virtual void * get_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT
101 virtual void * get_local_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT
106 virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT
111 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
113 void * operator new( std::size_t )
115 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
118 void operator delete( void * p )
120 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
125 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
127 void * operator new( std::size_t )
129 return quick_allocator<this_type>::alloc();
132 void operator delete( void * p )
134 quick_allocator<this_type>::dealloc( p );
141 // Borland's Codeguard trips up over the -Vx- option here:
144 # pragma option push -Vx-
147 template<class P, class D> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pd: public sp_counted_base
151 P ptr; // copy constructor must not throw
152 D del; // copy constructor must not throw
154 sp_counted_impl_pd( sp_counted_impl_pd const & );
155 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
157 typedef sp_counted_impl_pd<P, D> this_type;
161 // pre: d(p) must not throw
163 sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
167 sp_counted_impl_pd( P p ): ptr( p ), del()
171 virtual void dispose() BOOST_SP_NOEXCEPT
176 virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT
178 return ti == BOOST_SP_TYPEID_(D)? &reinterpret_cast<char&>( del ): 0;
181 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT
183 return ti == BOOST_SP_TYPEID_(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
186 virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT
188 return &reinterpret_cast<char&>( del );
191 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
193 void * operator new( std::size_t )
195 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
198 void operator delete( void * p )
200 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
205 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
207 void * operator new( std::size_t )
209 return quick_allocator<this_type>::alloc();
212 void operator delete( void * p )
214 quick_allocator<this_type>::dealloc( p );
220 template<class P, class D, class A> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pda: public sp_counted_base
224 P p_; // copy constructor must not throw
225 D d_; // copy constructor must not throw
226 A a_; // copy constructor must not throw
228 sp_counted_impl_pda( sp_counted_impl_pda const & );
229 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
231 typedef sp_counted_impl_pda<P, D, A> this_type;
235 // pre: d( p ) must not throw
237 sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
241 sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
245 virtual void dispose() BOOST_SP_NOEXCEPT
250 virtual void destroy() BOOST_SP_NOEXCEPT
252 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
254 typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
258 typedef typename A::template rebind< this_type >::other A2;
266 a2.deallocate( this, 1 );
269 virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT
271 return ti == BOOST_SP_TYPEID_( D )? &reinterpret_cast<char&>( d_ ): 0;
274 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT
276 return ti == BOOST_SP_TYPEID_( D )? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
279 virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT
281 return &reinterpret_cast<char&>( d_ );
289 } // namespace detail
293 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED