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.)
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.
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
17 #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
18 #define BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
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>
30 #if defined(BOOST_MSVC)
32 #pragma warning(disable:4521) // Multiple copy constuctors.
35 namespace boost { namespace ptr_container_detail {
40 move_ptrs::default_deleter<T> >
45 typedef typename remove_bounds<T>::type element_type;
46 typedef Deleter deleter_type;
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;
55 typedef typename impl_type::second_reference deleter_reference;
56 typedef typename impl_type::second_const_reference deleter_const_reference;
60 static_move_ptr() : impl_(0) { }
62 static_move_ptr(const static_move_ptr& p)
63 : impl_(p.get(), p.get_deleter())
65 const_cast<static_move_ptr&>(p).release();
68 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
69 static_move_ptr( const move_ptrs::move_source<static_move_ptr<T,Deleter> >& src )
71 static_move_ptr( const move_ptrs::move_source<static_move_ptr>& src )
73 : impl_(src.ptr().get(), src.ptr().get_deleter())
79 explicit static_move_ptr(TT* tt)
80 : impl_(tt, Deleter())
85 ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); }
89 static_move_ptr& operator=(static_move_ptr rhs)
95 // Smart pointer interface
97 element_type* get() const { return ptr(); }
99 element_type& operator*()
101 /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
104 const element_type& operator*() const
106 /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
109 element_type* operator->()
111 /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
114 const element_type* operator->() const
116 /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
120 element_type* release()
122 element_type* result = ptr();
129 if (ptr()) get_deleter()(ptr());
133 template<typename TT>
136 static_move_ptr(tt).swap(*this);
139 template<typename TT, typename DD>
140 void reset(TT* tt, DD dd)
142 static_move_ptr(tt, dd).swap(*this);
145 operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; }
147 void swap(static_move_ptr& p) { impl_.swap(p.impl_); }
149 deleter_reference get_deleter() { return impl_.second(); }
151 deleter_const_reference get_deleter() const { return impl_.second(); }
153 template<typename TT, typename DD>
154 void check(const static_move_ptr<TT, DD>& ptr)
156 typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible;
157 BOOST_STATIC_ASSERT(convertible::value);
160 #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE)
161 // give up on this behavior
164 template<typename Ptr> struct cant_move_from_const;
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;
171 template<typename Ptr>
172 static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0);
176 static_move_ptr(static_move_ptr&);
180 template<typename TT, typename DD>
181 static_move_ptr( static_move_ptr<TT, DD>&,
183 move_ptrs::enable_if_convertible<
184 TT, T, static_move_ptr&
185 >::type::type* = 0 );
187 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE
189 //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
190 // template<typename TT, typename DD>
191 // friend class static_move_ptr;
195 typename impl_type::first_reference
196 ptr() { return impl_.first(); }
198 typename impl_type::first_const_reference
199 ptr() const { return impl_.first(); }
204 } // namespace ptr_container_detail
205 } // End namespace boost.
207 #if defined(BOOST_MSVC)
208 #pragma warning(pop) // #pragma warning(disable:4251)
211 #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED