123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
- #define BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
- #ifndef BOOST_CONFIG_HPP
- # include <boost/config.hpp>
- #endif
- #
- #if defined(BOOST_HAS_PRAGMA_ONCE)
- # pragma once
- #endif
- #include <boost/interprocess/detail/config_begin.hpp>
- #include <boost/interprocess/detail/workaround.hpp>
- #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
- #include <boost/core/no_exceptions_support.hpp>
- #include <boost/interprocess/allocators/allocator.hpp>
- #include <boost/interprocess/smart_ptr/deleter.hpp>
- #include <boost/intrusive/pointer_traits.hpp>
- #include <boost/move/adl_move_swap.hpp>
- namespace boost{
- namespace interprocess{
- template<class T, class A, class D>
- class weak_ptr
- {
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- private:
-
- typedef weak_ptr<T, A, D> this_type;
- typedef typename boost::container::
- allocator_traits<A>::pointer alloc_ptr;
- typedef typename boost::intrusive::
- pointer_traits<alloc_ptr>::template
- rebind_pointer<T>::type pointer;
- typedef typename ipcdetail::add_reference
- <T>::type reference;
- typedef typename ipcdetail::add_reference
- <T>::type const_reference;
- #endif
- public:
- typedef T element_type;
- typedef T value_type;
-
-
- weak_ptr()
- : m_pn()
- {}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class Y>
- weak_ptr(weak_ptr<Y, A, D> const & r)
- : m_pn(r.m_pn)
- {
-
-
- const shared_ptr<T, A, D> &ref = r.lock();
- m_pn.set_pointer(ref.get());
- }
-
-
-
-
-
-
-
- template<class Y>
- weak_ptr(shared_ptr<Y, A, D> const & r)
- : m_pn(r.m_pn)
- {}
-
-
-
-
-
-
- template<class Y>
- weak_ptr & operator=(weak_ptr<Y, A, D> const & r)
- {
-
-
- const shared_ptr<T, A, D> &ref = r.lock();
- m_pn = r.m_pn;
- m_pn.set_pointer(ref.get());
- return *this;
- }
-
-
-
-
-
-
- template<class Y>
- weak_ptr & operator=(shared_ptr<Y, A, D> const & r)
- { m_pn = r.m_pn; return *this; }
-
-
-
- shared_ptr<T, A, D> lock() const
- {
-
- if(expired()){
- return shared_ptr<element_type, A, D>();
- }
- BOOST_TRY{
- return shared_ptr<element_type, A, D>(*this);
- }
- BOOST_CATCH(bad_weak_ptr const &){
-
-
- return shared_ptr<element_type, A, D>();
- }
- BOOST_CATCH_END
- }
-
-
-
-
-
-
-
- long use_count() const
- { return m_pn.use_count(); }
-
-
-
-
-
- bool expired() const
- { return m_pn.use_count() == 0; }
-
-
- void reset()
- { this_type().swap(*this); }
-
-
-
-
- void swap(this_type & other)
- { ::boost::adl_move_swap(m_pn, other.m_pn); }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- template<class T2, class A2, class D2>
- bool _internal_less(weak_ptr<T2, A2, D2> const & rhs) const
- { return m_pn < rhs.m_pn; }
- template<class Y>
- void _internal_assign(const ipcdetail::shared_count<Y, A, D> & pn2)
- {
- m_pn = pn2;
- }
- private:
- template<class T2, class A2, class D2> friend class shared_ptr;
- template<class T2, class A2, class D2> friend class weak_ptr;
- ipcdetail::weak_count<T, A, D> m_pn;
- #endif
- };
- template<class T, class A, class D, class U, class A2, class D2> inline
- bool operator<(weak_ptr<T, A, D> const & a, weak_ptr<U, A2, D2> const & b)
- { return a._internal_less(b); }
- template<class T, class A, class D> inline
- void swap(weak_ptr<T, A, D> & a, weak_ptr<T, A, D> & b)
- { a.swap(b); }
- template<class T, class ManagedMemory>
- struct managed_weak_ptr
- {
- typedef weak_ptr
- < T
- , typename ManagedMemory::template allocator<void>::type
- , typename ManagedMemory::template deleter<T>::type
- > type;
- };
- template<class T, class ManagedMemory>
- inline typename managed_weak_ptr<T, ManagedMemory>::type
- make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory)
- {
- return typename managed_weak_ptr<T, ManagedMemory>::type
- ( constructed_object
- , managed_memory.template get_allocator<void>()
- , managed_memory.template get_deleter<T>()
- );
- }
- }
- }
- #include <boost/interprocess/detail/config_end.hpp>
- #endif
|