Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // This file is the adaptation for Interprocess of boost/scoped_ptr.hpp Chris@16: // Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // (C) Copyright Peter Dimov 2001, 2002 Chris@16: // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED Chris@16: #define BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes the smart pointer scoped_ptr Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: //!scoped_ptr stores a pointer to a dynamically allocated object. Chris@16: //!The object pointed to is guaranteed to be deleted, either on destruction Chris@16: //!of the scoped_ptr, or via an explicit reset. The user can avoid this Chris@16: //!deletion using release(). Chris@16: //!scoped_ptr is parameterized on T (the type of the object pointed to) and Chris@16: //!Deleter (the functor to be executed to delete the internal pointer). Chris@16: //!The internal pointer will be of the same pointer type as typename Chris@16: //!Deleter::pointer type (that is, if typename Deleter::pointer is Chris@16: //!offset_ptr, the internal pointer will be offset_ptr). Chris@16: template Chris@16: class scoped_ptr Chris@16: : private Deleter Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: scoped_ptr(scoped_ptr const &); Chris@16: scoped_ptr & operator=(scoped_ptr const &); Chris@16: Chris@16: typedef scoped_ptr this_type; Chris@16: typedef typename ipcdetail::add_reference::type reference; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: typedef Deleter deleter_type; Chris@16: typedef typename ipcdetail::pointer_type::type pointer; Chris@16: Chris@16: //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d. Chris@16: //!Does not throw. Chris@16: explicit scoped_ptr(const pointer &p = 0, const Deleter &d = Deleter()) Chris@16: : Deleter(d), m_ptr(p) // throws if pointer/Deleter copy ctor throws Chris@16: {} Chris@16: Chris@16: //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer. Chris@16: //!calling the operator() of the stored deleter. Never throws Chris@16: ~scoped_ptr() Chris@16: { Chris@16: if(m_ptr){ Chris@16: Deleter &del = static_cast(*this); Chris@16: del(m_ptr); Chris@16: } Chris@16: } Chris@16: Chris@16: //!Deletes the object pointed to by the stored pointer and then Chris@16: //!stores a copy of p. Never throws Chris@16: void reset(const pointer &p = 0) // never throws Chris@16: { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p).swap(*this); } Chris@16: Chris@16: //!Deletes the object pointed to by the stored pointer and then Chris@16: //!stores a copy of p and a copy of d. Chris@16: void reset(const pointer &p, const Deleter &d) // never throws Chris@16: { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p, d).swap(*this); } Chris@16: Chris@16: //!Assigns internal pointer as 0 and returns previous pointer. This will Chris@16: //!avoid deletion on destructor Chris@16: pointer release() Chris@16: { pointer tmp(m_ptr); m_ptr = 0; return tmp; } Chris@16: Chris@16: //!Returns a reference to the object pointed to by the stored pointer. Chris@16: //!Never throws. Chris@16: reference operator*() const Chris@16: { BOOST_ASSERT(m_ptr != 0); return *m_ptr; } Chris@16: Chris@16: //!Returns the internal stored pointer. Chris@16: //!Never throws. Chris@16: pointer &operator->() Chris@16: { BOOST_ASSERT(m_ptr != 0); return m_ptr; } Chris@16: Chris@16: //!Returns the internal stored pointer. Chris@16: //!Never throws. Chris@16: const pointer &operator->() const Chris@16: { BOOST_ASSERT(m_ptr != 0); return m_ptr; } Chris@16: Chris@16: //!Returns the stored pointer. Chris@16: //!Never throws. Chris@16: pointer & get() Chris@16: { return m_ptr; } Chris@16: Chris@16: //!Returns the stored pointer. Chris@16: //!Never throws. Chris@16: const pointer & get() const Chris@16: { return m_ptr; } Chris@16: Chris@16: typedef pointer this_type::*unspecified_bool_type; Chris@16: Chris@16: //!Conversion to bool Chris@16: //!Never throws Chris@16: operator unspecified_bool_type() const Chris@16: { return m_ptr == 0? 0: &this_type::m_ptr; } Chris@16: Chris@16: //!Returns true if the stored pointer is 0. Chris@16: //!Never throws. Chris@16: bool operator! () const // never throws Chris@16: { return m_ptr == 0; } Chris@16: Chris@16: //!Exchanges the internal pointer and deleter with other scoped_ptr Chris@16: //!Never throws. Chris@16: void swap(scoped_ptr & b) // never throws Chris@101: { Chris@101: ::boost::adl_move_swap(static_cast(*this), static_cast(b)); Chris@101: ::boost::adl_move_swap(m_ptr, b.m_ptr); Chris@101: } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: pointer m_ptr; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: //!Exchanges the internal pointer and deleter with other scoped_ptr Chris@16: //!Never throws. Chris@16: template inline Chris@16: void swap(scoped_ptr & a, scoped_ptr & b) Chris@16: { a.swap(b); } Chris@16: Chris@16: //!Returns a copy of the stored pointer Chris@16: //!Never throws Chris@16: template inline Chris@16: typename scoped_ptr::pointer to_raw_pointer(scoped_ptr const & p) Chris@16: { return p.get(); } Chris@16: Chris@16: } // namespace interprocess Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER < 1400) Chris@16: template inline Chris@16: T *to_raw_pointer(boost::interprocess::scoped_ptr const & p) Chris@16: { return p.get(); } Chris@16: #endif Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED