Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/smart_ptr/scoped_ptr.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
0003 
0004 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
0005 //  Copyright (c) 2001, 2002 Peter Dimov
0006 //
0007 //  Distributed under the Boost Software License, Version 1.0. (See
0008 //  accompanying file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 //  See http://www.boost.org/libs/smart_ptr/ for documentation.
0012 
0013 #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
0014 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
0015 #include <boost/smart_ptr/detail/deprecated_macros.hpp>
0016 #include <boost/core/checked_delete.hpp>
0017 #include <boost/assert.hpp>
0018 #include <boost/config/workaround.hpp>
0019 #include <boost/config.hpp>
0020 #include <cstddef>
0021 
0022 #ifndef BOOST_NO_AUTO_PTR
0023 # include <memory>          // for std::auto_ptr
0024 #endif
0025 
0026 #if defined( BOOST_SP_DISABLE_DEPRECATED )
0027 #pragma GCC diagnostic push
0028 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0029 #endif
0030 
0031 namespace boost
0032 {
0033 
0034 // Debug hooks
0035 
0036 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0037 
0038 void sp_scalar_constructor_hook(void * p);
0039 void sp_scalar_destructor_hook(void * p);
0040 
0041 #endif
0042 
0043 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
0044 //  of the object pointed to, either on destruction of the scoped_ptr or via
0045 //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
0046 //  use shared_ptr or std::auto_ptr if your needs are more complex.
0047 
0048 template<class T> class scoped_ptr // noncopyable
0049 {
0050 private:
0051 
0052     T * px;
0053 
0054     scoped_ptr(scoped_ptr const &);
0055     scoped_ptr & operator=(scoped_ptr const &);
0056 
0057     typedef scoped_ptr<T> this_type;
0058 
0059     void operator==( scoped_ptr const& ) const;
0060     void operator!=( scoped_ptr const& ) const;
0061 
0062 public:
0063 
0064     typedef T element_type;
0065 
0066     explicit scoped_ptr( T * p = 0 ) noexcept : px( p )
0067     {
0068 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0069         boost::sp_scalar_constructor_hook( px );
0070 #endif
0071     }
0072 
0073 #ifndef BOOST_NO_AUTO_PTR
0074 
0075     explicit scoped_ptr( std::auto_ptr<T> p ) noexcept : px( p.release() )
0076     {
0077 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0078         boost::sp_scalar_constructor_hook( px );
0079 #endif
0080     }
0081 
0082 #endif
0083 
0084     ~scoped_ptr() noexcept
0085     {
0086 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
0087         boost::sp_scalar_destructor_hook( px );
0088 #endif
0089         boost::checked_delete( px );
0090     }
0091 
0092     void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
0093     {
0094         BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
0095         this_type(p).swap(*this);
0096     }
0097 
0098     T & operator*() const BOOST_SP_NOEXCEPT_WITH_ASSERT
0099     {
0100         BOOST_ASSERT( px != 0 );
0101         return *px;
0102     }
0103 
0104     T * operator->() const BOOST_SP_NOEXCEPT_WITH_ASSERT
0105     {
0106         BOOST_ASSERT( px != 0 );
0107         return px;
0108     }
0109 
0110     T * get() const noexcept
0111     {
0112         return px;
0113     }
0114 
0115     explicit operator bool () const noexcept
0116     {
0117         return px != 0;
0118     }
0119 
0120     void swap(scoped_ptr & b) noexcept
0121     {
0122         T * tmp = b.px;
0123         b.px = px;
0124         px = tmp;
0125     }
0126 };
0127 
0128 template<class T> inline bool operator==( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
0129 {
0130     return p.get() == 0;
0131 }
0132 
0133 template<class T> inline bool operator==( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
0134 {
0135     return p.get() == 0;
0136 }
0137 
0138 template<class T> inline bool operator!=( scoped_ptr<T> const & p, std::nullptr_t ) noexcept
0139 {
0140     return p.get() != 0;
0141 }
0142 
0143 template<class T> inline bool operator!=( std::nullptr_t, scoped_ptr<T> const & p ) noexcept
0144 {
0145     return p.get() != 0;
0146 }
0147 
0148 template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) noexcept
0149 {
0150     a.swap(b);
0151 }
0152 
0153 // get_pointer(p) is a generic way to say p.get()
0154 
0155 template<class T> inline T * get_pointer(scoped_ptr<T> const & p) noexcept
0156 {
0157     return p.get();
0158 }
0159 
0160 } // namespace boost
0161 
0162 #if defined( BOOST_SP_DISABLE_DEPRECATED )
0163 #pragma GCC diagnostic pop
0164 #endif
0165 
0166 #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED