]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr.hpp
simplify some, ws, begin minibuffer simplification
[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 Jan 01  Suppress some useless warnings with MSVC (David Abrahams)
21 //  19 Oct 00  Make shared_ptr ctor from auto_ptr explicit. (Robert Vugts) 
22 //  24 Jul 00  Change throw() to // never throws.  See lib guidelines
23 //             Exception-specification rationale. (Beman Dawes)
24 //  22 Jun 00  Remove #if continuations to fix GCC 2.95.2 problem (Beman Dawes)
25 //   1 Feb 00  Additional shared_ptr BOOST_NO_MEMBER_TEMPLATES workarounds
26 //             (Dave Abrahams)
27 //  31 Dec 99  Condition tightened for no member template friend workaround
28 //             (Dave Abrahams)
29 //  30 Dec 99  Moved BOOST_NMEMBER_TEMPLATES compatibility code to config.hpp
30 //             (Dave Abrahams)
31 //  30 Nov 99  added operator ==, operator !=, and std::swap and std::less
32 //             specializations for shared types (Darin Adler)
33 //  11 Oct 99  replaced op[](int) with op[](std::size_t) (Ed Brey, Valentin
34 //             Bonnard), added shared_ptr workaround for no member template
35 //             friends (Matthew Langston)
36 //  25 Sep 99  added shared_ptr::swap and shared_array::swap (Luis Coelho).
37 //  20 Jul 99  changed name to smart_ptr.hpp, #include <boost/config.hpp>,
38 //             #include <boost/utility.hpp> and use boost::noncopyable
39 //  17 May 99  remove scoped_array and shared_array operator*() as
40 //             unnecessary (Beman Dawes)
41 //  14 May 99  reorder code so no effects when bad_alloc thrown (Abrahams/Dawes)
42 //  13 May 99  remove certain throw() specifiers to avoid generated try/catch
43 //             code cost (Beman Dawes)
44 //  11 May 99  get() added, conversion to T* placed in macro guard (Valentin
45 //             Bonnard, Dave Abrahams, and others argued for elimination
46 //             of the automatic conversion)
47 //  28 Apr 99  #include <memory> fix (Valentin Bonnard)
48 //  28 Apr 99  rename transfer() to share() for clarity (Dave Abrahams)
49 //  28 Apr 99  remove unsafe shared_array template conversions(Valentin Bonnard)
50 //  28 Apr 99  p(r) changed to p(r.px) for clarity (Dave Abrahams)
51 //  21 Apr 99  reset() self assignment fix (Valentin Bonnard)
52 //  21 Apr 99  dispose() provided to improve clarity (Valentin Bonnard)
53 //  27 Apr 99  leak when new throws fixes (Dave Abrahams)
54 //  21 Oct 98  initial Version (Greg Colvin/Beman Dawes)
55
56 #ifndef BOOST_SMART_PTR_HPP
57 #define BOOST_SMART_PTR_HPP
58
59 #include <boost/config.hpp>   // for broken compiler workarounds
60 #include <cstddef>            // for std::size_t
61 #include <memory>             // for std::auto_ptr
62 #include <algorithm>          // for std::swap
63 #include <boost/utility.hpp>  // for boost::noncopyable
64 #include <functional>         // for std::less
65
66 #ifdef LYX_NO_EXCEPTIONS
67 #include <assert.h>
68 #endif
69
70 namespace boost {
71
72 //  scoped_ptr  --------------------------------------------------------------//
73
74 //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
75 //  of the object pointed to, either on destruction of the scoped_ptr or via
76 //  an explicit reset().  scoped_ptr is a simple solution for simple needs;
77 //  see shared_ptr (below) or std::auto_ptr if your needs are more complex.
78
79 template<typename T> class scoped_ptr : noncopyable {
80
81   T* ptr;
82
83  public:
84   typedef T element_type;
85
86   explicit scoped_ptr( T* p=0 ) : ptr(p) {}  // never throws
87   ~scoped_ptr()                 { delete ptr; }
88
89   void reset( T* p=0 )          { if ( ptr != p ) { delete ptr; ptr = p; } }
90   T& operator*() const          { return *ptr; }  // never throws
91 #ifdef BOOST_MSVC
92 # pragma warning(push)
93 # 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
94 #endif    
95   T* operator->() const         { return ptr; }  // never throws
96 #ifdef BOOST_MSVC
97 # pragma warning(pop)
98 #endif    
99   T* get() const                { return ptr; }  // never throws
100 #ifdef BOOST_SMART_PTR_CONVERSION
101   // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
102   operator T*() const           { return ptr; }  // never throws 
103 #endif
104   };  // scoped_ptr
105
106 //  scoped_array  ------------------------------------------------------------//
107
108 //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
109 //  is guaranteed, either on destruction of the scoped_array or via an explicit
110 //  reset(). See shared_array or std::vector if your needs are more complex.
111
112 template<typename T> class scoped_array : noncopyable {
113
114   T* ptr;
115
116  public:
117   typedef T element_type;
118
119   explicit scoped_array( T* p=0 ) : ptr(p) {}  // never throws
120   ~scoped_array()                    { delete [] ptr; }
121
122   void reset( T* p=0 )               { if ( ptr != p ) {delete [] ptr; ptr=p;} }
123
124   T* get() const                     { return ptr; }  // never throws
125 #ifdef BOOST_SMART_PTR_CONVERSION
126   // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
127   operator T*() const                { return ptr; }  // never throws
128 #else 
129   T& operator[](std::size_t i) const { return ptr[i]; }  // never throws
130 #endif
131   };  // scoped_array
132
133 //  shared_ptr  --------------------------------------------------------------//
134
135 //  An enhanced relative of scoped_ptr with reference counted copy semantics.
136 //  The object pointed to is deleted when the last shared_ptr pointing to it
137 //  is destroyed or reset.
138
139 template<typename T> class shared_ptr {
140   public:
141    typedef T element_type;
142
143    explicit shared_ptr(T* p =0) : px(p) {
144 #ifndef LYX_NO_EXCEPTIONS
145       try { pn = new long(1); }  // fix: prevent leak if new throws
146       catch (...) { delete p; throw; } 
147 #else
148           pn = new long(1);
149           assert(pn != 0);
150 #endif
151    }
152
153    shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); }  // never throws
154
155    ~shared_ptr() { dispose(); }
156
157    shared_ptr& operator=(const shared_ptr& r) {
158       share(r.px,r.pn);
159       return *this;
160    }
161
162 #if !defined( BOOST_NO_MEMBER_TEMPLATES )
163    template<typename Y>
164       shared_ptr(const shared_ptr<Y>& r) : px(r.px) {  // never throws 
165          ++*(pn = r.pn); 
166       }
167 #ifndef BOOST_NO_AUTO_PTR
168    template<typename Y>
169       explicit shared_ptr(std::auto_ptr<Y>& r) { 
170          pn = new long(1); // may throw
171          px = r.release(); // fix: moved here to stop leak if new throws
172       }
173 #endif 
174
175    template<typename Y>
176       shared_ptr& operator=(const shared_ptr<Y>& r) { 
177          share(r.px,r.pn);
178          return *this;
179       }
180
181 #ifndef BOOST_NO_AUTO_PTR
182    template<typename Y>
183       shared_ptr& operator=(std::auto_ptr<Y>& r) {
184          // code choice driven by guarantee of "no effect if new throws"
185          if (*pn == 1) { delete px; }
186          else { // allocate new reference counter
187            long * tmp = new long(1); // may throw
188            --*pn; // only decrement once danger of new throwing is past
189            pn = tmp;
190          } // allocate new reference counter
191          px = r.release(); // fix: moved here so doesn't leak if new throws 
192          return *this;
193       }
194 #endif
195 #else
196 #ifndef BOOST_NO_AUTO_PTR
197       explicit shared_ptr(std::auto_ptr<T>& r) { 
198          pn = new long(1); // may throw
199          px = r.release(); // fix: moved here to stop leak if new throws
200       } 
201
202       shared_ptr& operator=(std::auto_ptr<T>& r) {
203          // code choice driven by guarantee of "no effect if new throws"
204          if (*pn == 1) { delete px; }
205          else { // allocate new reference counter
206            long * tmp = new long(1); // may throw
207            --*pn; // only decrement once danger of new throwing is past
208            pn = tmp;
209          } // allocate new reference counter
210          px = r.release(); // fix: moved here so doesn't leak if new throws 
211          return *this;
212       }
213 #endif
214 #endif
215
216    void reset(T* p=0) {
217       if ( px == p ) return;  // fix: self-assignment safe
218       if (--*pn == 0) { delete px; }
219       else { // allocate new reference counter
220 #ifndef LYX_NO_EXCEPTIONS                 
221         try { pn = new long; }  // fix: prevent leak if new throws
222         catch (...) {
223           ++*pn;  // undo effect of --*pn above to meet effects guarantee 
224           delete p;
225           throw;
226         } // catch
227 #else
228                 pn = new long;
229                 assert(pn != 0);
230 #endif
231       } // allocate new reference counter
232       *pn = 1;
233       px = p;
234    } // reset
235
236    T& operator*() const          { return *px; }  // never throws
237 #ifdef BOOST_MSVC
238 # pragma warning(push)
239 # 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
240 #endif    
241    T* operator->() const         { return px; }  // never throws
242 #ifdef BOOST_MSVC
243 # pragma warning(pop)
244 #endif    
245    T* get() const                { return px; }  // never throws
246  #ifdef BOOST_SMART_PTR_CONVERSION
247    // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
248    operator T*() const           { return px; }  // never throws 
249  #endif
250
251    long use_count() const        { return *pn; }  // never throws
252    bool unique() const           { return *pn == 1; }  // never throws
253
254    void swap(shared_ptr<T>& other)  // never throws
255      { std::swap(px,other.px); std::swap(pn,other.pn); }
256
257 // Tasteless as this may seem, making all members public allows member templates
258 // to work in the absence of member template friends. (Matthew Langston)
259 // Don't split this line into two; that causes problems for some GCC 2.95.2 builds
260 #if defined(BOOST_NO_MEMBER_TEMPLATES) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )
261    private:
262 #endif
263
264    T*     px;     // contained pointer
265    long*  pn;     // ptr to reference counter
266
267 // Don't split this line into two; that causes problems for some GCC 2.95.2 builds
268 #if !defined( BOOST_NO_MEMBER_TEMPLATES ) && !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )
269    template<typename Y> friend class shared_ptr;
270 #endif
271
272    void dispose() { if (--*pn == 0) { delete px; delete pn; } }
273
274    void share(T* rpx, long* rpn) {
275       if (pn != rpn) {
276          dispose();
277          px = rpx;
278          ++*(pn = rpn);
279       }
280    } // share
281 };  // shared_ptr
282
283 template<typename T, typename U>
284   inline bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b)
285     { return a.get() == b.get(); }
286
287 template<typename T, typename U>
288   inline bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b)
289     { return a.get() != b.get(); }
290
291 //  shared_array  ------------------------------------------------------------//
292
293 //  shared_array extends shared_ptr to arrays.
294 //  The array pointed to is deleted when the last shared_array pointing to it
295 //  is destroyed or reset.
296
297 template<typename T> class shared_array {
298   public:
299    typedef T element_type;
300
301    explicit shared_array(T* p =0) : px(p) {
302 #ifndef LYX_NO_EXCEPTIONS
303       try { pn = new long(1); }  // fix: prevent leak if new throws
304       catch (...) { delete [] p; throw; } 
305 #else
306           pn = new long(1);
307           assert(pn != 0);
308 #endif
309    }
310
311    shared_array(const shared_array& r) : px(r.px)  // never throws
312       { ++*(pn = r.pn); }
313
314    ~shared_array() { dispose(); }
315
316    shared_array& operator=(const shared_array& r) {
317       if (pn != r.pn) {
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) { 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           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) { 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 #endif  // BOOST_SMART_PTR_HPP
425
426