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. 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 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/pointer_type.hpp>
21 #include <boost/assert.hpp>
22 #include <boost/pointer_to_other.hpp>
25 //!Describes the smart pointer scoped_ptr
28 namespace interprocess
{
30 //!scoped_ptr stores a pointer to a dynamically allocated object.
31 //!The object pointed to is guaranteed to be deleted, either on destruction
32 //!of the scoped_ptr, or via an explicit reset. The user can avoid this
33 //!deletion using release().
34 //!scoped_ptr is parameterized on T (the type of the object pointed to) and
35 //!Deleter (the functor to be executed to delete the internal pointer).
36 //!The internal pointer will be of the same pointer type as typename
37 //!Deleter::pointer type (that is, if typename Deleter::pointer is
38 //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
39 template<class T
, class Deleter
>
44 scoped_ptr(scoped_ptr
const &);
45 scoped_ptr
& operator=(scoped_ptr
const &);
47 typedef scoped_ptr
<T
, Deleter
> this_type
;
48 typedef typename
detail::add_reference
<T
>::type reference
;
53 typedef T element_type
;
54 typedef Deleter deleter_type
;
55 typedef typename
detail::pointer_type
<T
, Deleter
>::type pointer
;
57 //!Provides the type of the internal stored pointer
58 // typedef typename boost::pointer_to_other
59 // <typename Deleter::pointer, T>::type pointer;
61 //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d.
63 explicit scoped_ptr(const pointer
&p
= 0, const Deleter
&d
= Deleter())
64 : Deleter(d
), m_ptr(p
) // throws if pointer/Deleter copy ctor throws
67 //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer.
68 //!calling the operator() of the stored deleter. Never throws
72 Deleter
&del
= static_cast<Deleter
&>(*this);
77 //!Deletes the object pointed to by the stored pointer and then
78 //!stores a copy of p. Never throws
79 void reset(const pointer
&p
= 0) // never throws
80 { BOOST_ASSERT(p
== 0 || p
!= m_ptr
); this_type(p
).swap(*this); }
82 //!Deletes the object pointed to by the stored pointer and then
83 //!stores a copy of p and a copy of d.
84 void reset(const pointer
&p
, const Deleter
&d
) // never throws
85 { BOOST_ASSERT(p
== 0 || p
!= m_ptr
); this_type(p
).swap(*this); }
87 //!Assigns internal pointer as 0 and returns previous pointer. This will
88 //!avoid deletion on destructor
90 { pointer
tmp(m_ptr
); m_ptr
= 0; return tmp
; }
92 //!Returns a reference to the object pointed to by the stored pointer.
94 reference
operator*() const
95 { BOOST_ASSERT(m_ptr
!= 0); return *m_ptr
; }
97 //!Returns the internal stored pointer.
100 { BOOST_ASSERT(m_ptr
!= 0); return m_ptr
; }
102 //!Returns the internal stored pointer.
104 const pointer
&operator->() const
105 { BOOST_ASSERT(m_ptr
!= 0); return m_ptr
; }
107 //!Returns the stored pointer.
112 //!Returns the stored pointer.
114 const pointer
& get() const
117 typedef pointer
this_type::*unspecified_bool_type
;
119 //!Conversion to bool
121 operator unspecified_bool_type() const
122 { return m_ptr
== 0? 0: &this_type::m_ptr
; }
124 //!Returns true if the stored pointer is 0.
126 bool operator! () const // never throws
127 { return m_ptr
== 0; }
129 //!Exchanges the internal pointer and deleter with other scoped_ptr
131 void swap(scoped_ptr
& b
) // never throws
132 { detail::do_swap
<Deleter
>(*this, b
); detail::do_swap(m_ptr
, b
.m_ptr
); }
140 //!Exchanges the internal pointer and deleter with other scoped_ptr
142 template<class T
, class D
> inline
143 void swap(scoped_ptr
<T
, D
> & a
, scoped_ptr
<T
, D
> & b
)
146 //!Returns a copy of the stored pointer
148 template<class T
, class D
> inline
149 typename scoped_ptr
<T
, D
>::pointer
get_pointer(scoped_ptr
<T
, D
> const & p
)
152 } // namespace interprocess
156 #if defined(_MSC_VER) && (_MSC_VER < 1400)
157 template<class T
, class D
> inline
158 T
*get_pointer(boost::interprocess::scoped_ptr
<T
, D
> const & p
)
166 #include <boost/interprocess/detail/config_end.hpp>
168 #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED