1 #ifndef BOOST_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOST_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/detail/sp_counted_base.hpp>
30 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
31 #include <boost/detail/quick_allocator.hpp>
34 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
35 #include <memory> // std::allocator
38 #include <typeinfo> // std::type_info in get_deleter
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 );
54 template<class X> class sp_counted_impl_p: public sp_counted_base
60 sp_counted_impl_p( sp_counted_impl_p const & );
61 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
63 typedef sp_counted_impl_p<X> this_type;
67 explicit sp_counted_impl_p( X * px ): px_( px )
69 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
70 boost::sp_scalar_constructor_hook( px, sizeof(X), this );
74 virtual void dispose() // nothrow
76 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
77 boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
79 boost::checked_delete( px_ );
82 virtual void * get_deleter( std::type_info const & )
87 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
89 void * operator new( std::size_t )
91 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
94 void operator delete( void * p )
96 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
101 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
103 void * operator new( std::size_t )
105 return quick_allocator<this_type>::alloc();
108 void operator delete( void * p )
110 quick_allocator<this_type>::dealloc( p );
117 // Borland's Codeguard trips up over the -Vx- option here:
120 # pragma option push -Vx-
123 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
127 P ptr; // copy constructor must not throw
128 D del; // copy constructor must not throw
130 sp_counted_impl_pd( sp_counted_impl_pd const & );
131 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
133 typedef sp_counted_impl_pd<P, D> this_type;
137 // pre: d(p) must not throw
139 sp_counted_impl_pd( P p, D d ): ptr(p), del(d)
143 virtual void dispose() // nothrow
148 virtual void * get_deleter( std::type_info const & ti )
150 return ti == typeid(D)? &del: 0;
153 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
155 void * operator new( std::size_t )
157 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
160 void operator delete( void * p )
162 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
167 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
169 void * operator new( std::size_t )
171 return quick_allocator<this_type>::alloc();
174 void operator delete( void * p )
176 quick_allocator<this_type>::dealloc( p );
182 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
186 P p_; // copy constructor must not throw
187 D d_; // copy constructor must not throw
188 A a_; // copy constructor must not throw
190 sp_counted_impl_pda( sp_counted_impl_pda const & );
191 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
193 typedef sp_counted_impl_pda<P, D, A> this_type;
197 // pre: d( p ) must not throw
199 sp_counted_impl_pda( P p, D d, A a ): p_( p ), d_( d ), a_( a )
203 virtual void dispose() // nothrow
208 virtual void destroy() // nothrow
210 typedef typename A::template rebind< this_type >::other A2;
215 a2.deallocate( this, 1 );
218 virtual void * get_deleter( std::type_info const & ti )
220 return ti == typeid( D )? &d_: 0;
228 } // namespace detail
232 #endif // #ifndef BOOST_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED