]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/make_local_shared_object.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / smart_ptr / make_local_shared_object.hpp
1 #ifndef BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_MAKE_LOCAL_SHARED_OBJECT_HPP_INCLUDED
3
4 // make_local_shared_object.hpp
5 //
6 // Copyright 2017 Peter Dimov
7 //
8 // Distributed under the Boost Software License, Version 1.0.
9 // See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt
11 //
12 // See http://www.boost.org/libs/smart_ptr/ for documentation.
13
14 #include <boost/smart_ptr/local_shared_ptr.hpp>
15 #include <boost/smart_ptr/make_shared.hpp>
16 #include <boost/config.hpp>
17 #include <utility>
18 #include <cstddef>
19
20 namespace boost
21 {
22
23 namespace detail
24 {
25
26 // lsp_if_not_array
27
28 template<class T> struct lsp_if_not_array
29 {
30 typedef boost::local_shared_ptr<T> type;
31 };
32
33 template<class T> struct lsp_if_not_array<T[]>
34 {
35 };
36
37 template<class T, std::size_t N> struct lsp_if_not_array<T[N]>
38 {
39 };
40
41 // lsp_ms_deleter
42
43 template<class T, class A> class lsp_ms_deleter: public local_counted_impl_em
44 {
45 private:
46
47 typedef typename sp_aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type storage_type;
48
49 storage_type storage_;
50 A a_;
51 bool initialized_;
52
53 private:
54
55 void destroy() BOOST_SP_NOEXCEPT
56 {
57 if( initialized_ )
58 {
59 T * p = reinterpret_cast< T* >( storage_.data_ );
60
61 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
62
63 std::allocator_traits<A>::destroy( a_, p );
64
65 #else
66
67 p->~T();
68
69 #endif
70
71 initialized_ = false;
72 }
73 }
74
75 public:
76
77 explicit lsp_ms_deleter( A const & a ) BOOST_SP_NOEXCEPT : a_( a ), initialized_( false )
78 {
79 }
80
81 // optimization: do not copy storage_
82 lsp_ms_deleter( lsp_ms_deleter const & r ) BOOST_SP_NOEXCEPT : a_( r.a_), initialized_( false )
83 {
84 }
85
86 ~lsp_ms_deleter() BOOST_SP_NOEXCEPT
87 {
88 destroy();
89 }
90
91 void operator()( T * ) BOOST_SP_NOEXCEPT
92 {
93 destroy();
94 }
95
96 static void operator_fn( T* ) BOOST_SP_NOEXCEPT // operator() can't be static
97 {
98 }
99
100 void * address() BOOST_SP_NOEXCEPT
101 {
102 return storage_.data_;
103 }
104
105 void set_initialized() BOOST_SP_NOEXCEPT
106 {
107 initialized_ = true;
108 }
109 };
110
111 } // namespace detail
112
113 template<class T, class A, class... Args> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared( A const & a, Args&&... args )
114 {
115 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
116
117 typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
118
119 #else
120
121 typedef typename A::template rebind<T>::other A2;
122
123 #endif
124
125 A2 a2( a );
126
127 typedef boost::detail::lsp_ms_deleter<T, A2> D;
128
129 boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
130
131 D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
132 void * pv = pd->address();
133
134 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
135
136 std::allocator_traits<A2>::construct( a2, static_cast< T* >( pv ), std::forward<Args>( args )... );
137
138 #else
139
140 ::new( pv ) T( std::forward<Args>( args )... );
141
142 #endif
143
144 pd->set_initialized();
145
146 T * pt2 = static_cast< T* >( pv );
147 boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
148
149 pd->pn_ = pt._internal_count();
150
151 return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
152 }
153
154 template<class T, class A> typename boost::detail::lsp_if_not_array<T>::type allocate_local_shared_noinit( A const & a )
155 {
156 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
157
158 typedef typename std::allocator_traits<A>::template rebind_alloc<T> A2;
159
160 #else
161
162 typedef typename A::template rebind<T>::other A2;
163
164 #endif
165
166 A2 a2( a );
167
168 typedef boost::detail::lsp_ms_deleter< T, std::allocator<T> > D;
169
170 boost::shared_ptr<T> pt( static_cast< T* >( 0 ), boost::detail::sp_inplace_tag<D>(), a2 );
171
172 D * pd = static_cast< D* >( pt._internal_get_untyped_deleter() );
173 void * pv = pd->address();
174
175 ::new( pv ) T;
176
177 pd->set_initialized();
178
179 T * pt2 = static_cast< T* >( pv );
180 boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
181
182 pd->pn_ = pt._internal_count();
183
184 return boost::local_shared_ptr<T>( boost::detail::lsp_internal_constructor_tag(), pt2, pd );
185 }
186
187 template<class T, class... Args> typename boost::detail::lsp_if_not_array<T>::type make_local_shared( Args&&... args )
188 {
189 return boost::allocate_local_shared<T>( std::allocator<T>(), std::forward<Args>(args)... );
190 }
191
192 template<class T> typename boost::detail::lsp_if_not_array<T>::type make_local_shared_noinit()
193 {
194 return boost::allocate_shared_noinit<T>( std::allocator<T>() );
195 }
196
197 } // namespace boost
198
199 #endif // #ifndef BOOST_SMART_PTR_MAKE_SHARED_OBJECT_HPP_INCLUDED