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 
39 namespace boost{
40 namespace 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.
59 template<class T, class A, class D>
60 class 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;
66    typedef typename boost::intrusive::
67       pointer_traits<typename A::pointer>::template
68          rebind_pointer<T>::type                         pointer;
69    typedef typename ipcdetail::add_reference
70                      <T>::type            reference;
71    typedef typename ipcdetail::add_reference
72                      <T>::type            const_reference;
73    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
74 
75    public:
76    typedef T element_type;
77    typedef T value_type;
78 
79    //!Effects: Constructs an empty weak_ptr.
80    //!Postconditions: use_count() == 0.
weak_ptr()81    weak_ptr()
82    : m_pn() // never throws
83    {}
84    //  generated copy constructor, assignment, destructor are fine
85    //
86    //  The "obvious" converting constructor implementation:
87    //
88    //  template<class Y>
89    //  weak_ptr(weak_ptr<Y> const & r): m_px(r.m_px), m_pn(r.m_pn) // never throws
90    //  {
91    //  }
92    //
93    //  has a serious problem.
94    //
95    //  r.m_px may already have been invalidated. The m_px(r.m_px)
96    //  conversion may require access to *r.m_px (virtual inheritance).
97    //
98    //  It is not possible to avoid spurious access violations since
99    //  in multithreaded programs r.m_px may be invalidated at any point.
100 
101    //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
102    //!constructs a weak_ptr that shares ownership with r as if by storing a
103    //!copy of the pointer stored in r.
104    //!
105    //!Postconditions: use_count() == r.use_count().
106    //!
107    //!Throws: nothing.
108    template<class Y>
weak_ptr(weak_ptr<Y,A,D> const & r)109    weak_ptr(weak_ptr<Y, A, D> const & r)
110       : m_pn(r.m_pn) // never throws
111    {
112       //Construct a temporary shared_ptr so that nobody
113       //can destroy the value while constructing this
114       const shared_ptr<T, A, D> &ref = r.lock();
115       m_pn.set_pointer(ref.get());
116    }
117 
118    //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
119    //!constructs a weak_ptr that shares ownership with r as if by storing a
120    //!copy of the pointer stored in r.
121    //!
122    //!Postconditions: use_count() == r.use_count().
123    //!
124    //!Throws: nothing.
125    template<class Y>
weak_ptr(shared_ptr<Y,A,D> const & r)126    weak_ptr(shared_ptr<Y, A, D> const & r)
127       : m_pn(r.m_pn) // never throws
128    {}
129 
130    //!Effects: Equivalent to weak_ptr(r).swap(*this).
131    //!
132    //!Throws: nothing.
133    //!
134    //!Notes: The implementation is free to meet the effects (and the
135    //!implied guarantees) via different means, without creating a temporary.
136    template<class Y>
operator =(weak_ptr<Y,A,D> const & r)137    weak_ptr & operator=(weak_ptr<Y, A, D> const & r) // never throws
138    {
139       //Construct a temporary shared_ptr so that nobody
140       //can destroy the value while constructing this
141       const shared_ptr<T, A, D> &ref = r.lock();
142       m_pn = r.m_pn;
143       m_pn.set_pointer(ref.get());
144       return *this;
145    }
146 
147    //!Effects: Equivalent to weak_ptr(r).swap(*this).
148    //!
149    //!Throws: nothing.
150    //!
151    //!Notes: The implementation is free to meet the effects (and the
152    //!implied guarantees) via different means, without creating a temporary.
153    template<class Y>
operator =(shared_ptr<Y,A,D> const & r)154    weak_ptr & operator=(shared_ptr<Y, A, D> const & r) // never throws
155    {  m_pn = r.m_pn;  return *this;  }
156 
157    //!Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).
158    //!
159    //!Throws: nothing.
lock() const160    shared_ptr<T, A, D> lock() const // never throws
161    {
162       // optimization: avoid throw overhead
163       if(expired()){
164          return shared_ptr<element_type, A, D>();
165       }
166       BOOST_TRY{
167          return shared_ptr<element_type, A, D>(*this);
168       }
169       BOOST_CATCH(bad_weak_ptr const &){
170          // Q: how can we get here?
171          // A: another thread may have invalidated r after the use_count test above.
172          return shared_ptr<element_type, A, D>();
173       }
174       BOOST_CATCH_END
175    }
176 
177    //!Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects
178    //!that share ownership with *this.
179    //!
180    //!Throws: nothing.
181    //!
182    //!Notes: use_count() is not necessarily efficient. Use only for debugging and
183    //!testing purposes, not for production code.
use_count() const184    long use_count() const // never throws
185    {  return m_pn.use_count();  }
186 
187    //!Returns: Returns: use_count() == 0.
188    //!
189    //!Throws: nothing.
190    //!
191    //!Notes: expired() may be faster than use_count().
expired() const192    bool expired() const // never throws
193    {  return m_pn.use_count() == 0;   }
194 
195    //!Effects: Equivalent to:
196    //!weak_ptr().swap(*this).
reset()197    void reset() // never throws in 1.30+
198    {  this_type().swap(*this);   }
199 
200    //!Effects: Exchanges the contents of the two
201    //!smart pointers.
202    //!
203    //!Throws: nothing.
swap(this_type & other)204    void swap(this_type & other) // never throws
205    {  ::boost::adl_move_swap(m_pn, other.m_pn);   }
206 
207    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
208    template<class T2, class A2, class D2>
_internal_less(weak_ptr<T2,A2,D2> const & rhs) const209    bool _internal_less(weak_ptr<T2, A2, D2> const & rhs) const
210    {  return m_pn < rhs.m_pn;  }
211 
212    template<class Y>
_internal_assign(const ipcdetail::shared_count<Y,A,D> & pn2)213    void _internal_assign(const ipcdetail::shared_count<Y, A, D> & pn2)
214    {
215 
216       m_pn = pn2;
217    }
218 
219    private:
220 
221    template<class T2, class A2, class D2> friend class shared_ptr;
222    template<class T2, class A2, class D2> friend class weak_ptr;
223 
224    ipcdetail::weak_count<T, A, D> m_pn;      // reference counter
225    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
226 };  // weak_ptr
227 
228 template<class T, class A, class D, class U, class A2, class D2> inline
operator <(weak_ptr<T,A,D> const & a,weak_ptr<U,A2,D2> const & b)229 bool operator<(weak_ptr<T, A, D> const & a, weak_ptr<U, A2, D2> const & b)
230 {  return a._internal_less(b);   }
231 
232 template<class T, class A, class D> inline
swap(weak_ptr<T,A,D> & a,weak_ptr<T,A,D> & b)233 void swap(weak_ptr<T, A, D> & a, weak_ptr<T, A, D> & b)
234 {  a.swap(b);  }
235 
236 //!Returns the type of a weak pointer
237 //!of type T with the allocator boost::interprocess::allocator allocator
238 //!and boost::interprocess::deleter deleter
239 //!that can be constructed in the given managed segment type.
240 template<class T, class ManagedMemory>
241 struct managed_weak_ptr
242 {
243    typedef weak_ptr
244    < T
245    , typename ManagedMemory::template allocator<void>::type
246    , typename ManagedMemory::template deleter<T>::type
247    > type;
248 };
249 
250 //!Returns an instance of a weak pointer constructed
251 //!with the default allocator and deleter from a pointer
252 //!of type T that has been allocated in the passed managed segment
253 template<class T, class ManagedMemory>
254 inline typename managed_weak_ptr<T, ManagedMemory>::type
make_managed_weak_ptr(T * constructed_object,ManagedMemory & managed_memory)255    make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory)
256 {
257    return typename managed_weak_ptr<T, ManagedMemory>::type
258    ( constructed_object
259    , managed_memory.template get_allocator<void>()
260    , managed_memory.template get_deleter<T>()
261    );
262 }
263 
264 } // namespace interprocess
265 } // namespace boost
266 
267 #include <boost/interprocess/detail/config_end.hpp>
268 
269 #endif  // #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
270