scoped_ptr.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
00003 
00004 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
00005 //  Copyright (c) 2001, 2002 Peter Dimov
00006 //
00007 //  Distributed under the Boost Software License, Version 1.0. (See
00008 //  accompanying file LICENSE_1_0.txt or copy at
00009 //  http://www.boost.org/LICENSE_1_0.txt)
00010 //
00011 //  http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
00012 //
00013 
00014 #include <boost/assert.hpp>
00015 #include <boost/checked_delete.hpp>
00016 #include <boost/detail/workaround.hpp>
00017 
00018 #ifndef BOOST_NO_AUTO_PTR
00019 # include <memory>          // for std::auto_ptr
00020 #endif
00021 
00022 namespace boost
00023 {
00024 
00025 // Debug hooks
00026 
00027 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00028 
00029 void sp_scalar_constructor_hook(void * p);
00030 void sp_scalar_destructor_hook(void * p);
00031 
00032 #endif
00033 
00034 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
00035 //  of the object pointed to, either on destruction of the scoped_ptr or via
00036 //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
00037 //  use shared_ptr or std::auto_ptr if your needs are more complex.
00038 
00039 template<class T> class scoped_ptr // noncopyable
00040 {
00041 private:
00042 
00043     T * px;
00044 
00045     scoped_ptr(scoped_ptr const &);
00046     scoped_ptr & operator=(scoped_ptr const &);
00047 
00048     typedef scoped_ptr<T> this_type;
00049 
00050     void operator==( scoped_ptr const& ) const;
00051     void operator!=( scoped_ptr const& ) const;
00052 
00053 public:
00054 
00055     typedef T element_type;
00056 
00057     explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
00058     {
00059 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00060         boost::sp_scalar_constructor_hook( px );
00061 #endif
00062     }
00063 
00064 #ifndef BOOST_NO_AUTO_PTR
00065 
00066     explicit scoped_ptr( std::auto_ptr<T> p ): px( p.release() ) // never throws
00067     {
00068 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00069         boost::sp_scalar_constructor_hook( px );
00070 #endif
00071     }
00072 
00073 #endif
00074 
00075     ~scoped_ptr() // never throws
00076     {
00077 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
00078         boost::sp_scalar_destructor_hook( px );
00079 #endif
00080         boost::checked_delete( px );
00081     }
00082 
00083     void reset(T * p = 0) // never throws
00084     {
00085         BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
00086         this_type(p).swap(*this);
00087     }
00088 
00089     T & operator*() const // never throws
00090     {
00091         BOOST_ASSERT( px != 0 );
00092         return *px;
00093     }
00094 
00095     T * operator->() const // never throws
00096     {
00097         BOOST_ASSERT( px != 0 );
00098         return px;
00099     }
00100 
00101     T * get() const // never throws
00102     {
00103         return px;
00104     }
00105 
00106 // implicit conversion to "bool"
00107 #include <boost/smart_ptr/detail/operator_bool.hpp>
00108 
00109     void swap(scoped_ptr & b) // never throws
00110     {
00111         T * tmp = b.px;
00112         b.px = px;
00113         px = tmp;
00114     }
00115 };
00116 
00117 template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
00118 {
00119     a.swap(b);
00120 }
00121 
00122 // get_pointer(p) is a generic way to say p.get()
00123 
00124 template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
00125 {
00126     return p.get();
00127 }
00128 
00129 } // namespace boost
00130 
00131 #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29