sp_counted_impl.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  64. void * operator new( std::size_t )
  65. {
  66. return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
  67. }
  68. void operator delete( void * p )
  69. {
  70. std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
  71. }
  72. #endif
  73. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  74. void * operator new( std::size_t )
  75. {
  76. return quick_allocator<this_type>::alloc();
  77. }
  78. void operator delete( void * p )
  79. {
  80. quick_allocator<this_type>::dealloc( p );
  81. }
  82. #endif
  83. };
  84. //
  85. // Borland's Codeguard trips up over the -Vx- option here:
  86. //
  87. #ifdef __CODEGUARD__
  88. # pragma option push -Vx-
  89. #endif
  90. template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
  91. {
  92. private:
  93. P ptr; // copy constructor must not throw
  94. D del; // copy constructor must not throw
  95. sp_counted_impl_pd( sp_counted_impl_pd const & );
  96. sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
  97. typedef sp_counted_impl_pd<P, D> this_type;
  98. public:
  99. // pre: d(p) must not throw
  100. sp_counted_impl_pd( P p, D d ): ptr(p), del(d)
  101. {
  102. }
  103. virtual void dispose() // nothrow
  104. {
  105. del( ptr );
  106. }
  107. virtual void * get_deleter( detail::sp_typeinfo const & ti )
  108. {
  109. return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
  110. }
  111. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  112. void * operator new( std::size_t )
  113. {
  114. return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
  115. }
  116. void operator delete( void * p )
  117. {
  118. std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
  119. }
  120. #endif
  121. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  122. void * operator new( std::size_t )
  123. {
  124. return quick_allocator<this_type>::alloc();
  125. }
  126. void operator delete( void * p )
  127. {
  128. quick_allocator<this_type>::dealloc( p );
  129. }
  130. #endif
  131. };
  132. template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
  133. {
  134. private:
  135. P p_; // copy constructor must not throw
  136. D d_; // copy constructor must not throw
  137. A a_; // copy constructor must not throw
  138. sp_counted_impl_pda( sp_counted_impl_pda const & );
  139. sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
  140. typedef sp_counted_impl_pda<P, D, A> this_type;
  141. public:
  142. // pre: d( p ) must not throw
  143. sp_counted_impl_pda( P p, D d, A a ): p_( p ), d_( d ), a_( a )
  144. {
  145. }
  146. virtual void dispose() // nothrow
  147. {
  148. d_( p_ );
  149. }
  150. virtual void destroy() // nothrow
  151. {
  152. typedef typename A::template rebind< this_type >::other A2;
  153. A2 a2( a_ );
  154. this->~this_type();
  155. a2.deallocate( this, 1 );
  156. }
  157. virtual void * get_deleter( detail::sp_typeinfo const & ti )
  158. {
  159. return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
  160. }
  161. };
  162. #ifdef __CODEGUARD__
  163. # pragma option pop
  164. #endif
  165. } // namespace detail
  166. } // namespace boost
  167. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED