]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr.hpp
some new boost files that we now use
[lyx.git] / boost / boost / smart_ptr.hpp
1 // Baruch Even  baruch@ev-en.org  2001-02-20
2 //  This version is a modified version for use in LyX
3 //  The modifications are done in order to use it where exceptions are disabled
4 //  Currently it has no "no memory" checks in place, asserts are probably the
5 //  only real way.
6 //  all changed are #ifded'ed by LYX_NO_EXCEPTIONS
7 #define LYX_NO_EXCEPTIONS
8
9 //  Boost smart_ptr.hpp header file  -----------------------------------------//
10
11 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Permission to copy,
12 //  use, modify, sell and distribute this software is granted provided this
13 //  copyright notice appears in all copies. This software is provided "as is"
14 //  without express or implied warranty, and with no claim as to its
15 //  suitability for any purpose.
16
17 //  See http://www.boost.org for most recent version including documentation.
18
19 //  Revision History
20 //  21 May 01  Require complete type where incomplete type is unsafe.
21 //             (suggested by Vladimir Prus)
22 //  21 May 01  operator= fails if operand transitively owned by *this, as in a
23 //             linked list (report by Ken Johnson, fix by Beman Dawes)
24 //  21 Jan 01  Suppress some useless warnings with MSVC (David Abrahams)
25 //  19 Oct 00  Make shared_ptr ctor from auto_ptr explicit. (Robert Vugts) 
26 //  24 Jul 00  Change throw() to // never throws.  See lib guidelines
27 //             Exception-specification rationale. (Beman Dawes)
28 //  22 Jun 00  Remove #if continuations to fix GCC 2.95.2 problem (Beman Dawes)
29 //   1 Feb 00  Additional shared_ptr BOOST_NO_MEMBER_TEMPLATES workarounds
30 //             (Dave Abrahams)
31 //  31 Dec 99  Condition tightened for no member template friend workaround
32 //             (Dave Abrahams)
33 //  30 Dec 99  Moved BOOST_NMEMBER_TEMPLATES compatibility code to config.hpp
34 //             (Dave Abrahams)
35 //  30 Nov 99  added operator ==, operator !=, and std::swap and std::less
36 //             specializations for shared types (Darin Adler)
37 //  11 Oct 99  replaced op[](int) with op[](std::size_t) (Ed Brey, Valentin
38 //             Bonnard), added shared_ptr workaround for no member template
39 //             friends (Matthew Langston)
40 //  25 Sep 99  added shared_ptr::swap and shared_array::swap (Luis Coelho).
41 //  20 Jul 99  changed name to smart_ptr.hpp, #include <boost/config.hpp>,
42 //             #include <boost/utility.hpp> and use boost::noncopyable
43 //  17 May 99  remove scoped_array and shared_array operator*() as
44 //             unnecessary (Beman Dawes)
45 //  14 May 99  reorder code so no effects when bad_alloc thrown (Abrahams/Dawes)
46 //  13 May 99  remove certain throw() specifiers to avoid generated try/catch
47 //             code cost (Beman Dawes)
48 //  11 May 99  get() added, conversion to T* placed in macro guard (Valentin
49 //             Bonnard, Dave Abrahams, and others argued for elimination
50 //             of the automatic conversion)
51 //  28 Apr 99  #include <memory> fix (Valentin Bonnard)
52 //  28 Apr 99  rename transfer() to share() for clarity (Dave Abrahams)
53 //  28 Apr 99  remove unsafe shared_array template conversions(Valentin Bonnard)
54 //  28 Apr 99  p(r) changed to p(r.px) for clarity (Dave Abrahams)
55 //  21 Apr 99  reset() self assignment fix (Valentin Bonnard)
56 //  21 Apr 99  dispose() provided to improve clarity (Valentin Bonnard)
57 //  27 Apr 99  leak when new throws fixes (Dave Abrahams)
58 //  21 Oct 98  initial Version (Greg Colvin/Beman Dawes)
59
60 #ifndef BOOST_SMART_PTR_HPP
61 #define BOOST_SMART_PTR_HPP
62
63 #include <boost/config.hpp>   // for broken compiler workarounds
64 #include <cstddef>            // for std::size_t
65 #include <memory>             // for std::auto_ptr
66 #include <algorithm>          // for std::swap
67 #include <boost/utility.hpp>  // for boost::noncopyable, checked_delete, checked_array_delete
68 #include <functional>         // for std::less
69 #include <boost/static_assert.hpp> // for BOOST_STATIC_ASSERT
70
71 #ifdef LYX_NO_EXCEPTIONS
72 #include <assert.h>
73 #endif
74
75 #ifdef BOOST_MSVC  // moved here to work around VC++ compiler crash
76 # pragma warning(push)
77 # pragma warning(disable:4284) // return type for 'identifier::operator->' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation
78 #endif
79
80 namespace boost {
81
82 //  scoped_ptr  --------------------------------------------------------------//
83
84 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
85 //  of the object pointed to, either on destruction of the scoped_ptr or via
86 //  an explicit reset().  scoped_ptr is a simple solution for simple needs;
87 //  see shared_ptr (below) or std::auto_ptr if your needs are more complex.
88
89 template<typename T> class scoped_ptr : noncopyable {
90
91   T* ptr;
92
93  public:
94   typedef T element_type;
95
96   explicit scoped_ptr( T* p=0 ) : ptr(p) {}  // never throws
97   ~scoped_ptr()                 { checked_delete(ptr); }
98   void reset( T* p=0 )          { if ( ptr != p ) { checked_delete(ptr); ptr = p; } }
99   T& operator*() const          { return *ptr; }  // never throws
100   T* operator->() const         { return ptr; }  // never throws
101   T* get() const                { return ptr; }  // never throws
102 #ifdef BOOST_SMART_PTR_CONVERSION
103   // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
104   operator T*() const           { return ptr; }  // never throws 
105 #endif
106   };  // scoped_ptr
107
108 //  scoped_array  ------------------------------------------------------------//
109
110 //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
111 //  is guaranteed, either on destruction of the scoped_array or via an explicit
112 //  reset(). See shared_array or std::vector if your needs are more complex.
113
114 template<typename T> class scoped_array : noncopyable {
115
116   T* ptr;
117
118  public:
119   typedef T element_type;
120
121   explicit scoped_array( T* p=0 ) : ptr(p) {}  // never throws
122   ~scoped_array()                    { checked_array_delete(ptr); }
123
124   void reset( T* p=0 )               { if ( ptr != p )
125                                          {checked_array_delete(ptr); ptr=p;} }
126
127   T* get() const                     { return ptr; }  // never throws
128 #ifdef BOOST_SMART_PTR_CONVERSION
129   // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
130   operator T*() const                { return ptr; }  // never throws
131 #else 
132   T& operator[](std::size_t i) const { return ptr[i]; }  // never throws
133 #endif
134   };  // scoped_array
135
136 //  shared_ptr  --------------------------------------------------------------//
137
138 //  An enhanced relative of scoped_ptr with reference counted copy semantics.
139 //  The object pointed to is deleted when the last shared_ptr pointing to it
140 //  is destroyed or reset.
141
142 template<typename T> class shared_ptr {
143   public:
144    typedef T element_type;
145
146    explicit shared_ptr(T* p =0) : px(p) {
147 #ifndef LYX_NO_EXCEPTIONS
148       try { pn = new long(1); }  // fix: prevent leak if new throws
149       catch (...) { checked_delete(p); throw; } 
150 #else
151           pn = new long(1);
152           assert(pn != 0);
153 #endif
154    }
155
156    shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); }  // never throws
157
158    ~shared_ptr() { dispose(); }
159
160    shared_ptr& operator=(const shared_ptr& r) {
161       share(r.px,r.pn);
162       return *this;
163    }
164
165 #if !defined( BOOST_NO_MEMBER_TEMPLATES )
166    template<typename Y>
167       shared_ptr(const shared_ptr<Y>& r) : px(r.px) {  // never throws 
168          ++*(pn = r.pn); 
169       }
170 #ifndef BOOST_NO_AUTO_PTR
171    template<typename Y>
172       explicit shared_ptr(std::auto_ptr<Y>& r) { 
173          pn = new long(1); // may throw
174          px = r.release(); // fix: moved here to stop leak if new throws
175       }
176 #endif 
177
178    template<typename Y>
179       shared_ptr& operator=(const shared_ptr<Y>& r) { 
180          share(r.px,r.pn);
181          return *this;
182       }
183
184 #ifndef BOOST_NO_AUTO_PTR
185    template<typename Y>
186       shared_ptr& operator=(std::auto_ptr<Y>& r) {
187          // code choice driven by guarantee of "no effect if new throws"
188          if (*pn == 1) { checked_delete(px); }
189          else { // allocate new reference counter
190            long * tmp = new long(1); // may throw
191            --*pn; // only decrement once danger of new throwing is past
192            pn = tmp;
193          } // allocate new reference counter
194          px = r.release(); // fix: moved here so doesn't leak if new throws 
195          return *this;
196       }
197 #endif
198 #else
199 #ifndef BOOST_NO_AUTO_PTR
200       explicit shared_ptr(std::auto_ptr<T>& r) { 
201          pn = new long(1); // may throw
202          px = r.release(); // fix: moved here to stop leak if new throws
203       } 
204
205       shared_ptr& operator=(std::auto_ptr<T>& r) {
206          // code choice driven by guarantee of "no effect if new throws"
207          if (*pn == 1) { checked_delete(px); }
208          else { // allocate new reference counter
209            long * tmp = new long(1); // may throw
210            --*pn; // only decrement once danger of new throwing is past
211            pn = tmp;
212          } // allocate new reference counter
213          px = r.release(); // fix: moved here so doesn't leak if new throws 
214          return *this;
215       }
216 #endif
217 #endif
218
219    void reset(T* p=0) {
220       if ( px == p ) return;  // fix: self-assignment safe
221       if (--*pn == 0) { checked_delete(px); }
222       else { // allocate new reference counter
223 #ifndef LYX_NO_EXCEPTIONS                 
224         try { pn = new long; }  // fix: prevent leak if new throws
225         catch (...) {
226           ++*pn;  // undo effect of --*pn above to meet effects guarantee 
227           checked_delete(p);
228           throw;
229         } // catch
230 #else
231                 pn = new long;
232                 assert(pn != 0);
233 #endif
234       } // allocate new reference counter
235       *pn = 1;
236       px = p;
237    } // reset
238
239    T& operator*() const          { return *px; }  // never throws
240    T* operator->() const         { return px; }  // never throws
241    T* get() const                { return px; }  // never throws
242  #ifdef BOOST_SMART_PTR_CONVERSION
243    // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
244    operator T*() const           { return px; }  // never throws 
245  #endif
246
247    long use_count() const        { return *pn; }  // never throws
248    bool unique() const           { return *pn == 1; }  // never throws
249
250    void swap(shared_ptr<T>& other)  // never throws
251      { std::swap(px,other.px); std::swap(pn,other.pn); }
252
253 // Tasteless as this may seem, making all members public allows member templates
254 // to work in the absence of member template friends. (Matthew Langston)
255 // Don't split this line into two; that causes problems for some GCC 2.95.2 builds
256 #if defined(BOOST_NO_MEMBER_TEMPLATES) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )
257    private:
258 #endif
259
260    T*     px;     // contained pointer
261    long*  pn;     // ptr to reference counter
262
263 // Don't split this line into two; that causes problems for some GCC 2.95.2 builds
264 #if !defined( BOOST_NO_MEMBER_TEMPLATES ) && !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )
265    template<typename Y> friend class shared_ptr;
266 #endif
267
268    void dispose() { if (--*pn == 0) { checked_delete(px); delete pn; } }
269
270    void share(T* rpx, long* rpn) {
271       if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0
272          ++*rpn; // done before dispose() in case rpn transitively
273                  // dependent on *this (bug reported by Ken Johnson)
274          dispose();
275          px = rpx;
276          pn = rpn;
277       }
278    } // share
279 };  // shared_ptr
280
281 template<typename T, typename U>
282   inline bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b)
283     { return a.get() == b.get(); }
284
285 template<typename T, typename U>
286   inline bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b)
287     { return a.get() != b.get(); }
288
289 //  shared_array  ------------------------------------------------------------//
290
291 //  shared_array extends shared_ptr to arrays.
292 //  The array pointed to is deleted when the last shared_array pointing to it
293 //  is destroyed or reset.
294
295 template<typename T> class shared_array {
296   public:
297    typedef T element_type;
298
299    explicit shared_array(T* p =0) : px(p) {
300 #ifndef LYX_NO_EXCEPTIONS
301       try { pn = new long(1); }  // fix: prevent leak if new throws
302       catch (...) { checked_array_delete(p); throw; } 
303 #else
304           pn = new long(1);
305           assert(pn != 0);
306 #endif
307    }
308
309    shared_array(const shared_array& r) : px(r.px)  // never throws
310       { ++*(pn = r.pn); }
311
312    ~shared_array() { dispose(); }
313
314    shared_array& operator=(const shared_array& r) {
315       if (pn != r.pn) { // Q: why not px != r.px? A: fails when both px == 0
316          ++*r.pn; // done before dispose() in case r.pn transitively
317                   // dependent on *this (bug reported by Ken Johnson)
318          dispose();
319          px = r.px;
320          pn = r.pn;
321       }
322       return *this;
323    } // operator=
324
325    void reset(T* p=0) {
326       if ( px == p ) return;  // fix: self-assignment safe
327       if (--*pn == 0) { checked_array_delete(px); }
328       else { // allocate new reference counter
329 #ifndef LYX_NO_EXCEPTIONS
330         try { pn = new long; }  // fix: prevent leak if new throws
331         catch (...) {
332           ++*pn;  // undo effect of --*pn above to meet effects guarantee 
333           checked_array_delete(p);
334           throw;
335         } // catch
336 #else
337                 pn = new long;
338                 assert(pn != 0);
339 #endif
340       } // allocate new reference counter
341       *pn = 1;
342       px = p;
343    } // reset
344
345    T* get() const                     { return px; }  // never throws
346  #ifdef BOOST_SMART_PTR_CONVERSION
347    // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
348    operator T*() const                { return px; }  // never throws
349  #else 
350    T& operator[](std::size_t i) const { return px[i]; }  // never throws
351  #endif
352
353    long use_count() const             { return *pn; }  // never throws
354    bool unique() const                { return *pn == 1; }  // never throws
355
356    void swap(shared_array<T>& other)  // never throws
357      { std::swap(px,other.px); std::swap(pn,other.pn); }
358
359   private:
360
361    T*     px;     // contained pointer
362    long*  pn;     // ptr to reference counter
363
364    void dispose() { if (--*pn == 0) { checked_array_delete(px); delete pn; } }
365
366 };  // shared_array
367
368 template<typename T>
369   inline bool operator==(const shared_array<T>& a, const shared_array<T>& b)
370     { return a.get() == b.get(); }
371
372 template<typename T>
373   inline bool operator!=(const shared_array<T>& a, const shared_array<T>& b)
374     { return a.get() != b.get(); }
375
376 } // namespace boost
377
378 //  specializations for things in namespace std  -----------------------------//
379
380 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
381
382 namespace std {
383
384 // Specialize std::swap to use the fast, non-throwing swap that's provided
385 // as a member function instead of using the default algorithm which creates
386 // a temporary and uses assignment.
387
388 template<typename T>
389   inline void swap(boost::shared_ptr<T>& a, boost::shared_ptr<T>& b)
390     { a.swap(b); }
391
392 template<typename T>
393   inline void swap(boost::shared_array<T>& a, boost::shared_array<T>& b)
394     { a.swap(b); }
395
396 // Specialize std::less so we can use shared pointers and arrays as keys in
397 // associative collections.
398
399 // It's still a controversial question whether this is better than supplying
400 // a full range of comparison operators (<, >, <=, >=).
401
402 template<typename T>
403   struct less< boost::shared_ptr<T> >
404     : binary_function<boost::shared_ptr<T>, boost::shared_ptr<T>, bool>
405   {
406     bool operator()(const boost::shared_ptr<T>& a,
407         const boost::shared_ptr<T>& b) const
408       { return less<T*>()(a.get(),b.get()); }
409   };
410
411 template<typename T>
412   struct less< boost::shared_array<T> >
413     : binary_function<boost::shared_array<T>, boost::shared_array<T>, bool>
414   {
415     bool operator()(const boost::shared_array<T>& a,
416         const boost::shared_array<T>& b) const
417       { return less<T*>()(a.get(),b.get()); }
418   };
419
420 } // namespace std
421
422 #endif  // ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
423
424 #ifdef BOOST_MSVC
425 #pragma warning(pop)
426 #endif
427
428 #endif  // BOOST_SMART_PTR_HPP
429
430