| 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(BOOST_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 | static_move_ptr(TT* tt, Deleter del) |
| 80 | : impl_(tt, del) |
| 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(p&: *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, Deleter dd) |
| 135 | { |
| 136 | static_move_ptr(tt, dd).swap(p&: *this); |
| 137 | } |
| 138 | |
| 139 | operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; } |
| 140 | |
| 141 | void swap(static_move_ptr& p) { impl_.swap(p.impl_); } |
| 142 | |
| 143 | deleter_reference get_deleter() { return impl_.second(); } |
| 144 | |
| 145 | deleter_const_reference get_deleter() const { return impl_.second(); } |
| 146 | private: |
| 147 | template<typename TT, typename DD> |
| 148 | void check(const static_move_ptr<TT, DD>&) |
| 149 | { |
| 150 | typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible; |
| 151 | BOOST_STATIC_ASSERT(convertible::value); |
| 152 | } |
| 153 | |
| 154 | #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE) |
| 155 | // give up on this behavior |
| 156 | #else |
| 157 | |
| 158 | template<typename Ptr> struct cant_move_from_const; |
| 159 | |
| 160 | template<typename TT, typename DD> |
| 161 | struct cant_move_from_const< const static_move_ptr<TT, DD> > { |
| 162 | typedef typename static_move_ptr<TT, DD>::error type; |
| 163 | }; |
| 164 | |
| 165 | template<typename Ptr> |
| 166 | static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0); |
| 167 | |
| 168 | |
| 169 | public: |
| 170 | static_move_ptr(static_move_ptr&); |
| 171 | |
| 172 | |
| 173 | private: |
| 174 | template<typename TT, typename DD> |
| 175 | static_move_ptr( static_move_ptr<TT, DD>&, |
| 176 | typename |
| 177 | move_ptrs::enable_if_convertible< |
| 178 | TT, T, static_move_ptr& |
| 179 | >::type::type* = 0 ); |
| 180 | |
| 181 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE |
| 182 | |
| 183 | //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
| 184 | // template<typename TT, typename DD> |
| 185 | // friend class static_move_ptr; |
| 186 | //#else |
| 187 | public: |
| 188 | //#endif |
| 189 | typename impl_type::first_reference |
| 190 | ptr() { return impl_.first(); } |
| 191 | |
| 192 | typename impl_type::first_const_reference |
| 193 | ptr() const { return impl_.first(); } |
| 194 | |
| 195 | impl_type impl_; |
| 196 | }; |
| 197 | |
| 198 | } // namespace ptr_container_detail |
| 199 | } // End namespace boost. |
| 200 | |
| 201 | #if defined(BOOST_MSVC) |
| 202 | #pragma warning(pop) // #pragma warning(disable:4251) |
| 203 | #endif |
| 204 | |
| 205 | #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED |
| 206 | |