]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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-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) | |
10 | // | |
11 | // See http://www.boost.org/libs/interprocess for documentation. | |
12 | // | |
13 | ////////////////////////////////////////////////////////////////////////////// | |
14 | ||
15 | #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED | |
16 | #define BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED | |
17 | ||
18 | #ifndef BOOST_CONFIG_HPP | |
19 | # include <boost/config.hpp> | |
20 | #endif | |
21 | # | |
22 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
23 | # pragma once | |
24 | #endif | |
25 | ||
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> | |
32 | ||
33 | //!\file | |
34 | //!Describes the smart pointer scoped_ptr | |
35 | ||
36 | namespace boost { | |
37 | namespace interprocess { | |
38 | ||
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> | |
49 | class scoped_ptr | |
50 | : private Deleter | |
51 | { | |
52 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
53 | scoped_ptr(scoped_ptr const &); | |
54 | scoped_ptr & operator=(scoped_ptr const &); | |
55 | ||
56 | typedef scoped_ptr<T, Deleter> this_type; | |
57 | typedef typename ipcdetail::add_reference<T>::type reference; | |
58 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
59 | ||
60 | public: | |
61 | ||
62 | typedef T element_type; | |
63 | typedef Deleter deleter_type; | |
64 | typedef typename ipcdetail::pointer_type<T, Deleter>::type pointer; | |
65 | ||
66 | //!Constructs a scoped_ptr, storing a copy of p(which can be 0) and d. | |
67 | //!Does not throw. | |
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 | |
70 | {} | |
71 | ||
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 | |
74 | ~scoped_ptr() | |
75 | { | |
76 | if(m_ptr){ | |
77 | Deleter &del = static_cast<Deleter&>(*this); | |
78 | del(m_ptr); | |
79 | } | |
80 | } | |
81 | ||
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); } | |
86 | ||
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); } | |
91 | ||
92 | //!Assigns internal pointer as 0 and returns previous pointer. This will | |
93 | //!avoid deletion on destructor | |
1e59de90 | 94 | pointer release() BOOST_NOEXCEPT |
7c673cae FG |
95 | { pointer tmp(m_ptr); m_ptr = 0; return tmp; } |
96 | ||
97 | //!Returns a reference to the object pointed to by the stored pointer. | |
98 | //!Never throws. | |
1e59de90 | 99 | reference operator*() const BOOST_NOEXCEPT |
7c673cae FG |
100 | { BOOST_ASSERT(m_ptr != 0); return *m_ptr; } |
101 | ||
102 | //!Returns the internal stored pointer. | |
103 | //!Never throws. | |
1e59de90 | 104 | pointer &operator->() BOOST_NOEXCEPT |
7c673cae FG |
105 | { BOOST_ASSERT(m_ptr != 0); return m_ptr; } |
106 | ||
107 | //!Returns the internal stored pointer. | |
108 | //!Never throws. | |
1e59de90 | 109 | const pointer &operator->() const BOOST_NOEXCEPT |
7c673cae FG |
110 | { BOOST_ASSERT(m_ptr != 0); return m_ptr; } |
111 | ||
112 | //!Returns the stored pointer. | |
113 | //!Never throws. | |
1e59de90 | 114 | pointer & get() BOOST_NOEXCEPT |
7c673cae FG |
115 | { return m_ptr; } |
116 | ||
117 | //!Returns the stored pointer. | |
118 | //!Never throws. | |
1e59de90 | 119 | const pointer & get() const BOOST_NOEXCEPT |
7c673cae FG |
120 | { return m_ptr; } |
121 | ||
122 | typedef pointer this_type::*unspecified_bool_type; | |
123 | ||
124 | //!Conversion to bool | |
125 | //!Never throws | |
1e59de90 | 126 | operator unspecified_bool_type() const BOOST_NOEXCEPT |
7c673cae FG |
127 | { return m_ptr == 0? 0: &this_type::m_ptr; } |
128 | ||
129 | //!Returns true if the stored pointer is 0. | |
130 | //!Never throws. | |
1e59de90 | 131 | bool operator! () const BOOST_NOEXCEPT // never throws |
7c673cae FG |
132 | { return m_ptr == 0; } |
133 | ||
134 | //!Exchanges the internal pointer and deleter with other scoped_ptr | |
135 | //!Never throws. | |
1e59de90 | 136 | void swap(scoped_ptr & b) BOOST_NOEXCEPT // never throws |
7c673cae FG |
137 | { |
138 | ::boost::adl_move_swap(static_cast<Deleter&>(*this), static_cast<Deleter&>(b)); | |
139 | ::boost::adl_move_swap(m_ptr, b.m_ptr); | |
140 | } | |
141 | ||
142 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
143 | private: | |
144 | pointer m_ptr; | |
145 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
146 | }; | |
147 | ||
148 | //!Exchanges the internal pointer and deleter with other scoped_ptr | |
149 | //!Never throws. | |
150 | template<class T, class D> inline | |
1e59de90 | 151 | void swap(scoped_ptr<T, D> & a, scoped_ptr<T, D> & b) BOOST_NOEXCEPT |
7c673cae FG |
152 | { a.swap(b); } |
153 | ||
154 | //!Returns a copy of the stored pointer | |
155 | //!Never throws | |
156 | template<class T, class D> inline | |
157 | typename scoped_ptr<T, D>::pointer to_raw_pointer(scoped_ptr<T, D> const & p) | |
158 | { return p.get(); } | |
159 | ||
160 | } // namespace interprocess | |
161 | ||
162 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
163 | ||
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) | |
167 | { return p.get(); } | |
168 | #endif | |
169 | ||
170 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
171 | ||
172 | } // namespace boost | |
173 | ||
174 | #include <boost/interprocess/detail/config_end.hpp> | |
175 | ||
176 | #endif // #ifndef BOOST_INTERPROCESS_SCOPED_PTR_HPP_INCLUDED |