]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/interprocess/smart_ptr/weak_ptr.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / smart_ptr / weak_ptr.hpp
CommitLineData
7c673cae
FG
1//////////////////////////////////////////////////////////////////////////////
2//
3// This file is the adaptation for Interprocess of boost/weak_ptr.hpp
4//
5// (C) Copyright Peter Dimov 2001, 2002, 2003
6// (C) Copyright Ion Gaztanaga 2006-2012.
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/interprocess for documentation.
12//
13//////////////////////////////////////////////////////////////////////////////
14
15#ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
16#define BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
17
18#ifndef BOOST_CONFIG_HPP
19# include <boost/config.hpp>
20#endif
21#
22#if defined(BOOST_HAS_PRAGMA_ONCE)
23# pragma once
24#endif
25
26#include <boost/interprocess/detail/config_begin.hpp>
27#include <boost/interprocess/detail/workaround.hpp>
28
29#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
30#include <boost/core/no_exceptions_support.hpp>
31#include <boost/interprocess/allocators/allocator.hpp>
32#include <boost/interprocess/smart_ptr/deleter.hpp>
33#include <boost/intrusive/pointer_traits.hpp>
34#include <boost/move/adl_move_swap.hpp>
35
36//!\file
37//!Describes the smart pointer weak_ptr.
38
39namespace boost{
40namespace interprocess{
41
42//!The weak_ptr class template stores a "weak reference" to an object
43//!that's already managed by a shared_ptr. To access the object, a weak_ptr
44//!can be converted to a shared_ptr using the shared_ptr constructor or the
45//!member function lock. When the last shared_ptr to the object goes away
46//!and the object is deleted, the attempt to obtain a shared_ptr from the
47//!weak_ptr instances that refer to the deleted object will fail: the constructor
48//!will throw an exception of type bad_weak_ptr, and weak_ptr::lock will
49//!return an empty shared_ptr.
50//!
51//!Every weak_ptr meets the CopyConstructible and Assignable requirements
52//!of the C++ Standard Library, and so can be used in standard library containers.
53//!Comparison operators are supplied so that weak_ptr works with the standard
54//!library's associative containers.
55//!
56//!weak_ptr operations never throw exceptions.
57//!
58//!The class template is parameterized on T, the type of the object pointed to.
59template<class T, class A, class D>
60class weak_ptr
61{
62 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
63 private:
64 // Borland 5.5.1 specific workarounds
65 typedef weak_ptr<T, A, D> this_type;
1e59de90
TL
66 typedef typename boost::container::
67 allocator_traits<A>::pointer alloc_ptr;
7c673cae 68 typedef typename boost::intrusive::
1e59de90 69 pointer_traits<alloc_ptr>::template
7c673cae
FG
70 rebind_pointer<T>::type pointer;
71 typedef typename ipcdetail::add_reference
72 <T>::type reference;
73 typedef typename ipcdetail::add_reference
74 <T>::type const_reference;
75 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
76
77 public:
78 typedef T element_type;
79 typedef T value_type;
80
81 //!Effects: Constructs an empty weak_ptr.
82 //!Postconditions: use_count() == 0.
83 weak_ptr()
84 : m_pn() // never throws
85 {}
86 // generated copy constructor, assignment, destructor are fine
87 //
88 // The "obvious" converting constructor implementation:
89 //
90 // template<class Y>
91 // weak_ptr(weak_ptr<Y> const & r): m_px(r.m_px), m_pn(r.m_pn) // never throws
92 // {
93 // }
94 //
95 // has a serious problem.
96 //
97 // r.m_px may already have been invalidated. The m_px(r.m_px)
98 // conversion may require access to *r.m_px (virtual inheritance).
99 //
100 // It is not possible to avoid spurious access violations since
101 // in multithreaded programs r.m_px may be invalidated at any point.
102
103 //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
104 //!constructs a weak_ptr that shares ownership with r as if by storing a
105 //!copy of the pointer stored in r.
106 //!
107 //!Postconditions: use_count() == r.use_count().
108 //!
109 //!Throws: nothing.
110 template<class Y>
111 weak_ptr(weak_ptr<Y, A, D> const & r)
112 : m_pn(r.m_pn) // never throws
113 {
114 //Construct a temporary shared_ptr so that nobody
115 //can destroy the value while constructing this
116 const shared_ptr<T, A, D> &ref = r.lock();
117 m_pn.set_pointer(ref.get());
118 }
119
120 //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
121 //!constructs a weak_ptr that shares ownership with r as if by storing a
122 //!copy of the pointer stored in r.
123 //!
124 //!Postconditions: use_count() == r.use_count().
125 //!
126 //!Throws: nothing.
127 template<class Y>
128 weak_ptr(shared_ptr<Y, A, D> const & r)
129 : m_pn(r.m_pn) // never throws
130 {}
131
132 //!Effects: Equivalent to weak_ptr(r).swap(*this).
133 //!
134 //!Throws: nothing.
135 //!
136 //!Notes: The implementation is free to meet the effects (and the
137 //!implied guarantees) via different means, without creating a temporary.
138 template<class Y>
139 weak_ptr & operator=(weak_ptr<Y, A, D> const & r) // never throws
140 {
141 //Construct a temporary shared_ptr so that nobody
142 //can destroy the value while constructing this
143 const shared_ptr<T, A, D> &ref = r.lock();
144 m_pn = r.m_pn;
145 m_pn.set_pointer(ref.get());
146 return *this;
147 }
148
149 //!Effects: Equivalent to weak_ptr(r).swap(*this).
150 //!
151 //!Throws: nothing.
152 //!
153 //!Notes: The implementation is free to meet the effects (and the
154 //!implied guarantees) via different means, without creating a temporary.
155 template<class Y>
156 weak_ptr & operator=(shared_ptr<Y, A, D> const & r) // never throws
157 { m_pn = r.m_pn; return *this; }
158
159 //!Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).
160 //!
161 //!Throws: nothing.
162 shared_ptr<T, A, D> lock() const // never throws
163 {
164 // optimization: avoid throw overhead
165 if(expired()){
166 return shared_ptr<element_type, A, D>();
167 }
168 BOOST_TRY{
169 return shared_ptr<element_type, A, D>(*this);
170 }
171 BOOST_CATCH(bad_weak_ptr const &){
172 // Q: how can we get here?
173 // A: another thread may have invalidated r after the use_count test above.
174 return shared_ptr<element_type, A, D>();
175 }
176 BOOST_CATCH_END
177 }
178
179 //!Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects
180 //!that share ownership with *this.
181 //!
182 //!Throws: nothing.
183 //!
184 //!Notes: use_count() is not necessarily efficient. Use only for debugging and
185 //!testing purposes, not for production code.
186 long use_count() const // never throws
187 { return m_pn.use_count(); }
188
189 //!Returns: Returns: use_count() == 0.
190 //!
191 //!Throws: nothing.
192 //!
193 //!Notes: expired() may be faster than use_count().
194 bool expired() const // never throws
195 { return m_pn.use_count() == 0; }
196
197 //!Effects: Equivalent to:
198 //!weak_ptr().swap(*this).
199 void reset() // never throws in 1.30+
200 { this_type().swap(*this); }
201
202 //!Effects: Exchanges the contents of the two
203 //!smart pointers.
204 //!
205 //!Throws: nothing.
206 void swap(this_type & other) // never throws
207 { ::boost::adl_move_swap(m_pn, other.m_pn); }
208
209 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
210 template<class T2, class A2, class D2>
211 bool _internal_less(weak_ptr<T2, A2, D2> const & rhs) const
212 { return m_pn < rhs.m_pn; }
213
214 template<class Y>
215 void _internal_assign(const ipcdetail::shared_count<Y, A, D> & pn2)
216 {
217
218 m_pn = pn2;
219 }
220
221 private:
222
223 template<class T2, class A2, class D2> friend class shared_ptr;
224 template<class T2, class A2, class D2> friend class weak_ptr;
225
226 ipcdetail::weak_count<T, A, D> m_pn; // reference counter
227 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
228}; // weak_ptr
229
230template<class T, class A, class D, class U, class A2, class D2> inline
231bool operator<(weak_ptr<T, A, D> const & a, weak_ptr<U, A2, D2> const & b)
232{ return a._internal_less(b); }
233
234template<class T, class A, class D> inline
235void swap(weak_ptr<T, A, D> & a, weak_ptr<T, A, D> & b)
236{ a.swap(b); }
237
238//!Returns the type of a weak pointer
239//!of type T with the allocator boost::interprocess::allocator allocator
240//!and boost::interprocess::deleter deleter
241//!that can be constructed in the given managed segment type.
242template<class T, class ManagedMemory>
243struct managed_weak_ptr
244{
245 typedef weak_ptr
246 < T
247 , typename ManagedMemory::template allocator<void>::type
248 , typename ManagedMemory::template deleter<T>::type
249 > type;
250};
251
252//!Returns an instance of a weak pointer constructed
253//!with the default allocator and deleter from a pointer
254//!of type T that has been allocated in the passed managed segment
255template<class T, class ManagedMemory>
256inline typename managed_weak_ptr<T, ManagedMemory>::type
257 make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory)
258{
259 return typename managed_weak_ptr<T, ManagedMemory>::type
260 ( constructed_object
261 , managed_memory.template get_allocator<void>()
262 , managed_memory.template get_deleter<T>()
263 );
264}
265
266} // namespace interprocess
267} // namespace boost
268
269#include <boost/interprocess/detail/config_end.hpp>
270
271#endif // #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED