OSDN Git Service

2009-11-20 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / shared_ptr.h
1 // shared_ptr and weak_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 // GCC Note: Based on files from version 1.32.0 of the Boost library.
26
27 //  shared_count.hpp
28 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
29
30 //  shared_ptr.hpp
31 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
32 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
33
34 //  weak_ptr.hpp
35 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 //  enable_shared_from_this.hpp
38 //  Copyright (C) 2002 Peter Dimov
39
40 // Distributed under the Boost Software License, Version 1.0. (See
41 // accompanying file LICENSE_1_0.txt or copy at
42 // http://www.boost.org/LICENSE_1_0.txt)
43
44 /** @file bits/shared_ptr.h
45  *  This is an internal header file, included by other library headers.
46  *  You should not attempt to use it directly.
47  */
48
49 #ifndef _SHARED_PTR_H
50 #define _SHARED_PTR_H 1
51
52 #ifndef __GXX_EXPERIMENTAL_CXX0X__
53 # include <c++0x_warning.h>
54 #endif
55
56 #include <bits/shared_ptr_base.h>
57
58 _GLIBCXX_BEGIN_NAMESPACE(std)
59
60   /**
61    * @addtogroup pointer_abstractions
62    * @{
63    */
64
65   /// 2.2.3.7 shared_ptr I/O
66   template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
67     std::basic_ostream<_Ch, _Tr>&
68     operator<<(std::basic_ostream<_Ch, _Tr>& __os,
69                const __shared_ptr<_Tp, _Lp>& __p)
70     {
71       __os << __p.get();
72       return __os;
73     }
74
75   /// 2.2.3.10 shared_ptr get_deleter (experimental)
76   template<typename _Del, typename _Tp, _Lock_policy _Lp>
77     inline _Del*
78     get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
79     {
80 #ifdef __GXX_RTTI
81       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
82 #else
83       return 0;
84 #endif
85     }
86
87
88   /**
89    *  @brief  A smart pointer with reference-counted copy semantics.
90    *
91    *  The object pointed to is deleted when the last shared_ptr pointing to
92    *  it is destroyed or reset.
93   */
94   template<typename _Tp>
95     class shared_ptr : public __shared_ptr<_Tp>
96     {
97     public:
98       /**
99        *  @brief  Construct an empty %shared_ptr.
100        *  @post   use_count()==0 && get()==0
101        */
102       shared_ptr() : __shared_ptr<_Tp>() { }
103
104       /**
105        *  @brief  Construct a %shared_ptr that owns the pointer @a __p.
106        *  @param  __p  A pointer that is convertible to element_type*.
107        *  @post   use_count() == 1 && get() == __p
108        *  @throw  std::bad_alloc, in which case @c delete @a __p is called.
109        */
110       template<typename _Tp1>
111         explicit shared_ptr(_Tp1* __p) : __shared_ptr<_Tp>(__p) { }
112
113       /**
114        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
115        *          and the deleter @a __d.
116        *  @param  __p  A pointer.
117        *  @param  __d  A deleter.
118        *  @post   use_count() == 1 && get() == __p
119        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
120        *
121        *  Requirements: _Deleter's copy constructor and destructor must
122        *  not throw
123        *
124        *  __shared_ptr will release __p by calling __d(__p)
125        */
126         template<typename _Tp1, typename _Deleter>
127         shared_ptr(_Tp1* __p, _Deleter __d) : __shared_ptr<_Tp>(__p, __d) { }
128
129       /**
130        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
131        *          and the deleter @a __d.
132        *  @param  __p  A pointer.
133        *  @param  __d  A deleter.
134        *  @param  __a  An allocator.
135        *  @post   use_count() == 1 && get() == __p
136        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
137        *
138        *  Requirements: _Deleter's copy constructor and destructor must
139        *  not throw _Alloc's copy constructor and destructor must not
140        *  throw.
141        *
142        *  __shared_ptr will release __p by calling __d(__p)
143        */
144       template<typename _Tp1, typename _Deleter, typename _Alloc>
145         shared_ptr(_Tp1* __p, _Deleter __d, const _Alloc& __a)
146         : __shared_ptr<_Tp>(__p, __d, __a) { }
147
148       // Aliasing constructor
149
150       /**
151        *  @brief  Constructs a %shared_ptr instance that stores @a __p
152        *          and shares ownership with @a __r.
153        *  @param  __r  A %shared_ptr.
154        *  @param  __p  A pointer that will remain valid while @a *__r is valid.
155        *  @post   get() == __p && use_count() == __r.use_count()
156        *
157        *  This can be used to construct a @c shared_ptr to a sub-object
158        *  of an object managed by an existing @c shared_ptr.
159        *
160        * @code
161        * shared_ptr< pair<int,int> > pii(new pair<int,int>());
162        * shared_ptr<int> pi(pii, &pii->first);
163        * assert(pii.use_count() == 2);
164        * @endcode
165        */
166       template<typename _Tp1>
167         shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
168         : __shared_ptr<_Tp>(__r, __p) { }
169
170       /**
171        *  @brief  If @a __r is empty, constructs an empty %shared_ptr;
172        *          otherwise construct a %shared_ptr that shares ownership
173        *          with @a __r.
174        *  @param  __r  A %shared_ptr.
175        *  @post   get() == __r.get() && use_count() == __r.use_count()
176        */
177       template<typename _Tp1>
178         shared_ptr(const shared_ptr<_Tp1>& __r) : __shared_ptr<_Tp>(__r) { }
179
180       /**
181        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
182        *  @param  __r  A %shared_ptr rvalue.
183        *  @post   *this contains the old value of @a __r, @a __r is empty.
184        */
185       shared_ptr(shared_ptr&& __r)
186       : __shared_ptr<_Tp>(std::move(__r)) { }
187
188       /**
189        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
190        *  @param  __r  A %shared_ptr rvalue.
191        *  @post   *this contains the old value of @a __r, @a __r is empty.
192        */
193       template<typename _Tp1>
194         shared_ptr(shared_ptr<_Tp1>&& __r)
195         : __shared_ptr<_Tp>(std::move(__r)) { }
196
197       /**
198        *  @brief  Constructs a %shared_ptr that shares ownership with @a __r
199        *          and stores a copy of the pointer stored in @a __r.
200        *  @param  __r  A weak_ptr.
201        *  @post   use_count() == __r.use_count()
202        *  @throw  bad_weak_ptr when __r.expired(),
203        *          in which case the constructor has no effect.
204        */
205       template<typename _Tp1>
206         explicit shared_ptr(const weak_ptr<_Tp1>& __r)
207         : __shared_ptr<_Tp>(__r) { }
208
209 #if _GLIBCXX_DEPRECATED
210       template<typename _Tp1>
211         explicit
212         shared_ptr(std::auto_ptr<_Tp1>&& __r)
213         : __shared_ptr<_Tp>(std::move(__r)) { }
214 #endif
215
216       template<typename _Tp1, typename _Del>
217         explicit shared_ptr(const std::unique_ptr<_Tp1, _Del>&) = delete;
218
219       template<typename _Tp1, typename _Del>
220         explicit shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
221         : __shared_ptr<_Tp>(std::move(__r)) { }
222
223       template<typename _Tp1>
224         shared_ptr&
225         operator=(const shared_ptr<_Tp1>& __r) // never throws
226         {
227           this->__shared_ptr<_Tp>::operator=(__r);
228           return *this;
229         }
230
231 #if _GLIBCXX_DEPRECATED
232       template<typename _Tp1>
233         shared_ptr&
234         operator=(std::auto_ptr<_Tp1>&& __r)
235         {
236           this->__shared_ptr<_Tp>::operator=(std::move(__r));
237           return *this;
238         }
239 #endif
240
241       shared_ptr&
242       operator=(shared_ptr&& __r)
243       {
244         this->__shared_ptr<_Tp>::operator=(std::move(__r));
245         return *this;
246       }
247
248       template<class _Tp1>
249         shared_ptr&
250         operator=(shared_ptr<_Tp1>&& __r)
251         {
252           this->__shared_ptr<_Tp>::operator=(std::move(__r));
253           return *this;
254         }
255
256       template<typename _Tp1, typename _Del>
257         shared_ptr&
258         operator=(const std::unique_ptr<_Tp1, _Del>& __r) = delete;
259
260       template<typename _Tp1, typename _Del>
261         shared_ptr&
262         operator=(std::unique_ptr<_Tp1, _Del>&& __r)
263         {
264           this->__shared_ptr<_Tp>::operator=(std::move(__r));
265           return *this;
266         }
267
268     private:
269       // This constructor is non-standard, it is used by allocate_shared.
270       template<typename _Alloc, typename... _Args>
271         shared_ptr(_Sp_make_shared_tag __tag, _Alloc __a, _Args&&... __args)
272         : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
273         { }
274
275       template<typename _Tp1, typename _Alloc, typename... _Args>
276         friend shared_ptr<_Tp1>
277         allocate_shared(_Alloc __a, _Args&&... __args);
278     };
279
280   // 20.8.13.2.7 shared_ptr comparisons
281   template<typename _Tp1, typename _Tp2>
282     inline bool
283     operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
284     { return __a.get() == __b.get(); }
285
286   template<typename _Tp1, typename _Tp2>
287     inline bool
288     operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
289     { return __a.get() != __b.get(); }
290
291   template<typename _Tp1, typename _Tp2>
292     inline bool
293     operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
294     { return __a.get() < __b.get(); }
295
296   template<typename _Tp>
297     struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
298     { };
299
300   // 20.8.13.2.9 shared_ptr specialized algorithms.
301   template<typename _Tp>
302     inline void
303     swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
304     { __a.swap(__b); }
305
306   // 20.8.13.2.10 shared_ptr casts.
307   template<typename _Tp, typename _Tp1>
308     inline shared_ptr<_Tp>
309     static_pointer_cast(const shared_ptr<_Tp1>& __r)
310     { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
311
312   template<typename _Tp, typename _Tp1>
313     inline shared_ptr<_Tp>
314     const_pointer_cast(const shared_ptr<_Tp1>& __r)
315     { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
316
317   template<typename _Tp, typename _Tp1>
318     inline shared_ptr<_Tp>
319     dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
320     {
321       if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
322         return shared_ptr<_Tp>(__r, __p);
323       return shared_ptr<_Tp>();
324     }
325
326
327   /**
328    *  @brief  A smart pointer with weak semantics.
329    *
330    *  With forwarding constructors and assignment operators.
331    */
332   template<typename _Tp>
333     class weak_ptr : public __weak_ptr<_Tp>
334     {
335     public:
336       weak_ptr() : __weak_ptr<_Tp>() { }
337
338       template<typename _Tp1>
339         weak_ptr(const weak_ptr<_Tp1>& __r)
340         : __weak_ptr<_Tp>(__r) { }
341
342       template<typename _Tp1>
343         weak_ptr(const shared_ptr<_Tp1>& __r)
344         : __weak_ptr<_Tp>(__r) { }
345
346       template<typename _Tp1>
347         weak_ptr&
348         operator=(const weak_ptr<_Tp1>& __r) // never throws
349         {
350           this->__weak_ptr<_Tp>::operator=(__r);
351           return *this;
352         }
353
354       template<typename _Tp1>
355         weak_ptr&
356         operator=(const shared_ptr<_Tp1>& __r) // never throws
357         {
358           this->__weak_ptr<_Tp>::operator=(__r);
359           return *this;
360         }
361
362       shared_ptr<_Tp>
363       lock() const // never throws
364       {
365 #ifdef __GTHREADS
366         if (this->expired())
367           return shared_ptr<_Tp>();
368
369         __try
370           {
371             return shared_ptr<_Tp>(*this);
372           }
373         __catch(const bad_weak_ptr&)
374           {
375             return shared_ptr<_Tp>();
376           }
377 #else
378         return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
379 #endif
380       }
381
382       // Comparisons
383       template<typename _Tp1>
384         bool operator<(const weak_ptr<_Tp1>&) const = delete;
385       template<typename _Tp1>
386         bool operator<=(const weak_ptr<_Tp1>&) const = delete;
387       template<typename _Tp1>
388         bool operator>(const weak_ptr<_Tp1>&) const = delete;
389       template<typename _Tp1>
390         bool operator>=(const weak_ptr<_Tp1>&) const = delete;
391     };
392
393   // 20.8.13.3.7 weak_ptr specialized algorithms.
394   template<typename _Tp>
395     inline void
396     swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
397     { __a.swap(__b); }
398
399
400   /// Primary template owner_less
401   template<typename _Tp>
402     struct owner_less;
403
404   /// Partial specialization of owner_less for shared_ptr.
405   template<typename _Tp>
406     struct owner_less<shared_ptr<_Tp>>
407     : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
408     { };
409
410   /// Partial specialization of owner_less for weak_ptr.
411   template<typename _Tp>
412     struct owner_less<weak_ptr<_Tp>>
413     : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
414     { };
415
416   /**
417    *  @brief Base class allowing use of member function shared_from_this.
418    */
419   template<typename _Tp>
420     class enable_shared_from_this
421     {
422     protected:
423       enable_shared_from_this() { }
424
425       enable_shared_from_this(const enable_shared_from_this&) { }
426
427       enable_shared_from_this&
428       operator=(const enable_shared_from_this&)
429       { return *this; }
430
431       ~enable_shared_from_this() { }
432
433     public:
434       shared_ptr<_Tp>
435       shared_from_this()
436       { return shared_ptr<_Tp>(this->_M_weak_this); }
437
438       shared_ptr<const _Tp>
439       shared_from_this() const
440       { return shared_ptr<const _Tp>(this->_M_weak_this); }
441
442     private:
443       template<typename _Tp1>
444         void
445         _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
446         { _M_weak_this._M_assign(__p, __n); }
447
448       template<typename _Tp1>
449         friend void
450         __enable_shared_from_this_helper(const __shared_count<>& __pn,
451                                          const enable_shared_from_this* __pe,
452                                          const _Tp1* __px)
453         {
454           if (__pe != 0)
455             __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
456         }
457
458       mutable weak_ptr<_Tp>  _M_weak_this;
459     };
460
461   /**
462    *  @brief  Create an object that is owned by a shared_ptr.
463    *  @param  __a     An allocator.
464    *  @param  __args  Arguments for the @a _Tp object's constructor.
465    *  @return A shared_ptr that owns the newly created object.
466    *  @throw  An exception thrown from @a _Alloc::allocate or from the
467    *          constructor of @a _Tp.
468    *
469    *  A copy of @a __a will be used to allocate memory for the shared_ptr
470    *  and the new object.
471    */
472   template<typename _Tp, typename _Alloc, typename... _Args>
473     inline shared_ptr<_Tp>
474     allocate_shared(_Alloc __a, _Args&&... __args)
475     {
476       return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a),
477                              std::forward<_Args>(__args)...);
478     }
479
480   /**
481    *  @brief  Create an object that is owned by a shared_ptr.
482    *  @param  __args  Arguments for the @a _Tp object's constructor.
483    *  @return A shared_ptr that owns the newly created object.
484    *  @throw  std::bad_alloc, or an exception thrown from the
485    *          constructor of @a _Tp.
486    */
487   template<typename _Tp, typename... _Args>
488     inline shared_ptr<_Tp>
489     make_shared(_Args&&... __args)
490     {
491       typedef typename std::remove_const<_Tp>::type _Tp_nc;
492       return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
493                                   std::forward<_Args>(__args)...);
494     }
495
496   // @} group pointer_abstractions
497
498 _GLIBCXX_END_NAMESPACE
499
500 #endif // _SHARED_PTR_H