*: Use headername alias to associate private includes to public includes.
[platform/upstream/gcc.git] / libstdc++-v3 / include / bits / shared_ptr.h
1 // shared_ptr and weak_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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  *  Do not attempt to use it directly. @headername{memory}
47  */
48
49 #ifndef _SHARED_PTR_H
50 #define _SHARED_PTR_H 1
51
52 #include <bits/shared_ptr_base.h>
53
54 _GLIBCXX_BEGIN_NAMESPACE(std)
55
56   /**
57    * @addtogroup pointer_abstractions
58    * @{
59    */
60
61   /// 2.2.3.7 shared_ptr I/O
62   template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp>
63     inline std::basic_ostream<_Ch, _Tr>&
64     operator<<(std::basic_ostream<_Ch, _Tr>& __os,
65                const __shared_ptr<_Tp, _Lp>& __p)
66     {
67       __os << __p.get();
68       return __os;
69     }
70
71   /// 2.2.3.10 shared_ptr get_deleter (experimental)
72   template<typename _Del, typename _Tp, _Lock_policy _Lp>
73     inline _Del*
74     get_deleter(const __shared_ptr<_Tp, _Lp>& __p)
75     {
76 #ifdef __GXX_RTTI
77       return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del)));
78 #else
79       return 0;
80 #endif
81     }
82
83
84   /**
85    *  @brief  A smart pointer with reference-counted copy semantics.
86    *
87    *  The object pointed to is deleted when the last shared_ptr pointing to
88    *  it is destroyed or reset.
89   */
90   template<typename _Tp>
91     class shared_ptr : public __shared_ptr<_Tp>
92     {
93     public:
94       /**
95        *  @brief  Construct an empty %shared_ptr.
96        *  @post   use_count()==0 && get()==0
97        */
98       constexpr shared_ptr()
99       : __shared_ptr<_Tp>() { }
100
101       /**
102        *  @brief  Construct a %shared_ptr that owns the pointer @a __p.
103        *  @param  __p  A pointer that is convertible to element_type*.
104        *  @post   use_count() == 1 && get() == __p
105        *  @throw  std::bad_alloc, in which case @c delete @a __p is called.
106        */
107       template<typename _Tp1>
108         explicit shared_ptr(_Tp1* __p)
109         : __shared_ptr<_Tp>(__p) { }
110
111       /**
112        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
113        *          and the deleter @a __d.
114        *  @param  __p  A pointer.
115        *  @param  __d  A deleter.
116        *  @post   use_count() == 1 && get() == __p
117        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
118        *
119        *  Requirements: _Deleter's copy constructor and destructor must
120        *  not throw
121        *
122        *  __shared_ptr will release __p by calling __d(__p)
123        */
124       template<typename _Tp1, typename _Deleter>
125         shared_ptr(_Tp1* __p, _Deleter __d)
126         : __shared_ptr<_Tp>(__p, __d) { }
127
128       /**
129        *  @brief  Construct a %shared_ptr that owns a null pointer
130        *          and the deleter @a __d.
131        *  @param  __p  A null pointer constant.
132        *  @param  __d  A deleter.
133        *  @post   use_count() == 1 && get() == __p
134        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
135        *
136        *  Requirements: _Deleter's copy constructor and destructor must
137        *  not throw
138        *
139        *  The last owner will call __d(__p)
140        */
141       template<typename _Deleter>
142         shared_ptr(nullptr_t __p, _Deleter __d)
143         : __shared_ptr<_Tp>(__p, __d) { }
144
145       /**
146        *  @brief  Construct a %shared_ptr that owns the pointer @a __p
147        *          and the deleter @a __d.
148        *  @param  __p  A pointer.
149        *  @param  __d  A deleter.
150        *  @param  __a  An allocator.
151        *  @post   use_count() == 1 && get() == __p
152        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
153        *
154        *  Requirements: _Deleter's copy constructor and destructor must
155        *  not throw _Alloc's copy constructor and destructor must not
156        *  throw.
157        *
158        *  __shared_ptr will release __p by calling __d(__p)
159        */
160       template<typename _Tp1, typename _Deleter, typename _Alloc>
161         shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
162         : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
163
164       /**
165        *  @brief  Construct a %shared_ptr that owns a null pointer
166        *          and the deleter @a __d.
167        *  @param  __p  A null pointer constant.
168        *  @param  __d  A deleter.
169        *  @param  __a  An allocator.
170        *  @post   use_count() == 1 && get() == __p
171        *  @throw  std::bad_alloc, in which case @a __d(__p) is called.
172        *
173        *  Requirements: _Deleter's copy constructor and destructor must
174        *  not throw _Alloc's copy constructor and destructor must not
175        *  throw.
176        *
177        *  The last owner will call __d(__p)
178        */
179       template<typename _Deleter, typename _Alloc>
180         shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
181         : __shared_ptr<_Tp>(__p, __d, std::move(__a)) { }
182
183       // Aliasing constructor
184
185       /**
186        *  @brief  Constructs a %shared_ptr instance that stores @a __p
187        *          and shares ownership with @a __r.
188        *  @param  __r  A %shared_ptr.
189        *  @param  __p  A pointer that will remain valid while @a *__r is valid.
190        *  @post   get() == __p && use_count() == __r.use_count()
191        *
192        *  This can be used to construct a @c shared_ptr to a sub-object
193        *  of an object managed by an existing @c shared_ptr.
194        *
195        * @code
196        * shared_ptr< pair<int,int> > pii(new pair<int,int>());
197        * shared_ptr<int> pi(pii, &pii->first);
198        * assert(pii.use_count() == 2);
199        * @endcode
200        */
201       template<typename _Tp1>
202         shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p)
203         : __shared_ptr<_Tp>(__r, __p) { }
204
205       /**
206        *  @brief  If @a __r is empty, constructs an empty %shared_ptr;
207        *          otherwise construct a %shared_ptr that shares ownership
208        *          with @a __r.
209        *  @param  __r  A %shared_ptr.
210        *  @post   get() == __r.get() && use_count() == __r.use_count()
211        */
212       template<typename _Tp1, typename = typename
213                std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
214         shared_ptr(const shared_ptr<_Tp1>& __r)
215         : __shared_ptr<_Tp>(__r) { }
216
217       /**
218        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
219        *  @param  __r  A %shared_ptr rvalue.
220        *  @post   *this contains the old value of @a __r, @a __r is empty.
221        */
222       shared_ptr(shared_ptr&& __r)
223       : __shared_ptr<_Tp>(std::move(__r)) { }
224
225       /**
226        *  @brief  Move-constructs a %shared_ptr instance from @a __r.
227        *  @param  __r  A %shared_ptr rvalue.
228        *  @post   *this contains the old value of @a __r, @a __r is empty.
229        */
230       template<typename _Tp1, typename = typename
231                std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
232         shared_ptr(shared_ptr<_Tp1>&& __r)
233         : __shared_ptr<_Tp>(std::move(__r)) { }
234
235       /**
236        *  @brief  Constructs a %shared_ptr that shares ownership with @a __r
237        *          and stores a copy of the pointer stored in @a __r.
238        *  @param  __r  A weak_ptr.
239        *  @post   use_count() == __r.use_count()
240        *  @throw  bad_weak_ptr when __r.expired(),
241        *          in which case the constructor has no effect.
242        */
243       template<typename _Tp1>
244         explicit shared_ptr(const weak_ptr<_Tp1>& __r)
245         : __shared_ptr<_Tp>(__r) { }
246
247 #if _GLIBCXX_DEPRECATED
248       template<typename _Tp1>
249         shared_ptr(std::auto_ptr<_Tp1>&& __r)
250         : __shared_ptr<_Tp>(std::move(__r)) { }
251 #endif
252
253       template<typename _Tp1, typename _Del>
254         shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
255         : __shared_ptr<_Tp>(std::move(__r)) { }
256
257       /**
258        *  @brief  Construct an empty %shared_ptr.
259        *  @param  __p  A null pointer constant.
260        *  @post   use_count() == 0 && get() == nullptr
261        */
262       constexpr shared_ptr(nullptr_t __p)
263       : __shared_ptr<_Tp>(__p) { }
264
265       template<typename _Tp1>
266         shared_ptr&
267         operator=(const shared_ptr<_Tp1>& __r) // never throws
268         {
269           this->__shared_ptr<_Tp>::operator=(__r);
270           return *this;
271         }
272
273 #if _GLIBCXX_DEPRECATED
274       template<typename _Tp1>
275         shared_ptr&
276         operator=(std::auto_ptr<_Tp1>&& __r)
277         {
278           this->__shared_ptr<_Tp>::operator=(std::move(__r));
279           return *this;
280         }
281 #endif
282
283       shared_ptr&
284       operator=(shared_ptr&& __r)
285       {
286         this->__shared_ptr<_Tp>::operator=(std::move(__r));
287         return *this;
288       }
289
290       template<class _Tp1>
291         shared_ptr&
292         operator=(shared_ptr<_Tp1>&& __r)
293         {
294           this->__shared_ptr<_Tp>::operator=(std::move(__r));
295           return *this;
296         }
297
298       template<typename _Tp1, typename _Del>
299         shared_ptr&
300         operator=(std::unique_ptr<_Tp1, _Del>&& __r)
301         {
302           this->__shared_ptr<_Tp>::operator=(std::move(__r));
303           return *this;
304         }
305
306     private:
307       // This constructor is non-standard, it is used by allocate_shared.
308       template<typename _Alloc, typename... _Args>
309         shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
310                    _Args&&... __args)
311         : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...)
312         { }
313
314       template<typename _Tp1, typename _Alloc, typename... _Args>
315         friend shared_ptr<_Tp1>
316         allocate_shared(const _Alloc& __a, _Args&&... __args);
317     };
318
319   // 20.8.13.2.7 shared_ptr comparisons
320   template<typename _Tp1, typename _Tp2>
321     inline bool
322     operator==(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
323     { return __a.get() == __b.get(); }
324
325   template<typename _Tp>
326     inline bool
327     operator==(const shared_ptr<_Tp>& __a, nullptr_t)
328     { return __a.get() == nullptr; }
329
330   template<typename _Tp>
331     inline bool
332     operator==(nullptr_t, const shared_ptr<_Tp>& __b)
333     { return nullptr == __b.get(); }
334
335   template<typename _Tp1, typename _Tp2>
336     inline bool
337     operator!=(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
338     { return __a.get() != __b.get(); }
339
340   template<typename _Tp>
341     inline bool
342     operator!=(const shared_ptr<_Tp>& __a, nullptr_t)
343     { return __a.get() != nullptr; }
344
345   template<typename _Tp>
346     inline bool
347     operator!=(nullptr_t, const shared_ptr<_Tp>& __b)
348     { return nullptr != __b.get(); }
349
350   template<typename _Tp1, typename _Tp2>
351     inline bool
352     operator<(const shared_ptr<_Tp1>& __a, const shared_ptr<_Tp2>& __b)
353     { return __a.get() < __b.get(); }
354
355   template<typename _Tp>
356     struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>>
357     { };
358
359   // 20.8.13.2.9 shared_ptr specialized algorithms.
360   template<typename _Tp>
361     inline void
362     swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b)
363     { __a.swap(__b); }
364
365   // 20.8.13.2.10 shared_ptr casts.
366   template<typename _Tp, typename _Tp1>
367     inline shared_ptr<_Tp>
368     static_pointer_cast(const shared_ptr<_Tp1>& __r)
369     { return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get())); }
370
371   template<typename _Tp, typename _Tp1>
372     inline shared_ptr<_Tp>
373     const_pointer_cast(const shared_ptr<_Tp1>& __r)
374     { return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get())); }
375
376   template<typename _Tp, typename _Tp1>
377     inline shared_ptr<_Tp>
378     dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
379     {
380       if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
381         return shared_ptr<_Tp>(__r, __p);
382       return shared_ptr<_Tp>();
383     }
384
385
386   /**
387    *  @brief  A smart pointer with weak semantics.
388    *
389    *  With forwarding constructors and assignment operators.
390    */
391   template<typename _Tp>
392     class weak_ptr : public __weak_ptr<_Tp>
393     {
394     public:
395       constexpr weak_ptr()
396       : __weak_ptr<_Tp>() { }
397
398       template<typename _Tp1, typename = typename
399                std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
400         weak_ptr(const weak_ptr<_Tp1>& __r)
401         : __weak_ptr<_Tp>(__r) { }
402
403       template<typename _Tp1, typename = typename
404                std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
405         weak_ptr(const shared_ptr<_Tp1>& __r)
406         : __weak_ptr<_Tp>(__r) { }
407
408       template<typename _Tp1>
409         weak_ptr&
410         operator=(const weak_ptr<_Tp1>& __r) // never throws
411         {
412           this->__weak_ptr<_Tp>::operator=(__r);
413           return *this;
414         }
415
416       template<typename _Tp1>
417         weak_ptr&
418         operator=(const shared_ptr<_Tp1>& __r) // never throws
419         {
420           this->__weak_ptr<_Tp>::operator=(__r);
421           return *this;
422         }
423
424       shared_ptr<_Tp>
425       lock() const // never throws
426       {
427 #ifdef __GTHREADS
428         if (this->expired())
429           return shared_ptr<_Tp>();
430
431         __try
432           {
433             return shared_ptr<_Tp>(*this);
434           }
435         __catch(const bad_weak_ptr&)
436           {
437             return shared_ptr<_Tp>();
438           }
439 #else
440         return this->expired() ? shared_ptr<_Tp>() : shared_ptr<_Tp>(*this);
441 #endif
442       }
443     };
444
445   // 20.8.13.3.7 weak_ptr specialized algorithms.
446   template<typename _Tp>
447     inline void
448     swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b)
449     { __a.swap(__b); }
450
451
452   /// Primary template owner_less
453   template<typename _Tp>
454     struct owner_less;
455
456   /// Partial specialization of owner_less for shared_ptr.
457   template<typename _Tp>
458     struct owner_less<shared_ptr<_Tp>>
459     : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>>
460     { };
461
462   /// Partial specialization of owner_less for weak_ptr.
463   template<typename _Tp>
464     struct owner_less<weak_ptr<_Tp>>
465     : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>>
466     { };
467
468   /**
469    *  @brief Base class allowing use of member function shared_from_this.
470    */
471   template<typename _Tp>
472     class enable_shared_from_this
473     {
474     protected:
475       constexpr enable_shared_from_this() { }
476
477       enable_shared_from_this(const enable_shared_from_this&) { }
478
479       enable_shared_from_this&
480       operator=(const enable_shared_from_this&)
481       { return *this; }
482
483       ~enable_shared_from_this() { }
484
485     public:
486       shared_ptr<_Tp>
487       shared_from_this()
488       { return shared_ptr<_Tp>(this->_M_weak_this); }
489
490       shared_ptr<const _Tp>
491       shared_from_this() const
492       { return shared_ptr<const _Tp>(this->_M_weak_this); }
493
494     private:
495       template<typename _Tp1>
496         void
497         _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const
498         { _M_weak_this._M_assign(__p, __n); }
499
500       template<typename _Tp1>
501         friend void
502         __enable_shared_from_this_helper(const __shared_count<>& __pn,
503                                          const enable_shared_from_this* __pe,
504                                          const _Tp1* __px)
505         {
506           if (__pe != 0)
507             __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
508         }
509
510       mutable weak_ptr<_Tp>  _M_weak_this;
511     };
512
513   /**
514    *  @brief  Create an object that is owned by a shared_ptr.
515    *  @param  __a     An allocator.
516    *  @param  __args  Arguments for the @a _Tp object's constructor.
517    *  @return A shared_ptr that owns the newly created object.
518    *  @throw  An exception thrown from @a _Alloc::allocate or from the
519    *          constructor of @a _Tp.
520    *
521    *  A copy of @a __a will be used to allocate memory for the shared_ptr
522    *  and the new object.
523    */
524   template<typename _Tp, typename _Alloc, typename... _Args>
525     inline shared_ptr<_Tp>
526     allocate_shared(const _Alloc& __a, _Args&&... __args)
527     {
528       return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a,
529                              std::forward<_Args>(__args)...);
530     }
531
532   /**
533    *  @brief  Create an object that is owned by a shared_ptr.
534    *  @param  __args  Arguments for the @a _Tp object's constructor.
535    *  @return A shared_ptr that owns the newly created object.
536    *  @throw  std::bad_alloc, or an exception thrown from the
537    *          constructor of @a _Tp.
538    */
539   template<typename _Tp, typename... _Args>
540     inline shared_ptr<_Tp>
541     make_shared(_Args&&... __args)
542     {
543       typedef typename std::remove_const<_Tp>::type _Tp_nc;
544       return allocate_shared<_Tp>(std::allocator<_Tp_nc>(),
545                                   std::forward<_Args>(__args)...);
546     }
547
548   /// std::hash specialization for shared_ptr.
549   template<typename _Tp>
550     struct hash<shared_ptr<_Tp>>
551     : public std::unary_function<shared_ptr<_Tp>, size_t>
552     {
553       size_t
554       operator()(const shared_ptr<_Tp>& __s) const
555       { return std::hash<_Tp*>()(__s.get()); }
556     };
557
558   // @} group pointer_abstractions
559
560 _GLIBCXX_END_NAMESPACE
561
562 #endif // _SHARED_PTR_H