sp_counted_impl.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/sp_counted_impl.hpp
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/config.hpp>
  18. #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  19. # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
  20. #endif
  21. #include <boost/checked_delete.hpp>
  22. #include <boost/smart_ptr/detail/sp_counted_base.hpp>
  23. #include <boost/core/addressof.hpp>
  24. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  25. #include <boost/smart_ptr/detail/quick_allocator.hpp>
  26. #endif
  27. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  28. #include <memory> // std::allocator
  29. #endif
  30. #include <cstddef> // std::size_t
  31. namespace boost
  32. {
  33. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  34. void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
  35. void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
  36. #endif
  37. namespace detail
  38. {
  39. // get_local_deleter
  40. template<class D> class local_sp_deleter;
  41. template<class D> D * get_local_deleter( D * /*p*/ )
  42. {
  43. return 0;
  44. }
  45. template<class D> D * get_local_deleter( local_sp_deleter<D> * p );
  46. //
  47. template<class X> class sp_counted_impl_p: public sp_counted_base
  48. {
  49. private:
  50. X * px_;
  51. sp_counted_impl_p( sp_counted_impl_p const & );
  52. sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
  53. typedef sp_counted_impl_p<X> this_type;
  54. public:
  55. explicit sp_counted_impl_p( X * px ): px_( px )
  56. {
  57. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  58. boost::sp_scalar_constructor_hook( px, sizeof(X), this );
  59. #endif
  60. }
  61. virtual void dispose() // nothrow
  62. {
  63. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  64. boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
  65. #endif
  66. boost::checked_delete( px_ );
  67. }
  68. virtual void * get_deleter( sp_typeinfo const & )
  69. {
  70. return 0;
  71. }
  72. virtual void * get_local_deleter( sp_typeinfo const & )
  73. {
  74. return 0;
  75. }
  76. virtual void * get_untyped_deleter()
  77. {
  78. return 0;
  79. }
  80. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  81. void * operator new( std::size_t )
  82. {
  83. return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
  84. }
  85. void operator delete( void * p )
  86. {
  87. std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
  88. }
  89. #endif
  90. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  91. void * operator new( std::size_t )
  92. {
  93. return quick_allocator<this_type>::alloc();
  94. }
  95. void operator delete( void * p )
  96. {
  97. quick_allocator<this_type>::dealloc( p );
  98. }
  99. #endif
  100. };
  101. //
  102. // Borland's Codeguard trips up over the -Vx- option here:
  103. //
  104. #ifdef __CODEGUARD__
  105. # pragma option push -Vx-
  106. #endif
  107. template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
  108. {
  109. private:
  110. P ptr; // copy constructor must not throw
  111. D del; // copy constructor must not throw
  112. sp_counted_impl_pd( sp_counted_impl_pd const & );
  113. sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
  114. typedef sp_counted_impl_pd<P, D> this_type;
  115. public:
  116. // pre: d(p) must not throw
  117. sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
  118. {
  119. }
  120. sp_counted_impl_pd( P p ): ptr( p ), del()
  121. {
  122. }
  123. virtual void dispose() // nothrow
  124. {
  125. del( ptr );
  126. }
  127. virtual void * get_deleter( sp_typeinfo const & ti )
  128. {
  129. return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
  130. }
  131. virtual void * get_local_deleter( sp_typeinfo const & ti )
  132. {
  133. return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
  134. }
  135. virtual void * get_untyped_deleter()
  136. {
  137. return &reinterpret_cast<char&>( del );
  138. }
  139. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  140. void * operator new( std::size_t )
  141. {
  142. return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
  143. }
  144. void operator delete( void * p )
  145. {
  146. std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
  147. }
  148. #endif
  149. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  150. void * operator new( std::size_t )
  151. {
  152. return quick_allocator<this_type>::alloc();
  153. }
  154. void operator delete( void * p )
  155. {
  156. quick_allocator<this_type>::dealloc( p );
  157. }
  158. #endif
  159. };
  160. template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
  161. {
  162. private:
  163. P p_; // copy constructor must not throw
  164. D d_; // copy constructor must not throw
  165. A a_; // copy constructor must not throw
  166. sp_counted_impl_pda( sp_counted_impl_pda const & );
  167. sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
  168. typedef sp_counted_impl_pda<P, D, A> this_type;
  169. public:
  170. // pre: d( p ) must not throw
  171. sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
  172. {
  173. }
  174. sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
  175. {
  176. }
  177. virtual void dispose() // nothrow
  178. {
  179. d_( p_ );
  180. }
  181. virtual void destroy() // nothrow
  182. {
  183. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  184. typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
  185. #else
  186. typedef typename A::template rebind< this_type >::other A2;
  187. #endif
  188. A2 a2( a_ );
  189. this->~this_type();
  190. a2.deallocate( this, 1 );
  191. }
  192. virtual void * get_deleter( sp_typeinfo const & ti )
  193. {
  194. return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
  195. }
  196. virtual void * get_local_deleter( sp_typeinfo const & ti )
  197. {
  198. return ti == BOOST_SP_TYPEID(D)? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
  199. }
  200. virtual void * get_untyped_deleter()
  201. {
  202. return &reinterpret_cast<char&>( d_ );
  203. }
  204. };
  205. #ifdef __CODEGUARD__
  206. # pragma option pop
  207. #endif
  208. } // namespace detail
  209. } // namespace boost
  210. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED