make_local_shared_object.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
  3. // make_local_shared_object.hpp
  4. //
  5. // Copyright 2017 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. //
  11. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  12. #include <boost/smart_ptr/detail/requires_cxx11.hpp>
  13. #include <boost/smart_ptr/local_shared_ptr.hpp>
  14. #include <boost/smart_ptr/make_shared.hpp>
  15. #include <boost/type_traits/remove_const.hpp>
  16. #include <boost/config.hpp>
  17. #include <utility>
  18. #include <cstddef>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. // lsp_if_not_array
  24. template<class T> struct lsp_if_not_array
  25. {
  26. typedef boost::local_shared_ptr<T> type;
  27. };
  28. template<class T> struct lsp_if_not_array<T[]>
  29. {
  30. };
  31. template<class T, std::size_t N> struct lsp_if_not_array<T[N]>
  32. {
  33. };
  34. // lsp_ms_deleter
  35. template<class T, class A> class lsp_ms_deleter: public local_counted_impl_em
  36. {
  37. private:
  38. typedef typename sp_aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type storage_type;
  39. storage_type storage_;
  40. A a_;
  41. bool initialized_;
  42. private:
  43. void destroy() BOOST_SP_NOEXCEPT
  44. {
  45. if( initialized_ )
  46. {
  47. T * p = reinterpret_cast< T* >( storage_.data_ );
  48. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  49. std::allocator_traits<A>::destroy( a_, p );
  50. #else
  51. p->~T();
  52. #endif
  53. initialized_ = false;
  54. }
  55. }
  56. public:
  57. explicit lsp_ms_deleter( A const & a ) BOOST_SP_NOEXCEPT : a_( a ), initialized_( false )
  58. {
  59. }
  60. // optimization: do not copy storage_
  61. lsp_ms_deleter( lsp_ms_deleter const & r ) BOOST_SP_NOEXCEPT : a_( r.a_), initialized_( false )
  62. {
  63. }
  64. ~lsp_ms_deleter() BOOST_SP_NOEXCEPT
  65. {
  66. destroy();
  67. }
  68. void operator()( T * ) BOOST_SP_NOEXCEPT
  69. {
  70. destroy();
  71. }
  72. static void operator_fn( T* ) BOOST_SP_NOEXCEPT // operator() can't be static
  73. {
  74. }
  75. void * address() BOOST_SP_NOEXCEPT
  76. {
  77. return storage_.data_;
  78. }
  79. void set_initialized() BOOST_SP_NOEXCEPT
  80. {
  81. initialized_ = true;
  82. }
  83. };
  84. } // namespace detail
  85. template<class T, class A, class... Args> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared( A const & a, Args&&... args )
  86. {
  87. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  88. typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
  89. #else
  90. typedef typename A::template rebind<T>::other A2;
  91. #endif
  92. A2 a2( a );
  93. typedef boost::detail::lsp_ms_deleter<T, A2> D;
  94. boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
  95. D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
  96. void * pv = pd->address();
  97. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  98. std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
  99. #else
  100. ::new( pv ) T( std::forward<Args>( args )... );
  101. #endif
  102. pd->set_initialized();
  103. T * pt2 = static_cast< T* >( pv );
  104. boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
  105. pd->pn_ = pt._internal_count();
  106. return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
  107. }
  108. template<class T, class A> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared_noinit( A const & a )
  109. {
  110. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  111. typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
  112. #else
  113. typedef typename A::template rebind<T>::other A2;
  114. #endif
  115. A2 a2( a );
  116. typedef boost::detail::lsp_ms_deleter< T, std::allocator<T> > D;
  117. boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
  118. D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
  119. void * pv = pd->address();
  120. ::new( pv ) T;
  121. pd->set_initialized();
  122. T * pt2 = static_cast< T* >( pv );
  123. boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
  124. pd->pn_ = pt._internal_count();
  125. return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
  126. }
  127. template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
  128. {
  129. typedef typename boost::remove_const<T>::type T2;
  130. return boost::allocate_local_shared<T2>( std::allocator<T2>(), std::forward<Args>(args)... );
  131. }
  132. template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
  133. {
  134. typedef typename boost::remove_const<T>::type T2;
  135. return boost::allocate_shared_noinit<T2>( std::allocator<T2>() );
  136. }
  137. } // namespace boost
  138. #endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED