]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Thorsten Ottosen 2005. |
2 | // (C) Copyright Jonathan Turkanis 2004. | |
3 | // (C) Copyright Daniel Wallin 2004. | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) | |
6 | ||
7 | // Implementation of the move_ptr from the "Move Proposal" | |
8 | // (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm) | |
9 | // enhanced to support custom deleters and safe boolean conversions. | |
10 | // | |
11 | // The implementation is based on an implementation by Daniel Wallin, at | |
12 | // "http://aspn.activestate.com/ASPN/Mail/Message/Attachments/boost/ | |
13 | // 400DC271.1060903@student.umu.se/move_ptr.hpp". The current was adapted | |
14 | // by Jonathan Turkanis to incorporating ideas of Howard Hinnant and | |
15 | // Rani Sharoni. | |
16 | ||
17 | #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED | |
18 | #define BOOST_STATIC_MOVE_PTR_HPP_INCLUDED | |
19 | ||
20 | #include <boost/config.hpp> // Member template friends, put size_t in std. | |
21 | #include <cstddef> // size_t | |
22 | #include <boost/compressed_pair.hpp> | |
23 | #include <boost/ptr_container/detail/default_deleter.hpp> | |
24 | #include <boost/ptr_container/detail/is_convertible.hpp> | |
25 | #include <boost/ptr_container/detail/move.hpp> | |
26 | #include <boost/static_assert.hpp> | |
27 | #include <boost/type_traits/add_reference.hpp> | |
28 | #include <boost/type_traits/is_array.hpp> | |
29 | ||
30 | #if defined(BOOST_MSVC) | |
31 | #pragma warning(push) | |
32 | #pragma warning(disable:4521) // Multiple copy constuctors. | |
33 | #endif | |
34 | ||
35 | namespace boost { namespace ptr_container_detail { | |
36 | ||
37 | ||
38 | template< typename T, | |
39 | typename Deleter = | |
40 | move_ptrs::default_deleter<T> > | |
41 | class static_move_ptr | |
42 | { | |
43 | public: | |
44 | ||
45 | typedef typename remove_bounds<T>::type element_type; | |
46 | typedef Deleter deleter_type; | |
47 | ||
48 | private: | |
49 | ||
50 | struct safe_bool_helper { int x; }; | |
51 | typedef int safe_bool_helper::* safe_bool; | |
52 | typedef boost::compressed_pair<element_type*, Deleter> impl_type; | |
53 | ||
54 | public: | |
55 | typedef typename impl_type::second_reference deleter_reference; | |
56 | typedef typename impl_type::second_const_reference deleter_const_reference; | |
57 | ||
58 | // Constructors | |
59 | ||
60 | static_move_ptr() : impl_(0) { } | |
61 | ||
62 | static_move_ptr(const static_move_ptr& p) | |
63 | : impl_(p.get(), p.get_deleter()) | |
64 | { | |
65 | const_cast<static_move_ptr&>(p).release(); | |
66 | } | |
67 | ||
68 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) | |
69 | static_move_ptr( const move_ptrs::move_source<static_move_ptr<T,Deleter> >& src ) | |
70 | #else | |
71 | static_move_ptr( const move_ptrs::move_source<static_move_ptr>& src ) | |
72 | #endif | |
73 | : impl_(src.ptr().get(), src.ptr().get_deleter()) | |
74 | { | |
75 | src.ptr().release(); | |
76 | } | |
77 | ||
78 | template<typename TT> | |
79 | explicit static_move_ptr(TT* tt) | |
80 | : impl_(tt, Deleter()) | |
81 | { } | |
82 | ||
83 | // Destructor | |
84 | ||
85 | ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); } | |
86 | ||
87 | // Assignment | |
88 | ||
89 | static_move_ptr& operator=(static_move_ptr rhs) | |
90 | { | |
91 | rhs.swap(*this); | |
92 | return *this; | |
93 | } | |
94 | ||
95 | // Smart pointer interface | |
96 | ||
97 | element_type* get() const { return ptr(); } | |
98 | ||
99 | element_type& operator*() | |
100 | { | |
101 | /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr(); | |
102 | } | |
103 | ||
104 | const element_type& operator*() const | |
105 | { | |
106 | /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr(); | |
107 | } | |
108 | ||
109 | element_type* operator->() | |
110 | { | |
111 | /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr(); | |
112 | } | |
113 | ||
114 | const element_type* operator->() const | |
115 | { | |
116 | /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr(); | |
117 | } | |
118 | ||
119 | ||
120 | element_type* release() | |
121 | { | |
122 | element_type* result = ptr(); | |
123 | ptr() = 0; | |
124 | return result; | |
125 | } | |
126 | ||
127 | void reset() | |
128 | { | |
129 | if (ptr()) get_deleter()(ptr()); | |
130 | ptr() = 0; | |
131 | } | |
132 | ||
133 | template<typename TT> | |
134 | void reset(TT* tt) | |
135 | { | |
136 | static_move_ptr(tt).swap(*this); | |
137 | } | |
138 | ||
139 | template<typename TT, typename DD> | |
140 | void reset(TT* tt, DD dd) | |
141 | { | |
142 | static_move_ptr(tt, dd).swap(*this); | |
143 | } | |
144 | ||
145 | operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; } | |
146 | ||
147 | void swap(static_move_ptr& p) { impl_.swap(p.impl_); } | |
148 | ||
149 | deleter_reference get_deleter() { return impl_.second(); } | |
150 | ||
151 | deleter_const_reference get_deleter() const { return impl_.second(); } | |
152 | private: | |
153 | template<typename TT, typename DD> | |
154 | void check(const static_move_ptr<TT, DD>& ptr) | |
155 | { | |
156 | typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible; | |
157 | BOOST_STATIC_ASSERT(convertible::value); | |
158 | } | |
159 | ||
160 | #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE) | |
161 | // give up on this behavior | |
162 | #else | |
163 | ||
164 | template<typename Ptr> struct cant_move_from_const; | |
165 | ||
166 | template<typename TT, typename DD> | |
167 | struct cant_move_from_const< const static_move_ptr<TT, DD> > { | |
168 | typedef typename static_move_ptr<TT, DD>::error type; | |
169 | }; | |
170 | ||
171 | template<typename Ptr> | |
172 | static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0); | |
173 | ||
174 | ||
175 | public: | |
176 | static_move_ptr(static_move_ptr&); | |
177 | ||
178 | ||
179 | private: | |
180 | template<typename TT, typename DD> | |
181 | static_move_ptr( static_move_ptr<TT, DD>&, | |
182 | typename | |
183 | move_ptrs::enable_if_convertible< | |
184 | TT, T, static_move_ptr& | |
185 | >::type::type* = 0 ); | |
186 | ||
187 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE | |
188 | ||
189 | //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS | |
190 | // template<typename TT, typename DD> | |
191 | // friend class static_move_ptr; | |
192 | //#else | |
193 | public: | |
194 | //#endif | |
195 | typename impl_type::first_reference | |
196 | ptr() { return impl_.first(); } | |
197 | ||
198 | typename impl_type::first_const_reference | |
199 | ptr() const { return impl_.first(); } | |
200 | ||
201 | impl_type impl_; | |
202 | }; | |
203 | ||
204 | } // namespace ptr_container_detail | |
205 | } // End namespace boost. | |
206 | ||
207 | #if defined(BOOST_MSVC) | |
208 | #pragma warning(pop) // #pragma warning(disable:4251) | |
209 | #endif | |
210 | ||
211 | #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED |