sp_counted_impl.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/smart_ptr/detail/sp_counted_base.hpp>
  18. #include <boost/smart_ptr/detail/deprecated_macros.hpp>
  19. #include <boost/core/checked_delete.hpp>
  20. #include <boost/core/addressof.hpp>
  21. #include <boost/config.hpp>
  22. #include <memory> // std::allocator, std::allocator_traits
  23. #include <cstddef> // std::size_t
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. // get_local_deleter
  29. template<class D> class local_sp_deleter;
  30. template<class D> D * get_local_deleter( D * /*p*/ ) noexcept
  31. {
  32. return 0;
  33. }
  34. template<class D> D * get_local_deleter( local_sp_deleter<D> * p ) noexcept;
  35. //
  36. template<class X> class BOOST_SYMBOL_VISIBLE sp_counted_impl_p: public sp_counted_base
  37. {
  38. private:
  39. X * px_;
  40. sp_counted_impl_p( sp_counted_impl_p const & );
  41. sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
  42. typedef sp_counted_impl_p<X> this_type;
  43. public:
  44. explicit sp_counted_impl_p( X * px ): px_( px )
  45. {
  46. }
  47. void dispose() noexcept override
  48. {
  49. boost::checked_delete( px_ );
  50. }
  51. void * get_deleter( sp_typeinfo_ const & ) noexcept override
  52. {
  53. return 0;
  54. }
  55. void * get_local_deleter( sp_typeinfo_ const & ) noexcept override
  56. {
  57. return 0;
  58. }
  59. void * get_untyped_deleter() noexcept override
  60. {
  61. return 0;
  62. }
  63. };
  64. template<class P, class D> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pd: public sp_counted_base
  65. {
  66. private:
  67. P ptr; // copy constructor must not throw
  68. D del; // copy/move constructor must not throw
  69. sp_counted_impl_pd( sp_counted_impl_pd const & );
  70. sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
  71. typedef sp_counted_impl_pd<P, D> this_type;
  72. public:
  73. // pre: d(p) must not throw
  74. sp_counted_impl_pd( P p, D & d ): ptr( p ), del( static_cast< D&& >( d ) )
  75. {
  76. }
  77. sp_counted_impl_pd( P p ): ptr( p ), del()
  78. {
  79. }
  80. void dispose() noexcept override
  81. {
  82. del( ptr );
  83. }
  84. void * get_deleter( sp_typeinfo_ const & ti ) noexcept override
  85. {
  86. return ti == BOOST_SP_TYPEID_(D)? &reinterpret_cast<char&>( del ): 0;
  87. }
  88. void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept override
  89. {
  90. return ti == BOOST_SP_TYPEID_(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0;
  91. }
  92. void * get_untyped_deleter() noexcept override
  93. {
  94. return &reinterpret_cast<char&>( del );
  95. }
  96. };
  97. template<class P, class D, class A> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pda: public sp_counted_base
  98. {
  99. private:
  100. P p_; // copy constructor must not throw
  101. D d_; // copy/move constructor must not throw
  102. A a_; // copy constructor must not throw
  103. sp_counted_impl_pda( sp_counted_impl_pda const & );
  104. sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
  105. typedef sp_counted_impl_pda<P, D, A> this_type;
  106. public:
  107. // pre: d( p ) must not throw
  108. sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( static_cast< D&& >( d ) ), a_( a )
  109. {
  110. }
  111. sp_counted_impl_pda( P p, A a ): p_( p ), d_( a ), a_( a )
  112. {
  113. }
  114. void dispose() noexcept override
  115. {
  116. d_( p_ );
  117. }
  118. void destroy() noexcept override
  119. {
  120. typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
  121. A2 a2( a_ );
  122. this->~this_type();
  123. a2.deallocate( this, 1 );
  124. }
  125. void * get_deleter( sp_typeinfo_ const & ti ) noexcept override
  126. {
  127. return ti == BOOST_SP_TYPEID_( D )? &reinterpret_cast<char&>( d_ ): 0;
  128. }
  129. void * get_local_deleter( sp_typeinfo_ const & ti ) noexcept override
  130. {
  131. return ti == BOOST_SP_TYPEID_( D )? boost::detail::get_local_deleter( boost::addressof( d_ ) ): 0;
  132. }
  133. void * get_untyped_deleter() noexcept override
  134. {
  135. return &reinterpret_cast<char&>( d_ );
  136. }
  137. };
  138. } // namespace detail
  139. } // namespace boost
  140. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED