]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr/weak_ptr.hpp
tex2lyx: improve CJK handling
[lyx.git] / boost / boost / smart_ptr / weak_ptr.hpp
1 #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
3
4 //
5 //  weak_ptr.hpp
6 //
7 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 //  See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
14 //
15
16 #include <memory> // boost.TR1 include order fix
17 #include <boost/smart_ptr/detail/shared_count.hpp>
18 #include <boost/smart_ptr/shared_ptr.hpp>
19
20 namespace boost
21 {
22
23 template<class T> class weak_ptr
24 {
25 private:
26
27     // Borland 5.5.1 specific workarounds
28     typedef weak_ptr<T> this_type;
29
30 public:
31
32     typedef T element_type;
33
34     weak_ptr(): px(0), pn() // never throws in 1.30+
35     {
36     }
37
38 //  generated copy constructor, assignment, destructor are fine...
39
40 #if defined( BOOST_HAS_RVALUE_REFS )
41
42 // ... except in C++0x, move disables the implicit copy
43
44     weak_ptr( weak_ptr const & r ): px( r.px ), pn( r.pn ) // never throws
45     {
46     }
47
48     weak_ptr & operator=( weak_ptr const & r ) // never throws
49     {
50         px = r.px;
51         pn = r.pn;
52         return *this;
53     }
54
55 #endif
56
57 //
58 //  The "obvious" converting constructor implementation:
59 //
60 //  template<class Y>
61 //  weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
62 //  {
63 //  }
64 //
65 //  has a serious problem.
66 //
67 //  r.px may already have been invalidated. The px(r.px)
68 //  conversion may require access to *r.px (virtual inheritance).
69 //
70 //  It is not possible to avoid spurious access violations since
71 //  in multithreaded programs r.px may be invalidated at any point.
72 //
73
74     template<class Y>
75 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
76
77     weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
78
79 #else
80
81     weak_ptr( weak_ptr<Y> const & r )
82
83 #endif
84     : px(r.lock().get()), pn(r.pn) // never throws
85     {
86     }
87
88 #if defined( BOOST_HAS_RVALUE_REFS )
89
90     template<class Y>
91 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
92
93     weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
94
95 #else
96
97     weak_ptr( weak_ptr<Y> && r )
98
99 #endif
100     : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
101     {
102         r.px = 0;
103     }
104
105     // for better efficiency in the T == Y case
106     weak_ptr( weak_ptr && r ): px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) // never throws
107     {
108         r.px = 0;
109     }
110
111     // for better efficiency in the T == Y case
112     weak_ptr & operator=( weak_ptr && r ) // never throws
113     {
114         this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
115         return *this;
116     }
117
118
119 #endif
120
121     template<class Y>
122 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
123
124     weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
125
126 #else
127
128     weak_ptr( shared_ptr<Y> const & r )
129
130 #endif
131     : px( r.px ), pn( r.pn ) // never throws
132     {
133     }
134
135 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
136
137     template<class Y>
138     weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
139     {
140         px = r.lock().get();
141         pn = r.pn;
142         return *this;
143     }
144
145 #if defined( BOOST_HAS_RVALUE_REFS )
146
147     template<class Y>
148     weak_ptr & operator=( weak_ptr<Y> && r )
149     {
150         this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
151         return *this;
152     }
153
154 #endif
155
156     template<class Y>
157     weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
158     {
159         px = r.px;
160         pn = r.pn;
161         return *this;
162     }
163
164 #endif
165
166     shared_ptr<T> lock() const // never throws
167     {
168         return shared_ptr<element_type>( *this, boost::detail::sp_nothrow_tag() );
169     }
170
171     long use_count() const // never throws
172     {
173         return pn.use_count();
174     }
175
176     bool expired() const // never throws
177     {
178         return pn.use_count() == 0;
179     }
180
181     bool _empty() const // extension, not in std::weak_ptr
182     {
183         return pn.empty();
184     }
185
186     void reset() // never throws in 1.30+
187     {
188         this_type().swap(*this);
189     }
190
191     void swap(this_type & other) // never throws
192     {
193         std::swap(px, other.px);
194         pn.swap(other.pn);
195     }
196
197     void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
198     {
199         px = px2;
200         pn = pn2;
201     }
202
203     template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const
204     {
205         return pn < rhs.pn;
206     }
207
208     template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const
209     {
210         return pn < rhs.pn;
211     }
212
213 // Tasteless as this may seem, making all members public allows member templates
214 // to work in the absence of member template friends. (Matthew Langston)
215
216 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
217
218 private:
219
220     template<class Y> friend class weak_ptr;
221     template<class Y> friend class shared_ptr;
222
223 #endif
224
225     T * px;                       // contained pointer
226     boost::detail::weak_count pn; // reference counter
227
228 };  // weak_ptr
229
230 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
231 {
232     return a.owner_before( b );
233 }
234
235 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
236 {
237     a.swap(b);
238 }
239
240 } // namespace boost
241
242 #endif  // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED