]> git.lyx.org Git - lyx.git/blob - boost/boost/weak_ptr.hpp
complie fix
[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 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<typename 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()
39     {
40     }
41
42 //  generated copy constructor, assignment, destructor are fine
43
44     template<typename Y>
45     weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
46     {
47     }
48
49     template<typename Y>
50     weak_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
51     {
52     }
53
54 #if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
55
56     template<typename Y>
57     weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
58     {
59         px = r.px;
60         pn = r.pn;
61         return *this;
62     }
63
64     template<typename Y>
65     weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
66     {
67         px = r.px;
68         pn = r.pn;
69         return *this;
70     }
71
72 #endif
73
74     void reset()
75     {
76         this_type().swap(*this);
77     }
78
79     T * get() const // never throws; deprecated, removal pending, don't use
80     {
81         return pn.use_count() == 0? 0: px;
82     }
83
84     long use_count() const // never throws
85     {
86         return pn.use_count();
87     }
88
89     bool expired() const // never throws
90     {
91         return pn.use_count() == 0;
92     }
93
94     void swap(this_type & other) // never throws
95     {
96         std::swap(px, other.px);
97         pn.swap(other.pn);
98     }
99
100     bool less(this_type const & rhs) const // implementation detail, never throws
101     {
102         return pn < rhs.pn;
103     }
104
105 // Tasteless as this may seem, making all members public allows member templates
106 // to work in the absence of member template friends. (Matthew Langston)
107
108 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
109
110 private:
111
112     template<typename Y> friend class weak_ptr;
113     template<typename Y> friend class shared_ptr;
114
115 #endif
116
117     T * px;                     // contained pointer
118     detail::weak_count pn;      // reference counter
119
120 };  // weak_ptr
121
122 template<class T, class U> inline bool operator==(weak_ptr<T> const & a, weak_ptr<U> const & b)
123 {
124     return a.get() == b.get();
125 }
126
127 template<class T, class U> inline bool operator!=(weak_ptr<T> const & a, weak_ptr<U> const & b)
128 {
129     return a.get() != b.get();
130 }
131
132 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
133
134 // Resolve the ambiguity between our op!= and the one in rel_ops
135
136 template<typename T> inline bool operator!=(weak_ptr<T> const & a, weak_ptr<T> const & b)
137 {
138     return a.get() != b.get();
139 }
140
141 #endif
142
143 template<class T> inline bool operator<(weak_ptr<T> const & a, weak_ptr<T> const & b)
144 {
145     return a.less(b);
146 }
147
148 template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
149 {
150     a.swap(b);
151 }
152
153 template<class T> shared_ptr<T> make_shared(weak_ptr<T> const & r) // never throws
154 {
155     // optimization: avoid throw overhead
156     if(r.use_count() == 0)
157     {
158         return shared_ptr<T>();
159     }
160
161     try
162     {
163         return shared_ptr<T>(r);
164     }
165     catch(use_count_is_zero const &)
166     {
167         return shared_ptr<T>();
168     }
169 }
170
171 // Note: there is no get_pointer overload for weak_ptr.
172 // This is intentional. Even get() will disappear in a
173 // future release; these accessors are too error-prone.
174
175 } // namespace boost
176
177 #ifdef BOOST_MSVC
178 # pragma warning(pop)
179 #endif    
180
181 #endif  // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED