]> git.lyx.org Git - lyx.git/blob - boost/boost/weak_ptr.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / weak_ptr.hpp
1 #ifndef BOOST_WEAK_PTR_HPP_INCLUDED
2 #define BOOST_WEAK_PTR_HPP_INCLUDED
3
4 //
5 //  weak_ptr.hpp
6 //
7 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
8 //
9 //  Permission to copy, use, modify, sell and distribute this software
10 //  is granted provided this copyright notice appears in all copies.
11 //  This software is provided "as is" without express or implied
12 //  warranty, and with no claim as to its suitability for any purpose.
13 //
14 //  See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation.
15 //
16
17 #include <boost/shared_ptr.hpp>
18
19 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
20 # pragma warning(push)
21 # pragma warning(disable:4284) // odd return type for operator->
22 #endif
23
24 namespace boost
25 {
26
27 template<class T> class weak_ptr
28 {
29 private:
30
31     // Borland 5.5.1 specific workarounds
32     typedef weak_ptr<T> this_type;
33
34 public:
35
36     typedef T element_type;
37
38     weak_ptr(): px(0), pn() // never throws in 1.30+
39     {
40     }
41
42 //  generated copy constructor, assignment, destructor are fine
43
44
45 //
46 //  The "obvious" converting constructor implementation:
47 //
48 //  template<class Y>
49 //  weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
50 //  {
51 //  }
52 //
53 //  has a serious problem.
54 //
55 //  r.px may already have been invalidated. The px(r.px)
56 //  conversion may require access to *r.px (virtual inheritance).
57 //
58 //  It is not possible to avoid spurious access violations since
59 //  in multithreaded programs r.px may be invalidated at any point.
60 //
61
62     template<class Y>
63     weak_ptr(weak_ptr<Y> const & r): pn(r.pn) // never throws
64     {
65         px = r.lock().get();
66     }
67
68     template<class Y>
69     weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
70     {
71     }
72
73 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
74
75     template<class Y>
76     weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
77     {
78         px = r.lock().get();
79         pn = r.pn;
80         return *this;
81     }
82
83     template<class Y>
84     weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
85     {
86         px = r.px;
87         pn = r.pn;
88         return *this;
89     }
90
91 #endif
92
93     shared_ptr<T> lock() const // never throws
94     {
95 #if defined(BOOST_HAS_THREADS)
96
97         // optimization: avoid throw overhead
98         if(expired())
99         {
100             return shared_ptr<element_type>();
101         }
102
103         try
104         {
105             return shared_ptr<element_type>(*this);
106         }
107         catch(bad_weak_ptr const &)
108         {
109             // Q: how can we get here?
110             // A: another thread may have invalidated r after the use_count test above.
111             return shared_ptr<element_type>();
112         }
113
114 #else
115
116         // optimization: avoid try/catch overhead when single threaded
117         return expired()? shared_ptr<element_type>(): shared_ptr<element_type>(*this);
118
119 #endif
120     }
121
122     long use_count() const // never throws
123     {
124         return pn.use_count();
125     }
126
127     bool expired() const // never throws
128     {
129         return pn.use_count() == 0;
130     }
131
132     void reset() // never throws in 1.30+
133     {
134         this_type().swap(*this);
135     }
136
137     void swap(this_type & other) // never throws
138     {
139         std::swap(px, other.px);
140         pn.swap(other.pn);
141     }
142
143     void _internal_assign(T * px2, detail::shared_count const & pn2)
144     {
145         px = px2;
146         pn = pn2;
147     }
148
149     template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
150     {
151         return pn < rhs.pn;
152     }
153
154 // Tasteless as this may seem, making all members public allows member templates
155 // to work in the absence of member template friends. (Matthew Langston)
156
157 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
158
159 private:
160
161     template<class Y> friend class weak_ptr;
162     template<class Y> friend class shared_ptr;
163
164 #endif
165
166     T * px;                     // contained pointer
167     detail::weak_count pn;      // reference counter
168
169 };  // weak_ptr
170
171 template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
172 {
173     return a._internal_less(b);
174 }
175
176 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
177 {
178     a.swap(b);
179 }
180
181 // deprecated, provided for backward compatibility
182 template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r)
183 {
184     return r.lock();
185 }
186
187 } // namespace boost
188
189 #ifdef BOOST_MSVC
190 # pragma warning(pop)
191 #endif    
192
193 #endif  // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED