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