Line data Source code
1 : #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED 2 : #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED 3 : 4 : // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. 5 : // Copyright (c) 2001, 2002 Peter Dimov 6 : // 7 : // Distributed under the Boost Software License, Version 1.0. (See 8 : // accompanying file LICENSE_1_0.txt or copy at 9 : // http://www.boost.org/LICENSE_1_0.txt) 10 : // 11 : // See http://www.boost.org/libs/smart_ptr/ for documentation. 12 : 13 : #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp> 14 : #include <boost/smart_ptr/detail/sp_noexcept.hpp> 15 : #include <boost/smart_ptr/detail/deprecated_macros.hpp> 16 : #include <boost/core/checked_delete.hpp> 17 : #include <boost/assert.hpp> 18 : #include <boost/config/workaround.hpp> 19 : #include <boost/config.hpp> 20 : #include <cstddef> 21 : 22 : #ifndef BOOST_NO_AUTO_PTR 23 : # include <memory> // for std::auto_ptr 24 : #endif 25 : 26 : #if defined( BOOST_SP_DISABLE_DEPRECATED ) 27 : #pragma GCC diagnostic push 28 : #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 29 : #endif 30 : 31 : namespace boost 32 : { 33 : 34 : // scoped_ptr mimics a built-in pointer except that it guarantees deletion 35 : // of the object pointed to, either on destruction of the scoped_ptr or via 36 : // an explicit reset(). scoped_ptr is a simple solution for simple needs; 37 : // use shared_ptr or std::auto_ptr if your needs are more complex. 38 : 39 : template<class T> class scoped_ptr // noncopyable 40 : { 41 : private: 42 : 43 : T * px; 44 : 45 : scoped_ptr(scoped_ptr const &); 46 : scoped_ptr & operator=(scoped_ptr const &); 47 : 48 : typedef scoped_ptr<T> this_type; 49 : 50 : void operator==( scoped_ptr const& ) const; 51 : void operator!=( scoped_ptr const& ) const; 52 : 53 : public: 54 : 55 : typedef T element_type; 56 : 57 1168 : explicit scoped_ptr( T * p = 0 ) noexcept : px( p ) 58 584 : { 59 1168 : } 60 : 61 : #ifndef BOOST_NO_AUTO_PTR 62 : 63 : explicit scoped_ptr( std::auto_ptr<T> p ) noexcept : px( p.release() ) 64 : { 65 : } 66 : 67 : #endif 68 : 69 1168 : ~scoped_ptr() noexcept 70 584 : { 71 584 : boost::checked_delete( px ); 72 1168 : } 73 : 74 292 : void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT 75 : { 76 292 : BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors 77 292 : this_type(p).swap(*this); 78 292 : } 79 : 80 0 : T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT 81 : { 82 0 : BOOST_ASSERT( px != 0 ); 83 0 : return *px; 84 : } 85 : 86 580 : T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT 87 : { 88 580 : BOOST_ASSERT( px != 0 ); 89 580 : return px; 90 : } 91 : 92 : T * get() const noexcept 93 : { 94 : return px; 95 : } 96 : 97 : explicit operator bool () const noexcept 98 : { 99 : return px != 0; 100 : } 101 : 102 292 : void swap(scoped_ptr & b) noexcept 103 : { 104 292 : T * tmp = b.px; 105 292 : b.px = px; 106 292 : px = tmp; 107 292 : } 108 : }; 109 : 110 : template<class T> inline bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept 111 : { 112 : return p.get() == 0; 113 : } 114 : 115 : template<class T> inline bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept 116 : { 117 : return p.get() == 0; 118 : } 119 : 120 : template<class T> inline bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept 121 : { 122 : return p.get() != 0; 123 : } 124 : 125 : template<class T> inline bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept 126 : { 127 : return p.get() != 0; 128 : } 129 : 130 : template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept 131 : { 132 : a.swap(b); 133 : } 134 : 135 : // get_pointer(p) is a generic way to say p.get() 136 : 137 : template<class T> inline T * get_pointer(scoped_ptr<T> const & p) noexcept 138 : { 139 : return p.get(); 140 : } 141 : 142 : } // namespace boost 143 : 144 : #if defined( BOOST_SP_DISABLE_DEPRECATED ) 145 : #pragma GCC diagnostic pop 146 : #endif 147 : 148 : #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED