make_local_shared_object.hpp 4.7 KB

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