1 //////////////////////////////////////////////////////////////////////////////
3 // This file is the adaptation for Interprocess of boost/scoped_ptr.hpp
5 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
6 // (C) Copyright Peter Dimov 2001, 2002
7 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
8 // Software License, Version 1.0. (See accompanying file
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // See http://www.boost.org/libs/interprocess for documentation.
13 //////////////////////////////////////////////////////////////////////////////
15 #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
16 #define BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
18 #ifndef BOOST_CONFIG_HPP
19 # include <boost/config.hpp>
22 #if defined(BOOST_HAS_PRAGMA_ONCE)
26 #include <boost/interprocess/detail/config_begin.hpp>
27 #include <boost/interprocess/detail/workaround.hpp>
28 #include <boost/interprocess/detail/pointer_type.hpp>
29 #include <boost/interprocess/detail/utilities.hpp>
30 #include <boost/assert.hpp>
31 #include <boost/move/adl_move_swap.hpp>
34 //!Describes the smart pointer scoped_ptr
37 namespace interprocess {
39 //!scoped_ptr stores a pointer to a dynamically allocated object.
40 //!The object pointed to is guaranteed to be deleted, either on destruction
41 //!of the scoped_ptr, or via an explicit reset. The user can avoid this
42 //!deletion using release().
43 //!scoped_ptr is parameterized on T (the type of the object pointed to) and
44 //!Deleter (the functor to be executed to delete the internal pointer).
45 //!The internal pointer will be of the same pointer type as typename
46 //!Deleter::pointer type (that is, if typename Deleter::pointer is
47 //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
48 template<class T, class Deleter>
52 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
53 scoped_ptr(scoped_ptr const &);
54 scoped_ptr & operator=(scoped_ptr const &);
56 typedef scoped_ptr<T, Deleter> this_type;
57 typedef typename ipcdetail::add_reference<T>::type reference;
58 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
62 typedef T element_type;
63 typedef Deleter deleter_type;
64 typedef typename ipcdetail::pointer_type<T, Deleter>::type pointer;
66 //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d.
68 explicit scoped_ptr(const pointer &p = 0, const Deleter &d = Deleter())
69 : Deleter(d), m_ptr(p) // throws if pointer/Deleter copy ctor throws
72 //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer.
73 //!calling the operator() of the stored deleter. Never throws
77 Deleter &del = static_cast<Deleter&>(*this);
82 //!Deletes the object pointed to by the stored pointer and then
83 //!stores a copy of p. Never throws
84 void reset(const pointer &p = 0) // never throws
85 { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p).swap(*this); }
87 //!Deletes the object pointed to by the stored pointer and then
88 //!stores a copy of p and a copy of d.
89 void reset(const pointer &p, const Deleter &d) // never throws
90 { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p, d).swap(*this); }
92 //!Assigns internal pointer as 0 and returns previous pointer. This will
93 //!avoid deletion on destructor
95 { pointer tmp(m_ptr); m_ptr = 0; return tmp; }
97 //!Returns a reference to the object pointed to by the stored pointer.
99 reference operator*() const
100 { BOOST_ASSERT(m_ptr != 0); return *m_ptr; }
102 //!Returns the internal stored pointer.
104 pointer &operator->()
105 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
107 //!Returns the internal stored pointer.
109 const pointer &operator->() const
110 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
112 //!Returns the stored pointer.
117 //!Returns the stored pointer.
119 const pointer & get() const
122 typedef pointer this_type::*unspecified_bool_type;
124 //!Conversion to bool
126 operator unspecified_bool_type() const
127 { return m_ptr == 0? 0: &this_type::m_ptr; }
129 //!Returns true if the stored pointer is 0.
131 bool operator! () const // never throws
132 { return m_ptr == 0; }
134 //!Exchanges the internal pointer and deleter with other scoped_ptr
136 void swap(scoped_ptr & b) // never throws
138 ::boost::adl_move_swap(static_cast<Deleter&>(*this), static_cast<Deleter&>(b));
139 ::boost::adl_move_swap(m_ptr, b.m_ptr);
142 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
145 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
148 //!Exchanges the internal pointer and deleter with other scoped_ptr
150 template<class T, class D> inline
151 void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b)
154 //!Returns a copy of the stored pointer
156 template<class T, class D> inline
157 typename scoped_ptr<T, D>::pointer to_raw_pointer(scoped_ptr<T, D> const & p)
160 } // namespace interprocess
162 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
164 #if defined(_MSC_VER) && (_MSC_VER < 1400)
165 template<class T, class D> inline
166 T *to_raw_pointer(boost::interprocess::scoped_ptr<T, D> const & p)
170 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
174 #include <boost/interprocess/detail/config_end.hpp>
176 #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED