Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:35

0001 // (C) Copyright Thorsten Ottosen 2005.
0002 // (C) Copyright Jonathan Turkanis 2004.
0003 // (C) Copyright Daniel Wallin 2004.
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
0006 
0007 // Implementation of the move_ptr from the "Move Proposal"
0008 // (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm)
0009 // enhanced to support custom deleters and safe boolean conversions.
0010 //
0011 // The implementation is based on an implementation by Daniel Wallin, at
0012 // "http://aspn.activestate.com/ASPN/Mail/Message/Attachments/boost/
0013 // 400DC271.1060903@student.umu.se/move_ptr.hpp". The current was adapted
0014 // by Jonathan Turkanis to incorporating ideas of Howard Hinnant and
0015 // Rani Sharoni.
0016 
0017 #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
0018 #define BOOST_STATIC_MOVE_PTR_HPP_INCLUDED
0019 
0020 #include <boost/config.hpp> // Member template friends, put size_t in std.
0021 #include <cstddef>          // size_t
0022 #include <boost/compressed_pair.hpp>
0023 #include <boost/ptr_container/detail/default_deleter.hpp>
0024 #include <boost/ptr_container/detail/is_convertible.hpp>
0025 #include <boost/ptr_container/detail/move.hpp>
0026 #include <boost/static_assert.hpp>
0027 #include <boost/type_traits/add_reference.hpp>
0028 #include <boost/type_traits/is_array.hpp>
0029 
0030 #if defined(BOOST_MSVC)
0031 #pragma warning(push)
0032 #pragma warning(disable:4521)        // Multiple copy constuctors.
0033 #endif
0034 
0035 namespace boost { namespace ptr_container_detail {
0036 
0037 
0038 template< typename T,
0039           typename Deleter =
0040               move_ptrs::default_deleter<T> >
0041 class static_move_ptr
0042 {
0043 public:
0044 
0045     typedef typename remove_bounds<T>::type             element_type;
0046     typedef Deleter                                     deleter_type;
0047 
0048 private:
0049 
0050     struct safe_bool_helper { int x; };
0051     typedef int safe_bool_helper::* safe_bool;
0052     typedef boost::compressed_pair<element_type*, Deleter> impl_type;
0053 
0054 public:
0055     typedef typename impl_type::second_reference        deleter_reference;
0056     typedef typename impl_type::second_const_reference  deleter_const_reference;
0057 
0058         // Constructors
0059 
0060     static_move_ptr() : impl_(0) { }
0061 
0062     static_move_ptr(const static_move_ptr& p)
0063         : impl_(p.get(), p.get_deleter())
0064         {
0065             const_cast<static_move_ptr&>(p).release();
0066         }
0067 
0068 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
0069     static_move_ptr( const move_ptrs::move_source<static_move_ptr<T,Deleter> >& src )
0070 #else
0071     static_move_ptr( const move_ptrs::move_source<static_move_ptr>& src )
0072 #endif
0073             : impl_(src.ptr().get(), src.ptr().get_deleter())
0074             {
0075                 src.ptr().release();
0076             }
0077 
0078     template<typename TT>
0079     static_move_ptr(TT* tt, Deleter del)
0080         : impl_(tt, del)
0081         { }
0082 
0083         // Destructor
0084 
0085     ~static_move_ptr() { if (ptr()) get_deleter()(ptr()); }
0086 
0087         // Assignment
0088 
0089     static_move_ptr& operator=(static_move_ptr rhs)
0090         {
0091             rhs.swap(*this);
0092             return *this;
0093         }
0094 
0095         // Smart pointer interface
0096 
0097     element_type* get() const { return ptr(); }
0098 
0099     element_type& operator*()
0100         {
0101             /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
0102         }
0103 
0104     const element_type& operator*() const
0105         {
0106             /*BOOST_STATIC_ASSERT(!is_array);*/ return *ptr();
0107         }
0108 
0109     element_type* operator->()
0110         {
0111             /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
0112         }
0113 
0114     const element_type* operator->() const
0115         {
0116             /*BOOST_STATIC_ASSERT(!is_array);*/ return ptr();
0117         }
0118 
0119 
0120     element_type* release()
0121         {
0122             element_type* result = ptr();
0123             ptr() = 0;
0124             return result;
0125         }
0126 
0127     void reset()
0128         {
0129             if (ptr()) get_deleter()(ptr());
0130             ptr() = 0;
0131         }
0132 
0133     template<typename TT>
0134     void reset(TT* tt, Deleter dd)
0135         {
0136             static_move_ptr(tt, dd).swap(*this);
0137         }
0138 
0139     operator safe_bool() const { return ptr() ? &safe_bool_helper::x : 0; }
0140 
0141     void swap(static_move_ptr& p) { impl_.swap(p.impl_); }
0142 
0143     deleter_reference get_deleter() { return impl_.second(); }
0144 
0145     deleter_const_reference get_deleter() const { return impl_.second(); }
0146 private:
0147     template<typename TT, typename DD>
0148     void check(const static_move_ptr<TT, DD>&)
0149         {
0150             typedef move_ptrs::is_smart_ptr_convertible<TT, T> convertible;
0151             BOOST_STATIC_ASSERT(convertible::value);
0152         }
0153 
0154 #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_NO_SFINAE)
0155 // give up on this behavior
0156 #else
0157 
0158     template<typename Ptr> struct cant_move_from_const;
0159 
0160     template<typename TT, typename DD>
0161     struct cant_move_from_const< const static_move_ptr<TT, DD> > {
0162         typedef typename static_move_ptr<TT, DD>::error type;
0163     };
0164 
0165     template<typename Ptr>
0166     static_move_ptr(Ptr&, typename cant_move_from_const<Ptr>::type = 0);
0167 
0168 
0169 public:
0170     static_move_ptr(static_move_ptr&);
0171 
0172 
0173 private:
0174     template<typename TT, typename DD>
0175     static_move_ptr( static_move_ptr<TT, DD>&,
0176                      typename
0177                      move_ptrs::enable_if_convertible<
0178                          TT, T, static_move_ptr&
0179                      >::type::type* = 0 );
0180 
0181 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING || BOOST_NO_SFINAE
0182 
0183 //#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
0184 //    template<typename TT, typename DD>
0185 //    friend class static_move_ptr;
0186 //#else
0187     public:
0188 //#endif
0189     typename impl_type::first_reference
0190     ptr() { return impl_.first(); }
0191 
0192     typename impl_type::first_const_reference
0193     ptr() const { return impl_.first(); }
0194 
0195     impl_type impl_;
0196 };
0197 
0198 } // namespace ptr_container_detail
0199 } // End namespace boost.
0200 
0201 #if defined(BOOST_MSVC)
0202 #pragma warning(pop) // #pragma warning(disable:4251)
0203 #endif
0204 
0205 #endif      // #ifndef BOOST_STATIC_MOVE_PTR_HPP_INCLUDED