OSDN Git Service

cfae38da17a4736f75c303ffa803bc7da9fb827c
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / memory
1 // <tr1/memory> -*- C++ -*-
2
3 // Copyright (C) 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //  boost/checked_delete.hpp
31 //  Copyright (c) 2002, 2003 Peter Dimov
32 //  Copyright (c) 2003 Daniel Frey
33 //  Copyright (c) 2003 Howard Hinnant
34
35 //  boost/shared_count.hpp
36 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
37
38 //  shared_ptr.hpp
39 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
40 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
41
42 //  weak_ptr.hpp
43 //  Copyright (c) 2001, 2002, 2003 Peter Dimov
44
45 //  enable_shared_from_this.hpp
46 //  Copyright (c) 2002 Peter Dimov
47
48 #ifndef _TR1_MEMORY
49 #define _TR1_MEMORY 1
50
51 #include "../memory"
52 #include <functional>       // std::less
53 #include <exception>        // std::exception
54 #include <new>              // std::bad_alloc
55 #include <typeinfo>         // std::type_info in get_deleter
56 #include <cstddef>          // std::size_t
57 #include <algorithm>        // for std::swap
58 #include <iosfwd>           // for std::basic_ostream
59 #include <cstdlib>          // for std::abort
60
61 #include <bits/gthr.h>
62 #include <bits/atomicity.h>
63 #include <bits/functexcept.h>
64 #include <debug/debug.h>
65
66 // namespace std::tr1
67 namespace std
68 {
69 namespace tr1
70 {
71
72 class bad_weak_ptr : public std::exception
73 {
74 public:
75
76   virtual char const* what() const throw()
77   {
78     return "tr1::bad_weak_ptr";
79   }
80 };
81
82 // Helper for exception objects in <tr1/memory>
83 // TODO this should be defined in a different file.
84 inline void
85 __throw_bad_weak_ptr()
86 {
87 #if __EXCEPTIONS
88   throw bad_weak_ptr();
89 #else
90   std::abort();
91 #endif
92 }
93
94
95 // verify that types are complete for increased safety
96
97 template <typename _Tp>
98   inline void
99   checked_delete(_Tp * __x)
100   {
101     // intentionally complex - simplification causes regressions
102     typedef char type_must_be_complete[ sizeof(_Tp)? 1: -1 ];
103     (void) sizeof(type_must_be_complete);
104     delete __x;
105   }
106
107 template <typename _Tp>
108   struct checked_deleter
109   {
110     typedef void result_type;
111     typedef _Tp* argument_type;
112
113     void
114     operator()(_Tp * x) const
115     { std::tr1::checked_delete(x); }
116   };
117
118
119 class _Sp_counted_base
120 {
121 public:
122
123   _Sp_counted_base()
124   : _M_use_count(1), _M_weak_count(1)
125   { }
126
127   virtual
128   ~_Sp_counted_base() // nothrow
129   { }
130
131   // dispose() is called when _M_use_count drops to zero, to release
132   // the resources managed by *this.
133   virtual void
134   dispose() = 0; // nothrow
135
136   // destroy() is called when _M_weak_count drops to zero.
137   virtual void
138   destroy() // nothrow
139   {
140     delete this;
141   }
142
143   virtual void*
144   get_deleter(const std::type_info&) = 0;
145
146   void
147   add_ref_copy()
148   {
149     __gnu_cxx::__atomic_add(&_M_use_count, 1);
150   }
151
152   void
153   add_ref_lock()
154   {
155     if (_M_use_count <= 0) // TODO not yet MT safe XXX
156     {
157       __throw_bad_weak_ptr();
158     }
159     __gnu_cxx::__atomic_add(&_M_use_count, 1);
160   }
161
162   void
163   release() // nothrow
164   {
165     if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) <= 1)
166     {
167       dispose();
168       weak_release();
169     }
170   }
171
172   void
173   weak_add_ref() // nothrow
174   {
175     __gnu_cxx::__atomic_add(&_M_weak_count, 1);
176   }
177
178   void
179   weak_release() // nothrow
180   {
181     if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) <= 1)
182     {
183       destroy();
184     }
185   }
186
187   long
188   use_count() const // nothrow
189   {
190     return _M_use_count;  // XXX is this MT safe?
191   }
192
193 private:
194
195   _Sp_counted_base(_Sp_counted_base const&);
196   _Sp_counted_base& operator= (_Sp_counted_base const&);
197
198   _Atomic_word _M_use_count;        // #shared
199   _Atomic_word _M_weak_count;       // #weak + (#shared != 0)
200 };
201
202 template <typename _Ptr, typename _Deleter>
203 class _Sp_counted_base_impl : public _Sp_counted_base
204 {
205 public:
206
207   /**
208    *  @brief   
209    *  @pre     d(p) must not throw.
210    */
211   _Sp_counted_base_impl(_Ptr __p, _Deleter __d)
212   : _M_ptr(__p), _M_del(__d)
213   { }
214
215   virtual void
216   dispose() // nothrow
217   {
218     _M_del(_M_ptr);
219   }
220
221   virtual void*
222   get_deleter(const std::type_info& __ti)
223   {
224     return __ti == typeid(_Deleter) ? &_M_del : 0;
225   }
226
227 private:
228   _Sp_counted_base_impl(const _Sp_counted_base_impl&);
229   _Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
230
231   _Ptr     _M_ptr; // copy constructor must not throw
232   _Deleter _M_del; // copy constructor must not throw
233 };
234
235 class weak_count;
236
237 class shared_count
238 {
239 private:
240
241   _Sp_counted_base* _M_pi;
242
243   friend class weak_count;
244
245 public:
246
247   shared_count()
248   : _M_pi(0) // nothrow
249   { }
250
251   template <typename _Ptr, typename _Deleter>
252     shared_count(_Ptr __p, _Deleter __d)
253     : _M_pi(0)
254     {
255       try
256       {
257         _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
258       }
259       catch(...)
260       {
261         __d(__p); // delete __p
262         __throw_exception_again;
263       }
264     }
265
266   // auto_ptr<_Tp> is special cased to provide the strong guarantee
267
268   template <typename _Tp>
269     explicit shared_count(std::auto_ptr<_Tp>& __r)
270     : _M_pi(new _Sp_counted_base_impl<_Tp*,checked_deleter<_Tp> >(
271             __r.get(), checked_deleter<_Tp>()
272             ))
273     { __r.release(); }
274
275   // throws bad_weak_ptr when __r.use_count() == 0
276   explicit shared_count(const weak_count& __r);
277
278   ~shared_count() // nothrow
279   {
280     if (_M_pi != 0)
281       _M_pi->release();
282   }
283
284   shared_count(const shared_count& __r)
285   : _M_pi(__r._M_pi) // nothrow
286   {
287     if (_M_pi != 0)
288       _M_pi->add_ref_copy();
289   }
290
291   shared_count&
292   operator=(const shared_count& __r) // nothrow
293   {
294     _Sp_counted_base* __tmp = __r._M_pi;
295
296     if(__tmp != _M_pi)
297     {
298       if(__tmp != 0)
299         __tmp->add_ref_copy();
300       if(_M_pi != 0)
301         _M_pi->release();
302       _M_pi = __tmp;
303     }
304     return *this;
305   }
306
307   void swap(shared_count& __r) // nothrow
308   {
309     _Sp_counted_base* __tmp = __r._M_pi;
310     __r._M_pi = _M_pi;
311     _M_pi = __tmp;
312   }
313
314   long
315   use_count() const // nothrow
316   { return _M_pi != 0 ? _M_pi->use_count() : 0; }
317
318   bool
319   unique() const // nothrow
320   { return this->use_count() == 1; }
321
322   friend inline bool
323   operator==(const shared_count& __a, const shared_count& __b)
324   { return __a._M_pi == __b._M_pi; }
325
326   friend inline bool
327   operator<(const shared_count& __a, const shared_count& __b)
328   { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
329
330   void*
331   get_deleter(const std::type_info& __ti) const
332   { return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
333 };
334
335
336 class weak_count
337 {
338 private:
339
340   _Sp_counted_base * _M_pi;
341
342   friend class shared_count;
343
344 public:
345
346   weak_count()
347   : _M_pi(0) // nothrow
348   { }
349
350   weak_count(const shared_count& __r)
351   : _M_pi(__r._M_pi) // nothrow
352   {
353     if (_M_pi != 0)
354       _M_pi->weak_add_ref();
355   }
356
357   weak_count(const weak_count& __r)
358   : _M_pi(__r._M_pi) // nothrow
359   {
360     if (_M_pi != 0)
361       _M_pi->weak_add_ref();
362   }
363
364   ~weak_count() // nothrow
365   {
366     if (_M_pi != 0)
367       _M_pi->weak_release();
368   }
369
370   weak_count&
371   operator=(const shared_count& __r) // nothrow
372   {
373     _Sp_counted_base* __tmp = __r._M_pi;
374     if (__tmp != 0)
375       __tmp->weak_add_ref();
376     if (_M_pi != 0)
377       _M_pi->weak_release();
378     _M_pi = __tmp;
379
380     return *this;
381   }
382
383   weak_count&
384   operator=(const weak_count& __r) // nothrow
385   {
386     _Sp_counted_base * __tmp = __r._M_pi;
387     if (__tmp != 0)
388       __tmp->weak_add_ref();
389     if (_M_pi != 0)
390       _M_pi->weak_release();
391     _M_pi = __tmp;
392
393     return *this;
394   }
395
396   void
397   swap(weak_count& __r) // nothrow
398   {
399     _Sp_counted_base * __tmp = __r._M_pi;
400     __r._M_pi = _M_pi;
401     _M_pi = __tmp;
402   }
403
404   long
405   use_count() const // nothrow
406   { return _M_pi != 0 ? _M_pi->use_count() : 0; }
407
408   friend inline bool
409   operator==(const weak_count& __a, const weak_count& __b)
410   { return __a._M_pi == __b._M_pi; }
411
412   friend inline bool
413   operator<(const weak_count& __a, const weak_count& __b)
414   { return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
415 };
416
417 inline
418 shared_count::shared_count(const weak_count& __r)
419 : _M_pi(__r._M_pi)
420 {
421   if (_M_pi != 0)
422   {
423     _M_pi->add_ref_lock();
424   }
425   else
426   {
427     __throw_bad_weak_ptr();
428   }
429 }
430
431 // fwd decls
432 template <typename _Tp> class weak_ptr;
433 template <typename _Tp> class enable_shared_from_this;
434
435 struct __static_cast_tag {};
436 struct __const_cast_tag {};
437 struct __dynamic_cast_tag {};
438 struct __polymorphic_cast_tag {};
439
440 template<class _Tp> struct shared_ptr_traits
441 {
442     typedef _Tp & reference;
443 };
444
445 template<> struct shared_ptr_traits<void>
446 {
447     typedef void reference;
448 };
449
450 template<> struct shared_ptr_traits<void const>
451 {
452     typedef void reference;
453 };
454
455 template<> struct shared_ptr_traits<void volatile>
456 {
457     typedef void reference;
458 };
459
460 template<> struct shared_ptr_traits<void const volatile>
461 {
462     typedef void reference;
463 };
464
465
466 // enable_shared_from_this support
467
468 // friend of enable_shared_from_this
469 template <typename _Tp1, typename _Tp2>
470   void
471   __enable_shared_from_this( const shared_count& __pn,
472                              const enable_shared_from_this<_Tp1>* __pe,
473                              const _Tp2* __px );
474
475 inline void
476 __enable_shared_from_this(const shared_count&, ...)
477 { }
478
479 /**
480  *  @class shared_ptr <tr1/memory>
481  *
482  *  A smart pointer with reference-counted copy semantics.
483  *  The object pointed to is deleted when the last shared_ptr pointing to it
484  *  is destroyed or reset.
485  */
486
487 template <typename _Tp>
488   class shared_ptr
489   {
490     typedef typename shared_ptr_traits<_Tp>::reference _Reference;
491
492   public:
493
494     typedef _Tp   element_type;
495
496     /** @brief  Construct an empty %shared_ptr.
497      *  @post   use_count()==0 && get()==0
498      */
499     shared_ptr() : _M_ptr(0), _M_refcount() // never throws
500     { }
501
502     /** @brief  Construct a %shared_ptr that owns the pointer @a p.
503      *  @param  p  A pointer that is convertible to element_type*.
504      *  @post   use_count()==1 && get()==p
505      *  @throw  std::bad_alloc, in which case @c delete @a p is called.
506      */
507     template <typename _Tp1>
508       explicit shared_ptr(_Tp1* __p)
509       : _M_ptr(__p), _M_refcount(__p, checked_deleter<_Tp1>())
510       {
511         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
512         // __glibcxx_function_requires(_CompleteConcept<_Tp1*>)
513
514         __enable_shared_from_this( _M_refcount, __p, __p );
515       }
516
517     //
518     // Requirements: D's copy constructor and destructor must not throw
519     //
520     // shared_ptr will release p by calling d(p)
521     //
522     /** @brief  Construct a %shared_ptr that owns the pointer @a p
523      *          and the deleter @a d.
524      *  @param  p  A pointer.
525      *  @param  d  A deleter.
526      *  @post   use_count()==1 && get()==p
527      *  @throw  std::bad_alloc, in which case @a d(p) is called.
528      */
529     template <typename _Tp1, typename _Deleter>
530       shared_ptr(_Tp1* __p, _Deleter __d)
531       : _M_ptr(__p), _M_refcount(__p, __d)
532       {
533         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
534         // TODO requires D is CopyConstructible and d(p) well-formed
535
536         __enable_shared_from_this( _M_refcount, __p, __p );
537       }
538
539     //  generated copy constructor, assignment, destructor are fine.
540
541     /** @brief  If @a r is empty, constructs an empty %shared_ptr; otherwise
542      *          construct a %shared_ptr that shares ownership with @a r.
543      *  @param  r  A %shared_ptr.
544      *  @post   get()==r.get() && use_count()==r.use_count()
545      *  @throw  std::bad_alloc, in which case 
546      */
547     template <typename _Tp1>
548       shared_ptr(const shared_ptr<_Tp1>& __r)
549       : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
550       {
551         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
552       }
553
554     /** @brief  Constructs a %shared_ptr that shares ownership with @a r
555      *          and stores a copy of the pointer stored in @a r.
556      *  @param  r  A weak_ptr.
557      *  @post   use_count()==r.use_count()
558      *  @throw  bad_weak_ptr when r.expired(),
559      *          in which case the constructor has no effect.
560      */
561     template <typename _Tp1>
562       explicit shared_ptr(const weak_ptr<_Tp1>& __r)
563       : _M_refcount(__r._M_refcount) // may throw
564       {
565         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
566         // it is now safe to copy r__._M_ptr, as _M_refcount(__r._M_refcount)
567         // did not throw
568         _M_ptr = __r._M_ptr;
569       }
570
571     /**
572      * @post use_count()==1 and r.get()==0
573      */
574     template <typename _Tp1>
575       explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
576       : _M_ptr(__r.get()), _M_refcount()
577       {
578         // TODO requires r.release() convertible to _Tp*, Tp1 is complete,
579         // delete r.release() well-formed
580         _Tp1 * __tmp = __r.get();
581         _M_refcount = shared_count(__r);
582
583         __enable_shared_from_this( _M_refcount, __tmp, __tmp );
584       }
585
586     template <typename _Tp1>
587       shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
588       : _M_ptr(static_cast<element_type*>(__r._M_ptr))
589       , _M_refcount(__r._M_refcount)
590       { }
591
592     template <typename _Tp1>
593       shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
594       : _M_ptr(const_cast<element_type*>(__r._M_ptr))
595       , _M_refcount(__r._M_refcount)
596       { }
597
598     template <typename _Tp1>
599       shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
600       : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr))
601       , _M_refcount(__r._M_refcount)
602       {
603         if (_M_ptr == 0) // need to allocate new counter -- the cast failed
604         {
605           _M_refcount = shared_count();
606         }
607       }
608
609     template <typename _Tp1>
610       shared_ptr&
611       operator=(const shared_ptr<_Tp1>& __r) // never throws
612       {
613         _M_ptr = __r._M_ptr;
614         _M_refcount = __r._M_refcount; // shared_count::op= doesn't throw
615         return *this;
616       }
617
618     template <typename _Tp1>
619       shared_ptr&
620       operator=(std::auto_ptr<_Tp1>& __r)
621       {
622         shared_ptr(__r).swap(*this);
623         return *this;
624       }
625
626     void
627     reset() // never throws
628     { shared_ptr().swap(*this); }
629
630     template <typename _Tp1>
631       void
632       reset(_Tp1* __p) // _Tp1 must be complete
633       {
634         _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset errors
635         shared_ptr(__p).swap(*this);
636       }
637
638     template <typename _Tp1, typename _Deleter>
639       void
640       reset(_Tp1 * __p, _Deleter __d)
641       { shared_ptr(__p, __d).swap(*this); }
642
643     // error to instantiate if _Tp is [cv-qual] void
644     _Reference
645     operator*() const // never throws
646     {
647       _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
648       return *_M_ptr;
649     }
650
651     _Tp*
652     operator->() const // never throws
653     {
654       _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
655       return _M_ptr;
656     }
657     
658     _Tp*
659     get() const // never throws
660     { return _M_ptr; }
661
662     // implicit conversion to "bool"
663   private:
664     typedef _Tp* shared_ptr::*__unspecified_bool_type;
665
666   public:
667     operator __unspecified_bool_type() const // never throws
668     { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
669
670     bool
671     unique() const // never throws
672     { return _M_refcount.unique(); }
673
674     long
675     use_count() const // never throws
676     { return _M_refcount.use_count(); }
677
678     void
679     swap(shared_ptr<_Tp>& __other) // never throws
680     {
681       std::swap(_M_ptr, __other._M_ptr);
682       _M_refcount.swap(__other._M_refcount);
683     }
684
685   private:
686     template <typename _Tp1>
687       bool
688       _M_less(const shared_ptr<_Tp1>& __rhs) const
689       { return _M_refcount < __rhs._M_refcount; }
690
691     void*
692     _M_get_deleter(const std::type_info& __ti) const
693     { return _M_refcount.get_deleter(__ti); }
694
695     template <typename _Tp1> friend class shared_ptr;
696     template <typename _Tp1> friend class weak_ptr;
697
698     // friends injected into enclosing namespace and found by ADL:
699
700     // get_deleter (experimental)
701     template <typename _Del>
702       friend inline _Del*
703       get_deleter(const shared_ptr& __p)
704       { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
705
706     template <typename _Tp1>
707       friend inline bool
708       operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
709       { return __a.get() == __b.get(); }
710
711     template <typename _Tp1>
712       friend inline bool
713       operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
714       { return __a.get() != __b.get(); }
715
716     template <typename _Tp1>
717       friend inline bool
718       operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
719       { return __a._M_less(__b); }
720
721     _Tp*         _M_ptr;         // contained pointer
722     shared_count _M_refcount;    // reference counter
723   };  // shared_ptr
724
725 // 2.2.3.9 shared_ptr casts
726
727 /** @warning The seemingly equivalent
728  *           <code>shared_ptr<T>(static_cast<T*>(r.get()))</code>
729  *           will eventually result in undefined behaviour,
730  *           attempting to delete the same object twice.
731  */
732 template <typename _Tp, typename _Tp1>
733   shared_ptr<_Tp>
734   static_pointer_cast(const shared_ptr<_Tp1>& __r)
735   {
736     return shared_ptr<_Tp>(__r, __static_cast_tag());
737   }
738
739 /** @warning The seemingly equivalent
740  *           <code>shared_ptr<T>(const_cast<T*>(r.get()))</code>
741  *           will eventually result in undefined behaviour,
742  *           attempting to delete the same object twice.
743  */
744 template <typename _Tp, typename _Tp1>
745   shared_ptr<_Tp>
746   const_pointer_cast(const shared_ptr<_Tp1>& __r)
747   {
748     return shared_ptr<_Tp>(__r, __const_cast_tag());
749   }
750
751 /** @warning The seemingly equivalent
752  *           <code>shared_ptr<T>(dynamic_cast<T*>(r.get()))</code>
753  *           will eventually result in undefined behaviour,
754  *           attempting to delete the same object twice.
755  */
756 template <typename _Tp, typename _Tp1>
757   shared_ptr<_Tp>
758   dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
759   {
760     return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
761   }
762
763 // operator<<
764 template <typename _Ch, typename _Tr, typename _Tp>
765   std::basic_ostream<_Ch,_Tr>&
766   operator<<(std::basic_ostream<_Ch,_Tr>& __os, const shared_ptr<_Tp>& __p)
767   {
768     __os << __p.get();
769     return __os;
770   }
771
772
773 template <typename _Tp>
774   class weak_ptr
775   {
776   public:
777
778     typedef _Tp element_type;
779
780     weak_ptr()
781     : _M_ptr(0), _M_refcount() // never throws
782     { }
783
784   //  generated copy constructor, assignment, destructor are fine
785
786   //
787   //  The "obvious" converting constructor implementation:
788   //
789   //  template<class Y>
790   //  weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
791   //  {
792   //  }
793   //
794   //  has a serious problem.
795   //
796   //  r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
797   //  conversion may require access to *r._M_ptr (virtual inheritance).
798   //
799   //  It is not possible to avoid spurious access violations since
800   //  in multithreaded programs r._M_ptr may be invalidated at any point.
801   //
802
803     template <typename _Tp1>
804       weak_ptr(const weak_ptr<_Tp1>& r)
805       : _M_refcount(r._M_refcount) // never throws
806       {
807         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
808         _M_ptr = r.lock().get();
809       }
810
811     template <typename _Tp1>
812       weak_ptr(const shared_ptr<_Tp1>& r)
813       : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
814       {
815         __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
816       }
817
818     template <typename _Tp1>
819       weak_ptr&
820       operator=(const weak_ptr<_Tp1>& r) // never throws
821       {
822         _M_ptr = r.lock().get();
823         _M_refcount = r._M_refcount;
824         return *this;
825       }
826
827     template <typename _Tp1>
828       weak_ptr&
829       operator=(const shared_ptr<_Tp1>& r) // never throws
830       {
831         _M_ptr = r._M_ptr;
832         _M_refcount = r._M_refcount;
833         return *this;
834       }
835
836     shared_ptr<_Tp>
837     lock() const // never throws
838     {
839 #ifdef __GTHREADS
840
841       // optimization: avoid throw overhead
842       if (expired())
843       {
844         return shared_ptr<element_type>();
845       }
846
847       try
848       {
849         return shared_ptr<element_type>(*this);
850       }
851       catch (const bad_weak_ptr&)
852       {
853         // Q: how can we get here?
854         // A: another thread may have invalidated r after the use_count test above.
855         return shared_ptr<element_type>();
856       }
857
858 #else
859
860       // optimization: avoid try/catch overhead when single threaded
861       return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this);
862
863 #endif
864     } // XXX MT
865
866
867     long
868     use_count() const // never throws
869     { return _M_refcount.use_count(); }
870
871     bool
872     expired() const // never throws
873     { return _M_refcount.use_count() == 0; }
874
875     void
876     reset() // never throws
877     { weak_ptr().swap(*this); }
878
879     void
880     swap(weak_ptr& __s) // never throws
881     {
882       std::swap(_M_ptr, __s._M_ptr);
883       _M_refcount.swap(__s._M_refcount);
884     }
885
886   private:
887
888     template <typename _Tp1>
889       bool
890       _M_less(const weak_ptr<_Tp1>& __rhs) const
891       { return _M_refcount < __rhs._M_refcount; }
892
893     // used by __enable_shared_from_this
894     void
895     _M_assign(_Tp* __ptr, const shared_count& __refcount)
896     {
897       _M_ptr = __ptr;
898       _M_refcount = __refcount;
899     }
900
901     // friend injected into namespace and found by ADL
902
903     template <typename _Tp1>
904       friend inline bool
905       operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
906       { return __lhs._M_less(__rhs); }
907
908     template <typename _Tp1> friend class weak_ptr;
909     template <typename _Tp1> friend class shared_ptr;
910     friend class enable_shared_from_this<_Tp>;
911
912     _Tp*       _M_ptr;           // contained pointer
913     weak_count _M_refcount;      // reference counter
914
915   };  // weak_ptr
916
917
918
919 template <typename _Tp>
920   class enable_shared_from_this
921   {
922   protected:
923
924     enable_shared_from_this()
925     { }
926
927     enable_shared_from_this(const enable_shared_from_this&)
928     { }
929
930     enable_shared_from_this&
931     operator=(const enable_shared_from_this&)
932     { return *this; }
933
934     ~enable_shared_from_this()
935     { }
936
937   public:
938
939     shared_ptr<_Tp>
940     shared_from_this()
941     {
942       shared_ptr<_Tp> p(this->_M_weak_this);
943       return p;
944     }
945
946     shared_ptr<const _Tp>
947     shared_from_this() const
948     {
949       shared_ptr<const _Tp> p(this->_M_weak_this);
950       return p;
951     }
952
953   private:
954     template <typename _Tp1>
955       void
956       _M_weak_assign(_Tp1* __p, const shared_count& __n) const
957       { _M_weak_this._M_assign(__p, __n); }
958
959     template <typename _Tp1>
960       friend void
961       __enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px)
962       {
963         if(__pe != 0)
964           __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
965       }
966
967     mutable weak_ptr<_Tp> _M_weak_this;
968   };
969
970 } // namespace tr1
971
972 /**
973  *  @brief   std::swap() specialisation for shared_ptr.
974  *  @relates shared_ptr.
975  */
976 template <typename _Tp>
977   inline void
978   swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
979   {
980     __a.swap(__b);
981   }
982
983 /**
984  *  @brief   std::swap() specialisation for weak_ptr.
985  *  @relates weak_ptr.
986  */
987 template <typename _Tp>
988   void
989   swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)
990   {
991     __a.swap(__b);
992   }
993
994 } // namespace std
995
996 #endif