make_local_shared_object.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/config.hpp>
  15. #include <type_traits>
  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), std::alignment_of<T>::value>::type storage_type;
  38. storage_type storage_;
  39. A a_;
  40. bool initialized_;
  41. private:
  42. void destroy() noexcept
  43. {
  44. if( initialized_ )
  45. {
  46. T * p = reinterpret_cast< T* >( storage_.data_ );
  47. std::allocator_traits<A>::destroy( a_, p );
  48. initialized_ = false;
  49. }
  50. }
  51. public:
  52. explicit lsp_ms_deleter( A const & a ) noexcept : a_( a ), initialized_( false )
  53. {
  54. }
  55. // optimization: do not copy storage_
  56. lsp_ms_deleter( lsp_ms_deleter const & r ) noexcept : a_( r.a_), initialized_( false )
  57. {
  58. }
  59. ~lsp_ms_deleter() noexcept
  60. {
  61. destroy();
  62. }
  63. void operator()( T * ) noexcept
  64. {
  65. destroy();
  66. }
  67. static void operator_fn( T* ) noexcept // operator() can't be static
  68. {
  69. }
  70. void * address() noexcept
  71. {
  72. return storage_.data_;
  73. }
  74. void set_initialized() noexcept
  75. {
  76. initialized_ = true;
  77. }
  78. };
  79. } // namespace detail
  80. template<class T, class A, class... Args> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared( A const & a, Args&&... args )
  81. {
  82. typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
  83. A2 a2( a );
  84. typedef boost::detail::lsp_ms_deleter<T, A2> D;
  85. boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
  86. D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
  87. void * pv = pd->address();
  88. std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
  89. pd->set_initialized();
  90. T * pt2 = static_cast< T* >( pv );
  91. boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
  92. pd->pn_ = pt._internal_count();
  93. return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
  94. }
  95. template<class T, class A> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared_noinit( A const & a )
  96. {
  97. typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
  98. A2 a2( a );
  99. typedef boost::detail::lsp_ms_deleter< T, std::allocator<T> > D;
  100. boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
  101. D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
  102. void * pv = pd->address();
  103. ::new( pv ) T;
  104. pd->set_initialized();
  105. T * pt2 = static_cast< T* >( pv );
  106. boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
  107. pd->pn_ = pt._internal_count();
  108. return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
  109. }
  110. template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
  111. {
  112. typedef typename std::remove_const<T>::type T2;
  113. return boost::allocate_local_shared<T2>( std::allocator<T2>(), std::forward<Args>(args)... );
  114. }
  115. template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
  116. {
  117. typedef typename std::remove_const<T>::type T2;
  118. return boost::allocate_shared_noinit<T2>( std::allocator<T2>() );
  119. }
  120. } // namespace boost
  121. #endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED