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>
30 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
31 #include <boost/smart_ptr/detail/quick_allocator.hpp>
34 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
35 #include <memory> // std::allocator
38 #include <cstddef> // std::size_t
43 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
45 void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
46 void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
53 template<class X> class sp_counted_impl_p: public sp_counted_base
59 sp_counted_impl_p( sp_counted_impl_p const & );
60 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
62 typedef sp_counted_impl_p<X> this_type;
66 explicit sp_counted_impl_p( X * px ): px_( px )
68 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
69 boost::sp_scalar_constructor_hook( px, sizeof(X), this );
73 virtual void dispose() // nothrow
75 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
76 boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
78 boost::checked_delete( px_ );
81 virtual void * get_deleter( detail::sp_typeinfo const & )
86 virtual void * get_untyped_deleter()
91 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
93 void * operator new( std::size_t )
95 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
98 void operator delete( void * p )
100 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
105 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
107 void * operator new( std::size_t )
109 return quick_allocator<this_type>::alloc();
112 void operator delete( void * p )
114 quick_allocator<this_type>::dealloc( p );
121 // Borland's Codeguard trips up over the -Vx- option here:
124 # pragma option push -Vx-
127 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
131 P ptr; // copy constructor must not throw
132 D del; // copy constructor must not throw
134 sp_counted_impl_pd( sp_counted_impl_pd const & );
135 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
137 typedef sp_counted_impl_pd<P, D> this_type;
141 // pre: d(p) must not throw
143 sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
147 sp_counted_impl_pd( P p ): ptr( p ), del()
151 virtual void dispose() // nothrow
156 virtual void * get_deleter( detail::sp_typeinfo const & ti )
158 return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
161 virtual void * get_untyped_deleter()
163 return &reinterpret_cast<char&>( del );
166 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
168 void * operator new( std::size_t )
170 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
173 void operator delete( void * p )
175 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
180 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
182 void * operator new( std::size_t )
184 return quick_allocator<this_type>::alloc();
187 void operator delete( void * p )
189 quick_allocator<this_type>::dealloc( p );
195 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
199 P p_; // copy constructor must not throw
200 D d_; // copy constructor must not throw
201 A a_; // copy constructor must not throw
203 sp_counted_impl_pda( sp_counted_impl_pda const & );
204 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
206 typedef sp_counted_impl_pda<P, D, A> this_type;
210 // pre: d( p ) must not throw
212 sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
216 sp_counted_impl_pda( P p, A a ): p_( p ), d_(), a_( a )
220 virtual void dispose() // nothrow
225 virtual void destroy() // nothrow
227 typedef typename A::template rebind< this_type >::other A2;
232 a2.deallocate( this, 1 );
235 virtual void * get_deleter( detail::sp_typeinfo const & ti )
237 return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
240 virtual void * get_untyped_deleter()
242 return &reinterpret_cast<char&>( d_ );
250 } // namespace detail
254 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED