sp_counted_impl.hpp 5.4 KB

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