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://std.dkuug.dk/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 <algorithm> // swap.
22 #include <cstddef> // size_t
23 #include <boost/compressed_pair.hpp>
24 #include <boost/ptr_container/detail/default_deleter.hpp>
25 #include <boost/ptr_container/detail/is_convertible.hpp>
26 #include <boost/ptr_container/detail/move.hpp>
27 #include <boost/static_assert.hpp>
28 #include <boost/type_traits/add_reference.hpp>
29 #include <boost/type_traits/is_array.hpp>
31 #if defined(BOOST_MSVC)
33 #pragma warning(disable:4521) // Multiple copy constuctors.
36 namespace boost { namespace ptr_container_detail {
41 move_ptrs::default_deleter<T> >
46 typedef typename remove_bounds<T>::type element_type;
47 typedef Deleter deleter_type;
51 struct safe_bool_helper { int x; };
52 typedef int safe_bool_helper::* safe_bool;
53 typedef boost::compressed_pair<element_type*, Deleter> impl_type;
56 typedef typename impl_type::second_reference deleter_reference;
57 typedef typename impl_type::second_const_reference deleter_const_reference;
61 static_move_ptr() : impl_(0) { }
63 static_move_ptr(const static_move_ptr& p)
64 : impl_(p.get(), p.get_deleter())
66 const_cast<static_move_ptr&>(p).release();
69 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
70 static_move_ptr( const move_ptrs::move_source<static_move_ptr<T,Deleter> >& src )
72 static_move_ptr( const move_ptrs::move_source<static_move_ptr>& src )
74 : impl_(src.ptr().get(), src.ptr().get_deleter())
80 explicit static_move_ptr(TT* tt)
81 : impl_(tt, Deleter())
86 ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); }
90 static_move_ptr& operator=(static_move_ptr rhs)
96 // Smart pointer interface
98 element_type* get() const { return ptr(); }
100 element_type& operator*()
102 /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
105 const element_type& operator*() const
107 /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
110 element_type* operator->()
112 /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
115 const element_type* operator->() const
117 /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
121 element_type* release()
123 element_type* result = ptr();
130 if (ptr()) get_deleter()(ptr());
134 template<typename TT>
137 static_move_ptr(tt).swap(*this);
140 template<typename TT, typename DD>
141 void reset(TT* tt, DD dd)
143 static_move_ptr(tt, dd).swap(*this);
146 operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; }
148 void swap(static_move_ptr& p) { impl_.swap(p.impl_); }
150 deleter_reference get_deleter() { return impl_.second(); }
152 deleter_const_reference get_deleter() const { return impl_.second(); }
154 template<typename TT, typename DD>
155 void check(const static_move_ptr<TT, DD>& ptr)
157 typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible;
158 BOOST_STATIC_ASSERT(convertible::value);
161 #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE)
162 // give up on this behavior
165 template<typename Ptr> struct cant_move_from_const;
167 template<typename TT, typename DD>
168 struct cant_move_from_const< const static_move_ptr<TT, DD> > {
169 typedef typename static_move_ptr<TT, DD>::error type;
172 template<typename Ptr>
173 static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0);
177 static_move_ptr(static_move_ptr&);
181 template<typename TT, typename DD>
182 static_move_ptr( static_move_ptr<TT, DD>&,
184 move_ptrs::enable_if_convertible<
185 TT, T, static_move_ptr&
186 >::type::type* = 0 );
188 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE
190 //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
191 // template<typename TT, typename DD>
192 // friend class static_move_ptr;
196 typename impl_type::first_reference
197 ptr() { return impl_.first(); }
199 typename impl_type::first_const_reference
200 ptr() const { return impl_.first(); }
205 } // namespace ptr_container_detail
206 } // End namespace boost.
208 #if defined(BOOST_MSVC)
209 #pragma warning(pop) // #pragma warning(disable:4251)
212 #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED