Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // This file is the adaptation for Interprocess of boost/scoped_ptr.hpp
|
Chris@16
|
4 //
|
Chris@16
|
5 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
Chris@16
|
6 // (C) Copyright Peter Dimov 2001, 2002
|
Chris@16
|
7 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
|
Chris@16
|
8 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //
|
Chris@16
|
11 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
12 //
|
Chris@16
|
13 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
|
Chris@16
|
16 #define BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
|
Chris@16
|
17
|
Chris@101
|
18 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
19 # include <boost/config.hpp>
|
Chris@101
|
20 #endif
|
Chris@101
|
21 #
|
Chris@101
|
22 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
23 # pragma once
|
Chris@101
|
24 #endif
|
Chris@101
|
25
|
Chris@16
|
26 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
28 #include <boost/interprocess/detail/pointer_type.hpp>
|
Chris@16
|
29 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
30 #include <boost/assert.hpp>
|
Chris@101
|
31 #include <boost/move/adl_move_swap.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 //!\file
|
Chris@16
|
34 //!Describes the smart pointer scoped_ptr
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost {
|
Chris@16
|
37 namespace interprocess {
|
Chris@16
|
38
|
Chris@16
|
39 //!scoped_ptr stores a pointer to a dynamically allocated object.
|
Chris@16
|
40 //!The object pointed to is guaranteed to be deleted, either on destruction
|
Chris@16
|
41 //!of the scoped_ptr, or via an explicit reset. The user can avoid this
|
Chris@16
|
42 //!deletion using release().
|
Chris@16
|
43 //!scoped_ptr is parameterized on T (the type of the object pointed to) and
|
Chris@16
|
44 //!Deleter (the functor to be executed to delete the internal pointer).
|
Chris@16
|
45 //!The internal pointer will be of the same pointer type as typename
|
Chris@16
|
46 //!Deleter::pointer type (that is, if typename Deleter::pointer is
|
Chris@16
|
47 //!offset_ptr<void>, the internal pointer will be offset_ptr<T>).
|
Chris@16
|
48 template<class T, class Deleter>
|
Chris@16
|
49 class scoped_ptr
|
Chris@16
|
50 : private Deleter
|
Chris@16
|
51 {
|
Chris@101
|
52 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
53 scoped_ptr(scoped_ptr const &);
|
Chris@16
|
54 scoped_ptr & operator=(scoped_ptr const &);
|
Chris@16
|
55
|
Chris@16
|
56 typedef scoped_ptr<T, Deleter> this_type;
|
Chris@16
|
57 typedef typename ipcdetail::add_reference<T>::type reference;
|
Chris@101
|
58 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
59
|
Chris@16
|
60 public:
|
Chris@16
|
61
|
Chris@16
|
62 typedef T element_type;
|
Chris@16
|
63 typedef Deleter deleter_type;
|
Chris@16
|
64 typedef typename ipcdetail::pointer_type<T, Deleter>::type pointer;
|
Chris@16
|
65
|
Chris@16
|
66 //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d.
|
Chris@16
|
67 //!Does not throw.
|
Chris@16
|
68 explicit scoped_ptr(const pointer &p = 0, const Deleter &d = Deleter())
|
Chris@16
|
69 : Deleter(d), m_ptr(p) // throws if pointer/Deleter copy ctor throws
|
Chris@16
|
70 {}
|
Chris@16
|
71
|
Chris@16
|
72 //!If the stored pointer is not 0, destroys the object pointed to by the stored pointer.
|
Chris@16
|
73 //!calling the operator() of the stored deleter. Never throws
|
Chris@16
|
74 ~scoped_ptr()
|
Chris@16
|
75 {
|
Chris@16
|
76 if(m_ptr){
|
Chris@16
|
77 Deleter &del = static_cast<Deleter&>(*this);
|
Chris@16
|
78 del(m_ptr);
|
Chris@16
|
79 }
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 //!Deletes the object pointed to by the stored pointer and then
|
Chris@16
|
83 //!stores a copy of p. Never throws
|
Chris@16
|
84 void reset(const pointer &p = 0) // never throws
|
Chris@16
|
85 { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p).swap(*this); }
|
Chris@16
|
86
|
Chris@16
|
87 //!Deletes the object pointed to by the stored pointer and then
|
Chris@16
|
88 //!stores a copy of p and a copy of d.
|
Chris@16
|
89 void reset(const pointer &p, const Deleter &d) // never throws
|
Chris@16
|
90 { BOOST_ASSERT(p == 0 || p != m_ptr); this_type(p, d).swap(*this); }
|
Chris@16
|
91
|
Chris@16
|
92 //!Assigns internal pointer as 0 and returns previous pointer. This will
|
Chris@16
|
93 //!avoid deletion on destructor
|
Chris@16
|
94 pointer release()
|
Chris@16
|
95 { pointer tmp(m_ptr); m_ptr = 0; return tmp; }
|
Chris@16
|
96
|
Chris@16
|
97 //!Returns a reference to the object pointed to by the stored pointer.
|
Chris@16
|
98 //!Never throws.
|
Chris@16
|
99 reference operator*() const
|
Chris@16
|
100 { BOOST_ASSERT(m_ptr != 0); return *m_ptr; }
|
Chris@16
|
101
|
Chris@16
|
102 //!Returns the internal stored pointer.
|
Chris@16
|
103 //!Never throws.
|
Chris@16
|
104 pointer &operator->()
|
Chris@16
|
105 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
|
Chris@16
|
106
|
Chris@16
|
107 //!Returns the internal stored pointer.
|
Chris@16
|
108 //!Never throws.
|
Chris@16
|
109 const pointer &operator->() const
|
Chris@16
|
110 { BOOST_ASSERT(m_ptr != 0); return m_ptr; }
|
Chris@16
|
111
|
Chris@16
|
112 //!Returns the stored pointer.
|
Chris@16
|
113 //!Never throws.
|
Chris@16
|
114 pointer & get()
|
Chris@16
|
115 { return m_ptr; }
|
Chris@16
|
116
|
Chris@16
|
117 //!Returns the stored pointer.
|
Chris@16
|
118 //!Never throws.
|
Chris@16
|
119 const pointer & get() const
|
Chris@16
|
120 { return m_ptr; }
|
Chris@16
|
121
|
Chris@16
|
122 typedef pointer this_type::*unspecified_bool_type;
|
Chris@16
|
123
|
Chris@16
|
124 //!Conversion to bool
|
Chris@16
|
125 //!Never throws
|
Chris@16
|
126 operator unspecified_bool_type() const
|
Chris@16
|
127 { return m_ptr == 0? 0: &this_type::m_ptr; }
|
Chris@16
|
128
|
Chris@16
|
129 //!Returns true if the stored pointer is 0.
|
Chris@16
|
130 //!Never throws.
|
Chris@16
|
131 bool operator! () const // never throws
|
Chris@16
|
132 { return m_ptr == 0; }
|
Chris@16
|
133
|
Chris@16
|
134 //!Exchanges the internal pointer and deleter with other scoped_ptr
|
Chris@16
|
135 //!Never throws.
|
Chris@16
|
136 void swap(scoped_ptr & b) // never throws
|
Chris@101
|
137 {
|
Chris@101
|
138 ::boost::adl_move_swap(static_cast<Deleter&>(*this), static_cast<Deleter&>(b));
|
Chris@101
|
139 ::boost::adl_move_swap(m_ptr, b.m_ptr);
|
Chris@101
|
140 }
|
Chris@16
|
141
|
Chris@101
|
142 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
143 private:
|
Chris@16
|
144 pointer m_ptr;
|
Chris@101
|
145 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
146 };
|
Chris@16
|
147
|
Chris@16
|
148 //!Exchanges the internal pointer and deleter with other scoped_ptr
|
Chris@16
|
149 //!Never throws.
|
Chris@16
|
150 template<class T, class D> inline
|
Chris@16
|
151 void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b)
|
Chris@16
|
152 { a.swap(b); }
|
Chris@16
|
153
|
Chris@16
|
154 //!Returns a copy of the stored pointer
|
Chris@16
|
155 //!Never throws
|
Chris@16
|
156 template<class T, class D> inline
|
Chris@16
|
157 typename scoped_ptr<T, D>::pointer to_raw_pointer(scoped_ptr<T, D> const & p)
|
Chris@16
|
158 { return p.get(); }
|
Chris@16
|
159
|
Chris@16
|
160 } // namespace interprocess
|
Chris@16
|
161
|
Chris@101
|
162 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
163
|
Chris@16
|
164 #if defined(_MSC_VER) && (_MSC_VER < 1400)
|
Chris@16
|
165 template<class T, class D> inline
|
Chris@16
|
166 T *to_raw_pointer(boost::interprocess::scoped_ptr<T, D> const & p)
|
Chris@16
|
167 { return p.get(); }
|
Chris@16
|
168 #endif
|
Chris@16
|
169
|
Chris@101
|
170 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
171
|
Chris@16
|
172 } // namespace boost
|
Chris@16
|
173
|
Chris@16
|
174 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
175
|
Chris@16
|
176 #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED
|