1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // This file is the adaptation for Interprocess of boost/enable_shared_from_this.hpp 4 // 5 // (C) Copyright Peter Dimov 2002 6 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost 7 // Software License, Version 1.0. (See accompanying file 8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9 // 10 // See http://www.boost.org/libs/interprocess for documentation. 11 // 12 ////////////////////////////////////////////////////////////////////////////// 13 14 #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED 15 #define BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED 16 17 #ifndef BOOST_CONFIG_HPP 18 # include <boost/config.hpp> 19 #endif 20 # 21 #if defined(BOOST_HAS_PRAGMA_ONCE) 22 # pragma once 23 #endif 24 25 #include <boost/interprocess/detail/config_begin.hpp> 26 #include <boost/interprocess/detail/workaround.hpp> 27 28 #include <boost/assert.hpp> 29 #include <boost/interprocess/smart_ptr/weak_ptr.hpp> 30 #include <boost/interprocess/smart_ptr/shared_ptr.hpp> 31 32 //!\file 33 //!Describes an utility to form a shared pointer from this 34 35 namespace boost{ 36 namespace interprocess{ 37 38 //!This class is used as a base class that allows a shared_ptr to the current 39 //!object to be obtained from within a member function. 40 //!enable_shared_from_this defines two member functions called shared_from_this 41 //!that return a shared_ptr<T> and shared_ptr<T const>, depending on constness, to this. 42 template<class T, class A, class D> 43 class enable_shared_from_this 44 { 45 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 46 protected: enable_shared_from_this()47 enable_shared_from_this() 48 {} 49 enable_shared_from_this(enable_shared_from_this const &)50 enable_shared_from_this(enable_shared_from_this const &) 51 {} 52 operator =(enable_shared_from_this const &)53 enable_shared_from_this & operator=(enable_shared_from_this const &) 54 { return *this; } 55 ~enable_shared_from_this()56 ~enable_shared_from_this() 57 {} 58 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 59 60 public: shared_from_this()61 shared_ptr<T, A, D> shared_from_this() 62 { 63 shared_ptr<T, A, D> p(_internal_weak_this); 64 BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this); 65 return p; 66 } 67 shared_from_this() const68 shared_ptr<T const, A, D> shared_from_this() const 69 { 70 shared_ptr<T const, A, D> p(_internal_weak_this); 71 BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this); 72 return p; 73 } 74 75 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 76 typedef T element_type; 77 mutable weak_ptr<element_type, A, D> _internal_weak_this; 78 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 79 }; 80 81 } // namespace interprocess 82 } // namespace boost 83 84 #include <boost/interprocess/detail/config_end.hpp> 85 86 #endif // #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED 87 88