1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/weak_ptr.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,194 @@
1.4 +#ifndef BOOST_WEAK_PTR_HPP_INCLUDED
1.5 +#define BOOST_WEAK_PTR_HPP_INCLUDED
1.6 +
1.7 +//
1.8 +// weak_ptr.hpp
1.9 +//
1.10 +// Copyright (c) 2001, 2002, 2003 Peter Dimov
1.11 +//
1.12 +// Distributed under the Boost Software License, Version 1.0. (See
1.13 +// accompanying file LICENSE_1_0.txt or copy at
1.14 +// http://www.boost.org/LICENSE_1_0.txt)
1.15 +//
1.16 +// See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
1.17 +//
1.18 +
1.19 +#include <memory> // boost.TR1 include order fix
1.20 +#include <boost/detail/shared_count.hpp>
1.21 +#include <boost/shared_ptr.hpp>
1.22 +
1.23 +#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
1.24 +# pragma warning(push)
1.25 +# pragma warning(disable:4284) // odd return type for operator->
1.26 +#endif
1.27 +
1.28 +namespace boost
1.29 +{
1.30 +
1.31 +template<class T> class weak_ptr
1.32 +{
1.33 +private:
1.34 +
1.35 + // Borland 5.5.1 specific workarounds
1.36 + typedef weak_ptr<T> this_type;
1.37 +
1.38 +public:
1.39 +
1.40 + typedef T element_type;
1.41 +
1.42 + weak_ptr(): px(0), pn() // never throws in 1.30+
1.43 + {
1.44 + }
1.45 +
1.46 +// generated copy constructor, assignment, destructor are fine
1.47 +
1.48 +
1.49 +//
1.50 +// The "obvious" converting constructor implementation:
1.51 +//
1.52 +// template<class Y>
1.53 +// weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
1.54 +// {
1.55 +// }
1.56 +//
1.57 +// has a serious problem.
1.58 +//
1.59 +// r.px may already have been invalidated. The px(r.px)
1.60 +// conversion may require access to *r.px (virtual inheritance).
1.61 +//
1.62 +// It is not possible to avoid spurious access violations since
1.63 +// in multithreaded programs r.px may be invalidated at any point.
1.64 +//
1.65 +
1.66 + template<class Y>
1.67 + weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
1.68 + {
1.69 + px = r.lock().get();
1.70 + }
1.71 +
1.72 + template<class Y>
1.73 + weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
1.74 + {
1.75 + }
1.76 +
1.77 +#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
1.78 +
1.79 + template<class Y>
1.80 + weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
1.81 + {
1.82 + px = r.lock().get();
1.83 + pn = r.pn;
1.84 + return *this;
1.85 + }
1.86 +
1.87 + template<class Y>
1.88 + weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
1.89 + {
1.90 + px = r.px;
1.91 + pn = r.pn;
1.92 + return *this;
1.93 + }
1.94 +
1.95 +#endif
1.96 +
1.97 + shared_ptr<T> lock() const // never throws
1.98 + {
1.99 +#if defined(BOOST_HAS_THREADS)
1.100 +
1.101 + // optimization: avoid throw overhead
1.102 + if(expired())
1.103 + {
1.104 + return shared_ptr<element_type>();
1.105 + }
1.106 +
1.107 + try
1.108 + {
1.109 + return shared_ptr<element_type>(*this);
1.110 + }
1.111 + catch(bad_weak_ptr const &)
1.112 + {
1.113 + // Q: how can we get here?
1.114 + // A: another thread may have invalidated r after the use_count test above.
1.115 + return shared_ptr<element_type>();
1.116 + }
1.117 +
1.118 +#else
1.119 +
1.120 + // optimization: avoid try/catch overhead when single threaded
1.121 + return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
1.122 +
1.123 +#endif
1.124 + }
1.125 +
1.126 + long use_count() const // never throws
1.127 + {
1.128 + return pn.use_count();
1.129 + }
1.130 +
1.131 + bool expired() const // never throws
1.132 + {
1.133 + return pn.use_count() == 0;
1.134 + }
1.135 +
1.136 + void reset() // never throws in 1.30+
1.137 + {
1.138 + this_type().swap(*this);
1.139 + }
1.140 +
1.141 + void swap(this_type & other) // never throws
1.142 + {
1.143 + std::swap(px, other.px);
1.144 + pn.swap(other.pn);
1.145 + }
1.146 +
1.147 + void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
1.148 + {
1.149 + px = px2;
1.150 + pn = pn2;
1.151 + }
1.152 +
1.153 + template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
1.154 + {
1.155 + return pn < rhs.pn;
1.156 + }
1.157 +
1.158 +// Tasteless as this may seem, making all members public allows member templates
1.159 +// to work in the absence of member template friends. (Matthew Langston)
1.160 +
1.161 +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
1.162 +
1.163 +private:
1.164 +
1.165 + template<class Y> friend class weak_ptr;
1.166 + template<class Y> friend class shared_ptr;
1.167 +
1.168 +#endif
1.169 +
1.170 + T * px; // contained pointer
1.171 + boost::detail::weak_count pn; // reference counter
1.172 +
1.173 +}; // weak_ptr
1.174 +
1.175 +template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
1.176 +{
1.177 + return a._internal_less(b);
1.178 +}
1.179 +
1.180 +template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
1.181 +{
1.182 + a.swap(b);
1.183 +}
1.184 +
1.185 +// deprecated, provided for backward compatibility
1.186 +template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r)
1.187 +{
1.188 + return r.lock();
1.189 +}
1.190 +
1.191 +} // namespace boost
1.192 +
1.193 +#ifdef BOOST_MSVC
1.194 +# pragma warning(pop)
1.195 +#endif
1.196 +
1.197 +#endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED