weak_ptr.hpp
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 //  weak_ptr.hpp
00006 //
00007 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
00008 //
00009 // Distributed under the Boost Software License, Version 1.0. (See
00010 // accompanying file LICENSE_1_0.txt or copy at
00011 // http://www.boost.org/LICENSE_1_0.txt)
00012 //
00013 //  See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
00014 //
00015 
00016 #include <memory> // boost.TR1 include order fix
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     // Borland 5.5.1 specific workarounds
00033     typedef weak_ptr<T> this_type;
00034 
00035 public:
00036 
00037     typedef T element_type;
00038 
00039     weak_ptr(): px(0), pn() // never throws in 1.30+
00040     {
00041     }
00042 
00043 //  generated copy constructor, assignment, destructor are fine
00044 
00045 
00046 //
00047 //  The "obvious" converting constructor implementation:
00048 //
00049 //  template<class Y>
00050 //  weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
00051 //  {
00052 //  }
00053 //
00054 //  has a serious problem.
00055 //
00056 //  r.px may already have been invalidated. The px(r.px)
00057 //  conversion may require access to *r.px (virtual inheritance).
00058 //
00059 //  It is not possible to avoid spurious access violations since
00060 //  in multithreaded programs r.px may be invalidated at any point.
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) // never throws
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 ) // never throws
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) // never throws
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) // never throws
00104     {
00105         px = r.px;
00106         pn = r.pn;
00107         return *this;
00108     }
00109 
00110 #endif
00111 
00112     shared_ptr<T> lock() const // never throws
00113     {
00114         return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
00115     }
00116 
00117     long use_count() const // never throws
00118     {
00119         return pn.use_count();
00120     }
00121 
00122     bool expired() const // never throws
00123     {
00124         return pn.use_count() == 0;
00125     }
00126 
00127     void reset() // never throws in 1.30+
00128     {
00129         this_type().swap(*this);
00130     }
00131 
00132     void swap(this_type & other) // never throws
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 // Tasteless as this may seem, making all members public allows member templates
00150 // to work in the absence of member template friends. (Matthew Langston)
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;                       // contained pointer
00162     boost::detail::weak_count pn; // reference counter
00163 
00164 };  // weak_ptr
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 } // namespace boost
00177 
00178 #ifdef BOOST_MSVC
00179 # pragma warning(pop)
00180 #endif    
00181 
00182 #endif  // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED


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