// (C) Copyright Jonathan Turkanis 2004. // (C) Copyright Daniel Wallin 2004. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) // Implementation of the move_ptr from the "Move Proposal" // (http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1377.htm) // enhanced to support custom deleters and safe boolean conversions. // // The implementation is based on an implementation by Daniel Wallin, at // "http://aspn.activestate.com/ASPN/Mail/Message/Attachments/boost/ // 400DC271.1060903@student.umu.se/move_ptr.hpp". The current was adapted // by Jonathan Turkanis to incorporating ideas of Howard Hinnant and // Rani Sharoni. #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED #define BOOST_STATIC_MOVE_PTR_HPP_INCLUDED #include // Member template friends, put size_t in std. #include // swap. #include // size_t #include #include #include #include #include #include #include #include #include #if defined(BOOST_MSVC) #pragma warning(push) #pragma warning(disable:4521) // Multiple copy constuctors. #endif namespace boost { template class static_move_ptr; template struct is_const_static_move_ptr : mpl::false_ { }; template struct is_const_static_move_ptr< const static_move_ptr > : mpl::true_ { }; template< typename T, typename Deleter = move_ptrs::default_deleter > class static_move_ptr { public: typedef typename remove_bounds::type element_type; typedef Deleter deleter_type; private: BOOST_STATIC_CONSTANT(bool, is_array = boost::is_array::value); BOOST_STATIC_ASSERT(!is_array || (is_same::value)); struct safe_bool_helper { int x; }; typedef int safe_bool_helper::* safe_bool; typedef boost::compressed_pair impl_type; public: typedef typename impl_type::second_reference deleter_reference; typedef typename impl_type::second_const_reference deleter_const_reference; // Constructors static_move_ptr() : impl_(0) { } static_move_ptr(const static_move_ptr& p) : impl_(p.get(), p.get_deleter()) { const_cast(p).release(); } template static_move_ptr( const static_move_ptr& p, typename move_ptrs::enable_if_convertible::type* = 0 ) : impl_( p.get(), const_cast::type>(p.get_deleter()) ) { check(p); const_cast&>(p).release(); } template static_move_ptr(move_ptrs::move_source< static_move_ptr > src) : impl_(src.ptr().get(), src.ptr().get_deleter()) { typedef move_ptrs::is_smart_ptr_convertible convertible; BOOST_STATIC_ASSERT(convertible::value); src.ptr().release(); } template explicit static_move_ptr(TT* tt) : impl_(tt, Deleter()) { } template static_move_ptr(TT* tt, DD dd) : impl_(tt, dd) { } // Destructor ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); } // Assignment static_move_ptr& operator=(static_move_ptr rhs) { rhs.swap(*this); return *this; } template typename move_ptrs::enable_if_convertible::type operator=(static_move_ptr rhs) { static_move_ptr tmp(move(rhs)); this->swap(tmp); return *this; } // Smart pointer interface element_type* get() const { return ptr(); } element_type& operator*() const { BOOST_STATIC_ASSERT(!is_array); return *ptr(); } element_type* operator->() const { BOOST_STATIC_ASSERT(!is_array); return ptr(); } element_type& operator[](std::size_t i) const { BOOST_STATIC_ASSERT(is_array); return ptr()[i]; } element_type* release() { element_type* result = ptr(); ptr() = 0; return result; } void reset() { if (ptr()) get_deleter()(ptr()); ptr() = 0; } template void reset(TT* tt) { static_move_ptr(tt).swap(*this); } template void reset(TT* tt, DD dd) { static_move_ptr(tt, dd).swap(*this); } operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; } void swap(static_move_ptr& p) { impl_.swap(p.impl_); } deleter_reference get_deleter() { return impl_.second(); } deleter_const_reference get_deleter() const { return impl_.second(); } private: template void check(const static_move_ptr& ptr) { typedef move_ptrs::is_smart_ptr_convertible convertible; BOOST_STATIC_ASSERT(convertible::value); } //template //struct cant_move_from_const; //template //struct cant_move_from_const< const static_move_ptr > { // typedef typename static_move_ptr::error type; //}; template struct cant_move_from_const; template struct cant_move_from_const< TT, typename enable_if< is_const_static_move_ptr >::type > { typedef typename TT::error type; }; template static_move_ptr(Ptr&, typename cant_move_from_const::type = 0); static_move_ptr(static_move_ptr&); template static_move_ptr( static_move_ptr&, typename move_ptrs::enable_if_convertible< TT, T, static_move_ptr& >::type::type* = 0 ); #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS template friend class static_move_ptr; #else public: #endif typename impl_type::first_reference ptr() { return impl_.first(); } typename impl_type::first_const_reference ptr() const { return impl_.first(); } impl_type impl_; }; } // End namespace boost. #if defined(BOOST_MSVC) #pragma warning(pop) // #pragma warning(disable:4251) #endif #endif // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED