1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
\r
4 // MS compatible compilers support #pragma once
\r
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
\r
11 // detail/sp_counted_impl.hpp
\r
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
\r
14 // Copyright 2004-2005 Peter Dimov
\r
16 // Distributed under the Boost Software License, Version 1.0. (See
\r
17 // accompanying file LICENSE_1_0.txt or copy at
\r
18 // http://www.boost.org/LICENSE_1_0.txt)
\r
21 #include <boost/config.hpp>
\r
23 #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
\r
24 # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
\r
27 #include <boost/checked_delete.hpp>
\r
28 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
\r
30 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
\r
31 #include <boost/smart_ptr/detail/quick_allocator.hpp>
\r
34 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
\r
35 #include <memory> // std::allocator
\r
38 #include <cstddef> // std::size_t
\r
43 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
45 void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
\r
46 void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
\r
53 template<class X> class sp_counted_impl_p: public sp_counted_base
\r
59 sp_counted_impl_p( sp_counted_impl_p const & );
\r
60 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
\r
62 typedef sp_counted_impl_p<X> this_type;
\r
66 explicit sp_counted_impl_p( X * px ): px_( px )
\r
68 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
69 boost::sp_scalar_constructor_hook( px, sizeof(X), this );
\r
73 virtual void dispose() // nothrow
\r
75 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
76 boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
\r
78 boost::checked_delete( px_ );
\r
81 virtual void * get_deleter( detail::sp_typeinfo const & )
\r
86 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
\r
88 void * operator new( std::size_t )
\r
90 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
\r
93 void operator delete( void * p )
\r
95 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
\r
100 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
\r
102 void * operator new( std::size_t )
\r
104 return quick_allocator<this_type>::alloc();
\r
107 void operator delete( void * p )
\r
109 quick_allocator<this_type>::dealloc( p );
\r
116 // Borland's Codeguard trips up over the -Vx- option here:
\r
118 #ifdef __CODEGUARD__
\r
119 # pragma option push -Vx-
\r
122 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
\r
126 P ptr; // copy constructor must not throw
\r
127 D del; // copy constructor must not throw
\r
129 sp_counted_impl_pd( sp_counted_impl_pd const & );
\r
130 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
\r
132 typedef sp_counted_impl_pd<P, D> this_type;
\r
136 // pre: d(p) must not throw
\r
138 sp_counted_impl_pd( P p, D d ): ptr(p), del(d)
\r
142 virtual void dispose() // nothrow
\r
147 virtual void * get_deleter( detail::sp_typeinfo const & ti )
\r
149 return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
\r
152 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
\r
154 void * operator new( std::size_t )
\r
156 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
\r
159 void operator delete( void * p )
\r
161 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
\r
166 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
\r
168 void * operator new( std::size_t )
\r
170 return quick_allocator<this_type>::alloc();
\r
173 void operator delete( void * p )
\r
175 quick_allocator<this_type>::dealloc( p );
\r
181 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
\r
185 P p_; // copy constructor must not throw
\r
186 D d_; // copy constructor must not throw
\r
187 A a_; // copy constructor must not throw
\r
189 sp_counted_impl_pda( sp_counted_impl_pda const & );
\r
190 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
\r
192 typedef sp_counted_impl_pda<P, D, A> this_type;
\r
196 // pre: d( p ) must not throw
\r
198 sp_counted_impl_pda( P p, D d, A a ): p_( p ), d_( d ), a_( a )
\r
202 virtual void dispose() // nothrow
\r
207 virtual void destroy() // nothrow
\r
209 typedef typename A::template rebind< this_type >::other A2;
\r
213 this->~this_type();
\r
214 a2.deallocate( this, 1 );
\r
217 virtual void * get_deleter( detail::sp_typeinfo const & ti )
\r
219 return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
\r
223 #ifdef __CODEGUARD__
\r
224 # pragma option pop
\r
227 } // namespace detail
\r
229 } // namespace boost
\r
231 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
\r