fix doc example typo
[boost.git] / boost / interprocess / smart_ptr / scoped_ptr.hpp
blob74fe1d6395d09c718cdc4f3ceb3e0599cb662bec
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // This file is the adaptation for Interprocess of boost/scoped_ptr.hpp
4 //
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>
24 //!\file
25 //!Describes the smart pointer scoped_ptr
27 namespace boost {
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>
40 class scoped_ptr
41 : private Deleter
43 /// @cond
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;
49 /// @endcond
51 public:
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.
62 //!Does not throw.
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
69 ~scoped_ptr()
71 if(m_ptr){
72 Deleter &del = static_cast<Deleter&>(*this);
73 del(m_ptr);
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
89 pointer release()
90 { pointer tmp(m_ptr); m_ptr = 0; return tmp; }
92 //!Returns a reference to the object pointed to by the stored pointer.
93 //!Never throws.
94 reference operator*() const
95 { BOOST_ASSERT(m_ptr != 0); return *m_ptr; }
97 //!Returns the internal stored pointer.
98 //!Never throws.
99 pointer &operator->()
100 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
102 //!Returns the internal stored pointer.
103 //!Never throws.
104 const pointer &operator->() const
105 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
107 //!Returns the stored pointer.
108 //!Never throws.
109 pointer & get()
110 { return m_ptr; }
112 //!Returns the stored pointer.
113 //!Never throws.
114 const pointer & get() const
115 { return m_ptr; }
117 typedef pointer this_type::*unspecified_bool_type;
119 //!Conversion to bool
120 //!Never throws
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.
125 //!Never throws.
126 bool operator! () const // never throws
127 { return m_ptr == 0; }
129 //!Exchanges the internal pointer and deleter with other scoped_ptr
130 //!Never throws.
131 void swap(scoped_ptr & b) // never throws
132 { detail::do_swap<Deleter>(*this, b); detail::do_swap(m_ptr, b.m_ptr); }
134 /// @cond
135 private:
136 pointer m_ptr;
137 /// @endcond
140 //!Exchanges the internal pointer and deleter with other scoped_ptr
141 //!Never throws.
142 template<class T, class D> inline
143 void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b)
144 { a.swap(b); }
146 //!Returns a copy of the stored pointer
147 //!Never throws
148 template<class T, class D> inline
149 typename scoped_ptr<T, D>::pointer get_pointer(scoped_ptr<T, D> const & p)
150 { return p.get(); }
152 } // namespace interprocess
154 /// @cond
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)
159 { return p.get(); }
160 #endif
162 /// @endcond
164 } // namespace boost
166 #include <boost/interprocess/detail/config_end.hpp>
168 #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED