Go to the documentation of this file.00001 #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <memory>
00017 #include <boost/smart_ptr/detail/shared_count.hpp>
00018 #include <boost/smart_ptr/shared_ptr.hpp>
00019
00020 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
00021 # pragma warning(push)
00022 # pragma warning(disable:4284) // odd return type for operator->
00023 #endif
00024
00025 namespace boost
00026 {
00027
00028 template<class T> class weak_ptr
00029 {
00030 private:
00031
00032
00033 typedef weak_ptr<T> this_type;
00034
00035 public:
00036
00037 typedef T element_type;
00038
00039 weak_ptr(): px(0), pn()
00040 {
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 template<class Y>
00064 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
00065
00066 weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
00067
00068 #else
00069
00070 weak_ptr( weak_ptr<Y> const & r )
00071
00072 #endif
00073 : pn(r.pn)
00074 {
00075 px = r.lock().get();
00076 }
00077
00078 template<class Y>
00079 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
00080
00081 weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
00082
00083 #else
00084
00085 weak_ptr( shared_ptr<Y> const & r )
00086
00087 #endif
00088 : px( r.px ), pn( r.pn )
00089 {
00090 }
00091
00092 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
00093
00094 template<class Y>
00095 weak_ptr & operator=(weak_ptr<Y> const & r)
00096 {
00097 px = r.lock().get();
00098 pn = r.pn;
00099 return *this;
00100 }
00101
00102 template<class Y>
00103 weak_ptr & operator=(shared_ptr<Y> const & r)
00104 {
00105 px = r.px;
00106 pn = r.pn;
00107 return *this;
00108 }
00109
00110 #endif
00111
00112 shared_ptr<T> lock() const
00113 {
00114 return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
00115 }
00116
00117 long use_count() const
00118 {
00119 return pn.use_count();
00120 }
00121
00122 bool expired() const
00123 {
00124 return pn.use_count() == 0;
00125 }
00126
00127 void reset()
00128 {
00129 this_type().swap(*this);
00130 }
00131
00132 void swap(this_type & other)
00133 {
00134 std::swap(px, other.px);
00135 pn.swap(other.pn);
00136 }
00137
00138 void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
00139 {
00140 px = px2;
00141 pn = pn2;
00142 }
00143
00144 template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
00145 {
00146 return pn < rhs.pn;
00147 }
00148
00149
00150
00151
00152 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
00153
00154 private:
00155
00156 template<class Y> friend class weak_ptr;
00157 template<class Y> friend class shared_ptr;
00158
00159 #endif
00160
00161 T * px;
00162 boost::detail::weak_count pn;
00163
00164 };
00165
00166 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
00167 {
00168 return a._internal_less(b);
00169 }
00170
00171 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
00172 {
00173 a.swap(b);
00174 }
00175
00176 }
00177
00178 #ifdef BOOST_MSVC
00179 # pragma warning(pop)
00180 #endif
00181
00182 #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED