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
00005
00006
00007
00008
00009
00010
00011
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>
00020 #endif
00021
00022 namespace boost
00023 {
00024
00025
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
00035
00036
00037
00038
00039 template<class T> class scoped_ptr
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 )
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() )
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()
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)
00084 {
00085 BOOST_ASSERT( p == 0 || p != px );
00086 this_type(p).swap(*this);
00087 }
00088
00089 T & operator*() const
00090 {
00091 BOOST_ASSERT( px != 0 );
00092 return *px;
00093 }
00094
00095 T * operator->() const
00096 {
00097 BOOST_ASSERT( px != 0 );
00098 return px;
00099 }
00100
00101 T * get() const
00102 {
00103 return px;
00104 }
00105
00106
00107 #include <boost/smart_ptr/detail/operator_bool.hpp>
00108
00109 void swap(scoped_ptr & b)
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)
00118 {
00119 a.swap(b);
00120 }
00121
00122
00123
00124 template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
00125 {
00126 return p.get();
00127 }
00128
00129 }
00130
00131 #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED