123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #ifndef BOOST_INTERPROCESS_SHARED_PTR_HPP_INCLUDED
- #define BOOST_INTERPROCESS_SHARED_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/detail/utilities.hpp>
- #include <boost/interprocess/detail/cast_tags.hpp>
- #include <boost/assert.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/interprocess/smart_ptr/detail/shared_count.hpp>
- #include <boost/interprocess/detail/mpl.hpp>
- #include <boost/interprocess/detail/nothrow.hpp>
- #include <boost/move/utility_core.hpp>
- #include <boost/interprocess/detail/type_traits.hpp>
- #include <boost/interprocess/allocators/allocator.hpp>
- #include <boost/interprocess/smart_ptr/deleter.hpp>
- #include <boost/intrusive/pointer_traits.hpp>
- #include <iosfwd>
- namespace boost{
- namespace interprocess{
- template<class T, class VoidAllocator, class Deleter> class weak_ptr;
- template<class T, class VoidAllocator, class Deleter> class enable_shared_from_this;
- namespace ipcdetail{
- template<class T, class VoidAllocator, class Deleter>
- inline void sp_enable_shared_from_this
- (shared_count<T, VoidAllocator, Deleter> const & pn
- ,enable_shared_from_this<T, VoidAllocator, Deleter> const*pe
- ,T *ptr)
- {
- (void)ptr;
- if(pe != 0){
- pe->_internal_weak_this._internal_assign(pn);
- }
- }
- template<class T, class VoidAllocator, class Deleter>
- inline void sp_enable_shared_from_this(shared_count<T, VoidAllocator, Deleter> const &, ...)
- {}
- }
- template<class T, class VoidAllocator, class Deleter>
- class shared_ptr
- {
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- private:
- typedef shared_ptr<T, VoidAllocator, Deleter> this_type;
- #endif
- public:
- typedef T element_type;
- typedef T value_type;
- typedef typename boost::container::
- allocator_traits<VoidAllocator>::pointer void_ptr;
- typedef typename boost::intrusive::
- pointer_traits<void_ptr>::template
- rebind_pointer<T>::type pointer;
- typedef typename ipcdetail::add_reference
- <value_type>::type reference;
- typedef typename ipcdetail::add_reference
- <const value_type>::type const_reference;
- typedef typename boost::intrusive::
- pointer_traits<void_ptr>::template
- rebind_pointer<const Deleter>::type const_deleter_pointer;
- typedef typename boost::intrusive::
- pointer_traits<void_ptr>::template
- rebind_pointer<const VoidAllocator>::type const_allocator_pointer;
- BOOST_COPYABLE_AND_MOVABLE(shared_ptr)
- public:
-
-
- shared_ptr()
- : m_pn()
- {}
-
-
-
- explicit shared_ptr(const pointer&p, const VoidAllocator &a = VoidAllocator(), const Deleter &d = Deleter())
- : m_pn(p, a, d)
- {
-
-
- typedef typename boost::intrusive::
- pointer_traits<pointer>::template
- rebind_pointer<T>::type ParameterPointer;
- BOOST_STATIC_ASSERT((ipcdetail::is_same<pointer, ParameterPointer>::value) ||
- (ipcdetail::is_pointer<pointer>::value));
- ipcdetail::sp_enable_shared_from_this<T, VoidAllocator, Deleter>( m_pn, ipcdetail::to_raw_pointer(p), ipcdetail::to_raw_pointer(p) );
- }
-
-
- shared_ptr(const shared_ptr &r)
- : m_pn(r.m_pn)
- {}
-
-
-
- shared_ptr(const shared_ptr &other, const pointer &p)
- : m_pn(other.m_pn, p)
- {}
-
-
- template<class Y>
- shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r)
- : m_pn(r.m_pn)
- {}
-
-
- template<class Y>
- explicit shared_ptr(weak_ptr<Y, VoidAllocator, Deleter> const & r)
- : m_pn(r.m_pn)
- {}
-
-
-
- explicit shared_ptr(BOOST_RV_REF(shared_ptr) other)
- : m_pn()
- { this->swap(other); }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- template<class Y>
- shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::static_cast_tag)
- : m_pn( pointer(static_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
- , r.m_pn)
- {}
- template<class Y>
- shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::const_cast_tag)
- : m_pn( pointer(const_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
- , r.m_pn)
- {}
- template<class Y>
- shared_ptr(shared_ptr<Y, VoidAllocator, Deleter> const & r, ipcdetail::dynamic_cast_tag)
- : m_pn( pointer(dynamic_cast<T*>(ipcdetail::to_raw_pointer(r.m_pn.to_raw_pointer())))
- , r.m_pn)
- {
- if(!m_pn.to_raw_pointer()){
- m_pn = ipcdetail::shared_count<T, VoidAllocator, Deleter>();
- }
- }
- #endif
-
-
- template<class Y>
- shared_ptr & operator=(shared_ptr<Y, VoidAllocator, Deleter> const & r)
- {
- m_pn = r.m_pn;
- return *this;
- }
-
-
- shared_ptr & operator=(BOOST_COPY_ASSIGN_REF(shared_ptr) r)
- {
- m_pn = r.m_pn;
- return *this;
- }
-
-
- shared_ptr & operator=(BOOST_RV_REF(shared_ptr) other)
- {
- this_type(other).swap(*this);
- return *this;
- }
-
-
- void reset()
- {
- this_type().swap(*this);
- }
-
-
- template<class Pointer>
- void reset(const Pointer &p, const VoidAllocator &a = VoidAllocator(), const Deleter &d = Deleter())
- {
-
-
- typedef typename boost::intrusive::
- pointer_traits<Pointer>::template
- rebind_pointer<T>::type ParameterPointer;
- BOOST_STATIC_ASSERT((ipcdetail::is_same<pointer, ParameterPointer>::value) ||
- (ipcdetail::is_pointer<Pointer>::value));
- this_type(p, a, d).swap(*this);
- }
- template<class Y>
- void reset(shared_ptr<Y, VoidAllocator, Deleter> const & r, const pointer &p)
- {
- this_type(r, p).swap(*this);
- }
-
-
- reference operator* () const
- { BOOST_ASSERT(m_pn.to_raw_pointer() != 0); return *m_pn.to_raw_pointer(); }
-
-
- pointer operator-> () const
- { BOOST_ASSERT(m_pn.to_raw_pointer() != 0); return m_pn.to_raw_pointer(); }
-
-
- pointer get() const
- { return m_pn.to_raw_pointer(); }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
-
- void unspecified_bool_type_func() const {}
- typedef void (this_type::*unspecified_bool_type)() const;
- operator unspecified_bool_type() const
- { return !m_pn.to_raw_pointer() ? 0 : &this_type::unspecified_bool_type_func; }
- #endif
-
-
- bool operator! () const
- { return !m_pn.to_raw_pointer(); }
-
-
- bool unique() const
- { return m_pn.unique(); }
-
-
-
-
-
- long use_count() const
- { return m_pn.use_count(); }
-
-
- void swap(shared_ptr<T, VoidAllocator, Deleter> & other)
- { m_pn.swap(other.m_pn); }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- template<class T2, class A2, class Deleter2>
- bool _internal_less(shared_ptr<T2, A2, Deleter2> const & rhs) const
- { return m_pn < rhs.m_pn; }
- const_deleter_pointer get_deleter() const
- { return m_pn.get_deleter(); }
- private:
- template<class T2, class A2, class Deleter2> friend class shared_ptr;
- template<class T2, class A2, class Deleter2> friend class weak_ptr;
- ipcdetail::shared_count<T, VoidAllocator, Deleter> m_pn;
- #endif
- };
- template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
- bool operator==(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
- { return a.get() == b.get(); }
- template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
- bool operator!=(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
- { return a.get() != b.get(); }
- template<class T, class VoidAllocator, class Deleter, class U, class VoidAllocator2, class Deleter2> inline
- bool operator<(shared_ptr<T, VoidAllocator, Deleter> const & a, shared_ptr<U, VoidAllocator2, Deleter2> const & b)
- { return a._internal_less(b); }
- template<class T, class VoidAllocator, class Deleter> inline
- void swap(shared_ptr<T, VoidAllocator, Deleter> & a, shared_ptr<T, VoidAllocator, Deleter> & b)
- { a.swap(b); }
- template<class T, class VoidAllocator, class Deleter, class U> inline
- shared_ptr<T, VoidAllocator, Deleter> static_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
- { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::static_cast_tag()); }
- template<class T, class VoidAllocator, class Deleter, class U> inline
- shared_ptr<T, VoidAllocator, Deleter> const_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
- { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::const_cast_tag()); }
- template<class T, class VoidAllocator, class Deleter, class U> inline
- shared_ptr<T, VoidAllocator, Deleter> dynamic_pointer_cast(shared_ptr<U, VoidAllocator, Deleter> const & r)
- { return shared_ptr<T, VoidAllocator, Deleter>(r, ipcdetail::dynamic_cast_tag()); }
- template<class T, class VoidAllocator, class Deleter> inline
- T * to_raw_pointer(shared_ptr<T, VoidAllocator, Deleter> const & p)
- { return p.get(); }
- template<class E, class T, class Y, class VoidAllocator, class Deleter> inline
- std::basic_ostream<E, T> & operator<<
- (std::basic_ostream<E, T> & os, shared_ptr<Y, VoidAllocator, Deleter> const & p)
- { os << p.get(); return os; }
- template<class T, class ManagedMemory>
- struct managed_shared_ptr
- {
- typedef typename ManagedMemory::template allocator<void>::type void_allocator;
- typedef typename ManagedMemory::template deleter<T>::type deleter;
- typedef shared_ptr< T, void_allocator, deleter> type;
- };
- template<class T, class ManagedMemory>
- inline typename managed_shared_ptr<T, ManagedMemory>::type
- make_managed_shared_ptr(T *constructed_object, ManagedMemory &managed_memory)
- {
- return typename managed_shared_ptr<T, ManagedMemory>::type
- ( constructed_object
- , managed_memory.template get_allocator<void>()
- , managed_memory.template get_deleter<T>()
- );
- }
- template<class T, class ManagedMemory>
- inline typename managed_shared_ptr<T, ManagedMemory>::type
- make_managed_shared_ptr(T *constructed_object, ManagedMemory &managed_memory, const std::nothrow_t &)
- {
- BOOST_TRY{
- return typename managed_shared_ptr<T, ManagedMemory>::type
- ( constructed_object
- , managed_memory.template get_allocator<void>()
- , managed_memory.template get_deleter<T>()
- );
- }
- BOOST_CATCH(...){
- return typename managed_shared_ptr<T, ManagedMemory>::type();
- } BOOST_CATCH_END
- }
- }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- #if defined(_MSC_VER) && (_MSC_VER < 1400)
- template<class T, class VoidAllocator, class Deleter> inline
- T * to_raw_pointer(boost::interprocess::shared_ptr<T, VoidAllocator, Deleter> const & p)
- { return p.get(); }
- #endif
- #endif
- }
- #include <boost/interprocess/detail/config_end.hpp>
- #endif
|