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