OpenCores
URL https://opencores.org/ocsvn/altor32/altor32/trunk

Subversion Repositories altor32

[/] [altor32/] [trunk/] [gcc-x64/] [or1knd-elf/] [or1knd-elf/] [include/] [c++/] [4.8.0/] [bits/] [shared_ptr_base.h] - Blame information for rev 35

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 35 ultra_embe
// shared_ptr and weak_ptr implementation details -*- C++ -*-
2
 
3
// Copyright (C) 2007-2012 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_base.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_BASE_H
50
#define _SHARED_PTR_BASE_H 1
51
 
52
namespace std _GLIBCXX_VISIBILITY(default)
53
{
54
_GLIBCXX_BEGIN_NAMESPACE_VERSION
55
 
56
#if _GLIBCXX_USE_DEPRECATED
57
  template<typename> class auto_ptr;
58
#endif
59
 
60
 /**
61
   *  @brief  Exception possibly thrown by @c shared_ptr.
62
   *  @ingroup exceptions
63
   */
64
  class bad_weak_ptr : public std::exception
65
  {
66
  public:
67
    virtual char const*
68
    what() const noexcept;
69
 
70
    virtual ~bad_weak_ptr() noexcept;
71
  };
72
 
73
  // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
74
  inline void
75
  __throw_bad_weak_ptr()
76
  { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr()); }
77
 
78
  using __gnu_cxx::_Lock_policy;
79
  using __gnu_cxx::__default_lock_policy;
80
  using __gnu_cxx::_S_single;
81
  using __gnu_cxx::_S_mutex;
82
  using __gnu_cxx::_S_atomic;
83
 
84
  // Empty helper class except when the template argument is _S_mutex.
85
  template<_Lock_policy _Lp>
86
    class _Mutex_base
87
    {
88
    protected:
89
      // The atomic policy uses fully-fenced builtins, single doesn't care.
90
      enum { _S_need_barriers = 0 };
91
    };
92
 
93
  template<>
94
    class _Mutex_base<_S_mutex>
95
    : public __gnu_cxx::__mutex
96
    {
97
    protected:
98
      // This policy is used when atomic builtins are not available.
99
      // The replacement atomic operations might not have the necessary
100
      // memory barriers.
101
      enum { _S_need_barriers = 1 };
102
    };
103
 
104
  template<_Lock_policy _Lp = __default_lock_policy>
105
    class _Sp_counted_base
106
    : public _Mutex_base<_Lp>
107
    {
108
    public:
109
      _Sp_counted_base() noexcept
110
      : _M_use_count(1), _M_weak_count(1) { }
111
 
112
      virtual
113
      ~_Sp_counted_base() noexcept
114
      { }
115
 
116
      // Called when _M_use_count drops to zero, to release the resources
117
      // managed by *this.
118
      virtual void
119
      _M_dispose() noexcept = 0;
120
 
121
      // Called when _M_weak_count drops to zero.
122
      virtual void
123
      _M_destroy() noexcept
124
      { delete this; }
125
 
126
      virtual void*
127
      _M_get_deleter(const std::type_info&) = 0;
128
 
129
      void
130
      _M_add_ref_copy()
131
      { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
132
 
133
      void
134
      _M_add_ref_lock();
135
 
136
      void
137
      _M_release() noexcept
138
      {
139
        // Be race-detector-friendly.  For more info see bits/c++config.
140
        _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
141
        if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
142
          {
143
            _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
144
            _M_dispose();
145
            // There must be a memory barrier between dispose() and destroy()
146
            // to ensure that the effects of dispose() are observed in the
147
            // thread that runs destroy().
148
            // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
149
            if (_Mutex_base<_Lp>::_S_need_barriers)
150
              {
151
                _GLIBCXX_READ_MEM_BARRIER;
152
                _GLIBCXX_WRITE_MEM_BARRIER;
153
              }
154
 
155
            // Be race-detector-friendly.  For more info see bits/c++config.
156
            _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
157
            if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
158
                                                       -1) == 1)
159
              {
160
                _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
161
                _M_destroy();
162
              }
163
          }
164
      }
165
 
166
      void
167
      _M_weak_add_ref() noexcept
168
      { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
169
 
170
      void
171
      _M_weak_release() noexcept
172
      {
173
        // Be race-detector-friendly. For more info see bits/c++config.
174
        _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
175
        if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
176
          {
177
            _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
178
            if (_Mutex_base<_Lp>::_S_need_barriers)
179
              {
180
                // See _M_release(),
181
                // destroy() must observe results of dispose()
182
                _GLIBCXX_READ_MEM_BARRIER;
183
                _GLIBCXX_WRITE_MEM_BARRIER;
184
              }
185
            _M_destroy();
186
          }
187
      }
188
 
189
      long
190
      _M_get_use_count() const noexcept
191
      {
192
        // No memory barrier is used here so there is no synchronization
193
        // with other threads.
194
        return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED);
195
      }
196
 
197
    private:
198
      _Sp_counted_base(_Sp_counted_base const&) = delete;
199
      _Sp_counted_base& operator=(_Sp_counted_base const&) = delete;
200
 
201
      _Atomic_word  _M_use_count;     // #shared
202
      _Atomic_word  _M_weak_count;    // #weak + (#shared != 0)
203
    };
204
 
205
  template<>
206
    inline void
207
    _Sp_counted_base<_S_single>::
208
    _M_add_ref_lock()
209
    {
210
      if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
211
        {
212
          _M_use_count = 0;
213
          __throw_bad_weak_ptr();
214
        }
215
    }
216
 
217
  template<>
218
    inline void
219
    _Sp_counted_base<_S_mutex>::
220
    _M_add_ref_lock()
221
    {
222
      __gnu_cxx::__scoped_lock sentry(*this);
223
      if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
224
        {
225
          _M_use_count = 0;
226
          __throw_bad_weak_ptr();
227
        }
228
    }
229
 
230
  template<>
231
    inline void
232
    _Sp_counted_base<_S_atomic>::
233
    _M_add_ref_lock()
234
    {
235
      // Perform lock-free add-if-not-zero operation.
236
      _Atomic_word __count = _M_use_count;
237
      do
238
        {
239
          if (__count == 0)
240
            __throw_bad_weak_ptr();
241
          // Replace the current counter value with the old value + 1, as
242
          // long as it's not changed meanwhile. 
243
        }
244
      while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1,
245
                                          true, __ATOMIC_ACQ_REL,
246
                                          __ATOMIC_RELAXED));
247
    }
248
 
249
 
250
  // Forward declarations.
251
  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
252
    class __shared_ptr;
253
 
254
  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
255
    class __weak_ptr;
256
 
257
  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
258
    class __enable_shared_from_this;
259
 
260
  template<typename _Tp>
261
    class shared_ptr;
262
 
263
  template<typename _Tp>
264
    class weak_ptr;
265
 
266
  template<typename _Tp>
267
    struct owner_less;
268
 
269
  template<typename _Tp>
270
    class enable_shared_from_this;
271
 
272
  template<_Lock_policy _Lp = __default_lock_policy>
273
    class __weak_count;
274
 
275
  template<_Lock_policy _Lp = __default_lock_policy>
276
    class __shared_count;
277
 
278
 
279
  // Counted ptr with no deleter or allocator support
280
  template<typename _Ptr, _Lock_policy _Lp>
281
    class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
282
    {
283
    public:
284
      explicit
285
      _Sp_counted_ptr(_Ptr __p)
286
      : _M_ptr(__p) { }
287
 
288
      virtual void
289
      _M_dispose() noexcept
290
      { delete _M_ptr; }
291
 
292
      virtual void
293
      _M_destroy() noexcept
294
      { delete this; }
295
 
296
      virtual void*
297
      _M_get_deleter(const std::type_info&)
298
      { return 0; }
299
 
300
      _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
301
      _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
302
 
303
    protected:
304
      _Ptr             _M_ptr;  // copy constructor must not throw
305
    };
306
 
307
  template<>
308
    inline void
309
    _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { }
310
 
311
  template<>
312
    inline void
313
    _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { }
314
 
315
  template<>
316
    inline void
317
    _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { }
318
 
319
  // Support for custom deleter and/or allocator
320
  template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp>
321
    class _Sp_counted_deleter final : public _Sp_counted_base<_Lp>
322
    {
323
      // Helper class that stores the Deleter and also acts as an allocator.
324
      // Used to dispose of the owned pointer and the internal refcount
325
      // Requires that copies of _Alloc can free each other's memory.
326
      struct _My_Deleter
327
      : public _Alloc           // copy constructor must not throw
328
      {
329
        _Deleter _M_del;        // copy constructor must not throw
330
        _My_Deleter(_Deleter __d, const _Alloc& __a)
331
        : _Alloc(__a), _M_del(__d) { }
332
      };
333
 
334
    public:
335
      // __d(__p) must not throw.
336
      _Sp_counted_deleter(_Ptr __p, _Deleter __d)
337
      : _M_ptr(__p), _M_del(__d, _Alloc()) { }
338
 
339
      // __d(__p) must not throw.
340
      _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a)
341
      : _M_ptr(__p), _M_del(__d, __a) { }
342
 
343
      ~_Sp_counted_deleter() noexcept { }
344
 
345
      virtual void
346
      _M_dispose() noexcept
347
      { _M_del._M_del(_M_ptr); }
348
 
349
      virtual void
350
      _M_destroy() noexcept
351
      {
352
        typedef typename allocator_traits<_Alloc>::template
353
          rebind_traits<_Sp_counted_deleter> _Alloc_traits;
354
        typename _Alloc_traits::allocator_type __a(_M_del);
355
        _Alloc_traits::destroy(__a, this);
356
        _Alloc_traits::deallocate(__a, this, 1);
357
      }
358
 
359
      virtual void*
360
      _M_get_deleter(const std::type_info& __ti)
361
      {
362
#ifdef __GXX_RTTI
363
        return __ti == typeid(_Deleter) ? &_M_del._M_del : 0;
364
#else
365
        return 0;
366
#endif
367
      }
368
 
369
    protected:
370
      _Ptr             _M_ptr;  // copy constructor must not throw
371
      _My_Deleter      _M_del;  // copy constructor must not throw
372
    };
373
 
374
  // helpers for make_shared / allocate_shared
375
 
376
  struct _Sp_make_shared_tag { };
377
 
378
  template<typename _Tp, typename _Alloc, _Lock_policy _Lp>
379
    class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp>
380
    {
381
      // Helper class that stores the pointer and also acts as an allocator.
382
      // Used to dispose of the owned pointer and the internal refcount
383
      // Requires that copies of _Alloc can free each other's memory.
384
      struct _Impl
385
      : public _Alloc           // copy constructor must not throw
386
      {
387
        _Impl(_Alloc __a) : _Alloc(__a), _M_ptr() { }
388
        _Tp* _M_ptr;
389
      };
390
 
391
    public:
392
      template<typename... _Args>
393
        _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
394
        : _M_impl(__a), _M_storage()
395
        {
396
          _M_impl._M_ptr = static_cast<_Tp*>(static_cast<void*>(&_M_storage));
397
          // _GLIBCXX_RESOLVE_LIB_DEFECTS
398
          // 2070.  allocate_shared should use allocator_traits<A>::construct
399
          allocator_traits<_Alloc>::construct(__a, _M_impl._M_ptr,
400
              std::forward<_Args>(__args)...); // might throw
401
        }
402
 
403
      ~_Sp_counted_ptr_inplace() noexcept { }
404
 
405
      virtual void
406
      _M_dispose() noexcept
407
      { allocator_traits<_Alloc>::destroy(_M_impl, _M_impl._M_ptr); }
408
 
409
      // Override because the allocator needs to know the dynamic type
410
      virtual void
411
      _M_destroy() noexcept
412
      {
413
        typedef typename allocator_traits<_Alloc>::template
414
          rebind_traits<_Sp_counted_ptr_inplace> _Alloc_traits;
415
        typename _Alloc_traits::allocator_type __a(_M_impl);
416
        _Alloc_traits::destroy(__a, this);
417
        _Alloc_traits::deallocate(__a, this, 1);
418
      }
419
 
420
      // Sneaky trick so __shared_ptr can get the managed pointer
421
      virtual void*
422
      _M_get_deleter(const std::type_info& __ti) noexcept
423
      {
424
#ifdef __GXX_RTTI
425
        return __ti == typeid(_Sp_make_shared_tag)
426
               ? static_cast<void*>(&_M_storage)
427
               : 0;
428
#else
429
        return 0;
430
#endif
431
      }
432
 
433
    private:
434
      _Impl _M_impl;
435
      typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
436
        _M_storage;
437
    };
438
 
439
  template<_Lock_policy _Lp>
440
    class __shared_count
441
    {
442
    public:
443
      constexpr __shared_count() noexcept : _M_pi(0)
444
      { }
445
 
446
      template<typename _Ptr>
447
        explicit
448
        __shared_count(_Ptr __p) : _M_pi(0)
449
        {
450
          __try
451
            {
452
              _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
453
            }
454
          __catch(...)
455
            {
456
              delete __p;
457
              __throw_exception_again;
458
            }
459
        }
460
 
461
      template<typename _Ptr, typename _Deleter>
462
        __shared_count(_Ptr __p, _Deleter __d) : _M_pi(0)
463
        {
464
          // The allocator's value_type doesn't matter, will rebind it anyway.
465
          typedef std::allocator<int> _Alloc;
466
          typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
467
          typedef typename allocator_traits<_Alloc>::template
468
            rebind_traits<_Sp_cd_type> _Alloc_traits;
469
          typename _Alloc_traits::allocator_type __a;
470
          _Sp_cd_type* __mem = 0;
471
          __try
472
            {
473
              __mem = _Alloc_traits::allocate(__a, 1);
474
              _Alloc_traits::construct(__a, __mem, __p, std::move(__d));
475
              _M_pi = __mem;
476
            }
477
          __catch(...)
478
            {
479
              __d(__p); // Call _Deleter on __p.
480
              if (__mem)
481
                _Alloc_traits::deallocate(__a, __mem, 1);
482
              __throw_exception_again;
483
            }
484
        }
485
 
486
      template<typename _Ptr, typename _Deleter, typename _Alloc>
487
        __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0)
488
        {
489
          typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type;
490
          typedef typename allocator_traits<_Alloc>::template
491
            rebind_traits<_Sp_cd_type> _Alloc_traits;
492
          typename _Alloc_traits::allocator_type __a2(__a);
493
          _Sp_cd_type* __mem = 0;
494
          __try
495
            {
496
              __mem = _Alloc_traits::allocate(__a2, 1);
497
              _Alloc_traits::construct(__a2, __mem,
498
                  __p, std::move(__d), std::move(__a));
499
              _M_pi = __mem;
500
            }
501
          __catch(...)
502
            {
503
              __d(__p); // Call _Deleter on __p.
504
              if (__mem)
505
                _Alloc_traits::deallocate(__a2, __mem, 1);
506
              __throw_exception_again;
507
            }
508
        }
509
 
510
      template<typename _Tp, typename _Alloc, typename... _Args>
511
        __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a,
512
                       _Args&&... __args)
513
        : _M_pi(0)
514
        {
515
          typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type;
516
          typedef typename allocator_traits<_Alloc>::template
517
            rebind_traits<_Sp_cp_type> _Alloc_traits;
518
          typename _Alloc_traits::allocator_type __a2(__a);
519
          _Sp_cp_type* __mem = _Alloc_traits::allocate(__a2, 1);
520
          __try
521
            {
522
              _Alloc_traits::construct(__a2, __mem, std::move(__a),
523
                    std::forward<_Args>(__args)...);
524
              _M_pi = __mem;
525
            }
526
          __catch(...)
527
            {
528
              _Alloc_traits::deallocate(__a2, __mem, 1);
529
              __throw_exception_again;
530
            }
531
        }
532
 
533
#if _GLIBCXX_USE_DEPRECATED
534
      // Special case for auto_ptr<_Tp> to provide the strong guarantee.
535
      template<typename _Tp>
536
        explicit
537
        __shared_count(std::auto_ptr<_Tp>&& __r);
538
#endif
539
 
540
      // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee.
541
      template<typename _Tp, typename _Del>
542
        explicit
543
        __shared_count(std::unique_ptr<_Tp, _Del>&& __r)
544
        : _M_pi(_S_create_from_up(std::move(__r)))
545
        { __r.release(); }
546
 
547
      // Throw bad_weak_ptr when __r._M_get_use_count() == 0.
548
      explicit __shared_count(const __weak_count<_Lp>& __r);
549
 
550
      ~__shared_count() noexcept
551
      {
552
        if (_M_pi != 0)
553
          _M_pi->_M_release();
554
      }
555
 
556
      __shared_count(const __shared_count& __r) noexcept
557
      : _M_pi(__r._M_pi)
558
      {
559
        if (_M_pi != 0)
560
          _M_pi->_M_add_ref_copy();
561
      }
562
 
563
      __shared_count&
564
      operator=(const __shared_count& __r) noexcept
565
      {
566
        _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
567
        if (__tmp != _M_pi)
568
          {
569
            if (__tmp != 0)
570
              __tmp->_M_add_ref_copy();
571
            if (_M_pi != 0)
572
              _M_pi->_M_release();
573
            _M_pi = __tmp;
574
          }
575
        return *this;
576
      }
577
 
578
      void
579
      _M_swap(__shared_count& __r) noexcept
580
      {
581
        _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
582
        __r._M_pi = _M_pi;
583
        _M_pi = __tmp;
584
      }
585
 
586
      long
587
      _M_get_use_count() const noexcept
588
      { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
589
 
590
      bool
591
      _M_unique() const noexcept
592
      { return this->_M_get_use_count() == 1; }
593
 
594
      void*
595
      _M_get_deleter(const std::type_info& __ti) const noexcept
596
      { return _M_pi ? _M_pi->_M_get_deleter(__ti) : 0; }
597
 
598
      bool
599
      _M_less(const __shared_count& __rhs) const noexcept
600
      { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
601
 
602
      bool
603
      _M_less(const __weak_count<_Lp>& __rhs) const noexcept
604
      { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
605
 
606
      // Friend function injected into enclosing namespace and found by ADL
607
      friend inline bool
608
      operator==(const __shared_count& __a, const __shared_count& __b) noexcept
609
      { return __a._M_pi == __b._M_pi; }
610
 
611
    private:
612
      friend class __weak_count<_Lp>;
613
 
614
      template<typename _Tp, typename _Del>
615
        static _Sp_counted_base<_Lp>*
616
        _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
617
          typename std::enable_if<!std::is_reference<_Del>::value>::type* = 0)
618
        {
619
          return new _Sp_counted_deleter<_Tp*, _Del, std::allocator<void>,
620
            _Lp>(__r.get(), __r.get_deleter());
621
        }
622
 
623
      template<typename _Tp, typename _Del>
624
        static _Sp_counted_base<_Lp>*
625
        _S_create_from_up(std::unique_ptr<_Tp, _Del>&& __r,
626
          typename std::enable_if<std::is_reference<_Del>::value>::type* = 0)
627
        {
628
          typedef typename std::remove_reference<_Del>::type _Del1;
629
          typedef std::reference_wrapper<_Del1> _Del2;
630
          return new _Sp_counted_deleter<_Tp*, _Del2, std::allocator<void>,
631
            _Lp>(__r.get(), std::ref(__r.get_deleter()));
632
        }
633
 
634
      _Sp_counted_base<_Lp>*  _M_pi;
635
    };
636
 
637
 
638
  template<_Lock_policy _Lp>
639
    class __weak_count
640
    {
641
    public:
642
      constexpr __weak_count() noexcept : _M_pi(0)
643
      { }
644
 
645
      __weak_count(const __shared_count<_Lp>& __r) noexcept
646
      : _M_pi(__r._M_pi)
647
      {
648
        if (_M_pi != 0)
649
          _M_pi->_M_weak_add_ref();
650
      }
651
 
652
      __weak_count(const __weak_count<_Lp>& __r) noexcept
653
      : _M_pi(__r._M_pi)
654
      {
655
        if (_M_pi != 0)
656
          _M_pi->_M_weak_add_ref();
657
      }
658
 
659
      ~__weak_count() noexcept
660
      {
661
        if (_M_pi != 0)
662
          _M_pi->_M_weak_release();
663
      }
664
 
665
      __weak_count<_Lp>&
666
      operator=(const __shared_count<_Lp>& __r) noexcept
667
      {
668
        _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
669
        if (__tmp != 0)
670
          __tmp->_M_weak_add_ref();
671
        if (_M_pi != 0)
672
          _M_pi->_M_weak_release();
673
        _M_pi = __tmp;
674
        return *this;
675
      }
676
 
677
      __weak_count<_Lp>&
678
      operator=(const __weak_count<_Lp>& __r) noexcept
679
      {
680
        _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
681
        if (__tmp != 0)
682
          __tmp->_M_weak_add_ref();
683
        if (_M_pi != 0)
684
          _M_pi->_M_weak_release();
685
        _M_pi = __tmp;
686
        return *this;
687
      }
688
 
689
      void
690
      _M_swap(__weak_count<_Lp>& __r) noexcept
691
      {
692
        _Sp_counted_base<_Lp>* __tmp = __r._M_pi;
693
        __r._M_pi = _M_pi;
694
        _M_pi = __tmp;
695
      }
696
 
697
      long
698
      _M_get_use_count() const noexcept
699
      { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; }
700
 
701
      bool
702
      _M_less(const __weak_count& __rhs) const noexcept
703
      { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
704
 
705
      bool
706
      _M_less(const __shared_count<_Lp>& __rhs) const noexcept
707
      { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); }
708
 
709
      // Friend function injected into enclosing namespace and found by ADL
710
      friend inline bool
711
      operator==(const __weak_count& __a, const __weak_count& __b) noexcept
712
      { return __a._M_pi == __b._M_pi; }
713
 
714
    private:
715
      friend class __shared_count<_Lp>;
716
 
717
      _Sp_counted_base<_Lp>*  _M_pi;
718
    };
719
 
720
  // Now that __weak_count is defined we can define this constructor:
721
  template<_Lock_policy _Lp>
722
    inline __shared_count<_Lp>:: __shared_count(const __weak_count<_Lp>& __r)
723
    : _M_pi(__r._M_pi)
724
    {
725
      if (_M_pi != 0)
726
        _M_pi->_M_add_ref_lock();
727
      else
728
        __throw_bad_weak_ptr();
729
    }
730
 
731
 
732
  // Support for enable_shared_from_this.
733
 
734
  // Friend of __enable_shared_from_this.
735
  template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
736
    void
737
    __enable_shared_from_this_helper(const __shared_count<_Lp>&,
738
                                     const __enable_shared_from_this<_Tp1,
739
                                     _Lp>*, const _Tp2*) noexcept;
740
 
741
  // Friend of enable_shared_from_this.
742
  template<typename _Tp1, typename _Tp2>
743
    void
744
    __enable_shared_from_this_helper(const __shared_count<>&,
745
                                     const enable_shared_from_this<_Tp1>*,
746
                                     const _Tp2*) noexcept;
747
 
748
  template<_Lock_policy _Lp>
749
    inline void
750
    __enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
751
    { }
752
 
753
 
754
  template<typename _Tp, _Lock_policy _Lp>
755
    class __shared_ptr
756
    {
757
    public:
758
      typedef _Tp   element_type;
759
 
760
      constexpr __shared_ptr() noexcept
761
      : _M_ptr(0), _M_refcount()
762
      { }
763
 
764
      template<typename _Tp1>
765
        explicit __shared_ptr(_Tp1* __p)
766
        : _M_ptr(__p), _M_refcount(__p)
767
        {
768
          __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
769
          static_assert( sizeof(_Tp1) > 0, "incomplete type" );
770
          __enable_shared_from_this_helper(_M_refcount, __p, __p);
771
        }
772
 
773
      template<typename _Tp1, typename _Deleter>
774
        __shared_ptr(_Tp1* __p, _Deleter __d)
775
        : _M_ptr(__p), _M_refcount(__p, __d)
776
        {
777
          __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
778
          // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
779
          __enable_shared_from_this_helper(_M_refcount, __p, __p);
780
        }
781
 
782
      template<typename _Tp1, typename _Deleter, typename _Alloc>
783
        __shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
784
        : _M_ptr(__p), _M_refcount(__p, __d, std::move(__a))
785
        {
786
          __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
787
          // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
788
          __enable_shared_from_this_helper(_M_refcount, __p, __p);
789
        }
790
 
791
      template<typename _Deleter>
792
        __shared_ptr(nullptr_t __p, _Deleter __d)
793
        : _M_ptr(0), _M_refcount(__p, __d)
794
        { }
795
 
796
      template<typename _Deleter, typename _Alloc>
797
        __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
798
        : _M_ptr(0), _M_refcount(__p, __d, std::move(__a))
799
        { }
800
 
801
      template<typename _Tp1>
802
        __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r, _Tp* __p) noexcept
803
        : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws
804
        { }
805
 
806
      __shared_ptr(const __shared_ptr&) noexcept = default;
807
      __shared_ptr& operator=(const __shared_ptr&) noexcept = default;
808
      ~__shared_ptr() = default;
809
 
810
      template<typename _Tp1, typename = typename
811
               std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
812
        __shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
813
        : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
814
        { }
815
 
816
      __shared_ptr(__shared_ptr&& __r) noexcept
817
      : _M_ptr(__r._M_ptr), _M_refcount()
818
      {
819
        _M_refcount._M_swap(__r._M_refcount);
820
        __r._M_ptr = 0;
821
      }
822
 
823
      template<typename _Tp1, typename = typename
824
               std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
825
        __shared_ptr(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
826
        : _M_ptr(__r._M_ptr), _M_refcount()
827
        {
828
          _M_refcount._M_swap(__r._M_refcount);
829
          __r._M_ptr = 0;
830
        }
831
 
832
      template<typename _Tp1>
833
        explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
834
        : _M_refcount(__r._M_refcount) // may throw
835
        {
836
          __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
837
 
838
          // It is now safe to copy __r._M_ptr, as
839
          // _M_refcount(__r._M_refcount) did not throw.
840
          _M_ptr = __r._M_ptr;
841
        }
842
 
843
      // If an exception is thrown this constructor has no effect.
844
      template<typename _Tp1, typename _Del>
845
        __shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
846
        : _M_ptr(__r.get()), _M_refcount()
847
        {
848
          __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
849
          _Tp1* __tmp = __r.get();
850
          _M_refcount = __shared_count<_Lp>(std::move(__r));
851
          __enable_shared_from_this_helper(_M_refcount, __tmp, __tmp);
852
        }
853
 
854
#if _GLIBCXX_USE_DEPRECATED
855
      // Postcondition: use_count() == 1 and __r.get() == 0
856
      template<typename _Tp1>
857
        __shared_ptr(std::auto_ptr<_Tp1>&& __r);
858
#endif
859
 
860
      /* TODO: use delegating constructor */
861
      constexpr __shared_ptr(nullptr_t) noexcept
862
      : _M_ptr(0), _M_refcount()
863
      { }
864
 
865
      template<typename _Tp1>
866
        __shared_ptr&
867
        operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
868
        {
869
          _M_ptr = __r._M_ptr;
870
          _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw
871
          return *this;
872
        }
873
 
874
#if _GLIBCXX_USE_DEPRECATED
875
      template<typename _Tp1>
876
        __shared_ptr&
877
        operator=(std::auto_ptr<_Tp1>&& __r)
878
        {
879
          __shared_ptr(std::move(__r)).swap(*this);
880
          return *this;
881
        }
882
#endif
883
 
884
      __shared_ptr&
885
      operator=(__shared_ptr&& __r) noexcept
886
      {
887
        __shared_ptr(std::move(__r)).swap(*this);
888
        return *this;
889
      }
890
 
891
      template<class _Tp1>
892
        __shared_ptr&
893
        operator=(__shared_ptr<_Tp1, _Lp>&& __r) noexcept
894
        {
895
          __shared_ptr(std::move(__r)).swap(*this);
896
          return *this;
897
        }
898
 
899
      template<typename _Tp1, typename _Del>
900
        __shared_ptr&
901
        operator=(std::unique_ptr<_Tp1, _Del>&& __r)
902
        {
903
          __shared_ptr(std::move(__r)).swap(*this);
904
          return *this;
905
        }
906
 
907
      void
908
      reset() noexcept
909
      { __shared_ptr().swap(*this); }
910
 
911
      template<typename _Tp1>
912
        void
913
        reset(_Tp1* __p) // _Tp1 must be complete.
914
        {
915
          // Catch self-reset errors.
916
          _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr);
917
          __shared_ptr(__p).swap(*this);
918
        }
919
 
920
      template<typename _Tp1, typename _Deleter>
921
        void
922
        reset(_Tp1* __p, _Deleter __d)
923
        { __shared_ptr(__p, __d).swap(*this); }
924
 
925
      template<typename _Tp1, typename _Deleter, typename _Alloc>
926
        void
927
        reset(_Tp1* __p, _Deleter __d, _Alloc __a)
928
        { __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
929
 
930
      // Allow class instantiation when _Tp is [cv-qual] void.
931
      typename std::add_lvalue_reference<_Tp>::type
932
      operator*() const noexcept
933
      {
934
        _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
935
        return *_M_ptr;
936
      }
937
 
938
      _Tp*
939
      operator->() const noexcept
940
      {
941
        _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
942
        return _M_ptr;
943
      }
944
 
945
      _Tp*
946
      get() const noexcept
947
      { return _M_ptr; }
948
 
949
      explicit operator bool() const // never throws
950
      { return _M_ptr == 0 ? false : true; }
951
 
952
      bool
953
      unique() const noexcept
954
      { return _M_refcount._M_unique(); }
955
 
956
      long
957
      use_count() const noexcept
958
      { return _M_refcount._M_get_use_count(); }
959
 
960
      void
961
      swap(__shared_ptr<_Tp, _Lp>& __other) noexcept
962
      {
963
        std::swap(_M_ptr, __other._M_ptr);
964
        _M_refcount._M_swap(__other._M_refcount);
965
      }
966
 
967
      template<typename _Tp1>
968
        bool
969
        owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const
970
        { return _M_refcount._M_less(__rhs._M_refcount); }
971
 
972
      template<typename _Tp1>
973
        bool
974
        owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const
975
        { return _M_refcount._M_less(__rhs._M_refcount); }
976
 
977
#ifdef __GXX_RTTI
978
    protected:
979
      // This constructor is non-standard, it is used by allocate_shared.
980
      template<typename _Alloc, typename... _Args>
981
        __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
982
                     _Args&&... __args)
983
        : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a,
984
                                std::forward<_Args>(__args)...)
985
        {
986
          // _M_ptr needs to point to the newly constructed object.
987
          // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
988
          void* __p = _M_refcount._M_get_deleter(typeid(__tag));
989
          _M_ptr = static_cast<_Tp*>(__p);
990
          __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
991
        }
992
#else
993
      template<typename _Alloc>
994
        struct _Deleter
995
        {
996
          void operator()(_Tp* __ptr)
997
          {
998
            typedef allocator_traits<_Alloc> _Alloc_traits;
999
            _Alloc_traits::destroy(_M_alloc, __ptr);
1000
            _Alloc_traits::deallocate(_M_alloc, __ptr, 1);
1001
          }
1002
          _Alloc _M_alloc;
1003
        };
1004
 
1005
      template<typename _Alloc, typename... _Args>
1006
        __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
1007
                     _Args&&... __args)
1008
        : _M_ptr(), _M_refcount()
1009
        {
1010
          typedef typename _Alloc::template rebind<_Tp>::other _Alloc2;
1011
          _Deleter<_Alloc2> __del = { _Alloc2(__a) };
1012
          typedef allocator_traits<_Alloc2> __traits;
1013
          _M_ptr = __traits::allocate(__del._M_alloc, 1);
1014
          __try
1015
            {
1016
              // _GLIBCXX_RESOLVE_LIB_DEFECTS
1017
              // 2070. allocate_shared should use allocator_traits<A>::construct
1018
              __traits::construct(__del._M_alloc, _M_ptr,
1019
                                  std::forward<_Args>(__args)...);
1020
            }
1021
          __catch(...)
1022
            {
1023
              __traits::deallocate(__del._M_alloc, _M_ptr, 1);
1024
              __throw_exception_again;
1025
            }
1026
          __shared_count<_Lp> __count(_M_ptr, __del, __del._M_alloc);
1027
          _M_refcount._M_swap(__count);
1028
          __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
1029
        }
1030
#endif
1031
 
1032
      template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc,
1033
               typename... _Args>
1034
        friend __shared_ptr<_Tp1, _Lp1>
1035
        __allocate_shared(const _Alloc& __a, _Args&&... __args);
1036
 
1037
    private:
1038
      void*
1039
      _M_get_deleter(const std::type_info& __ti) const noexcept
1040
      { return _M_refcount._M_get_deleter(__ti); }
1041
 
1042
      template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1043
      template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1044
 
1045
      template<typename _Del, typename _Tp1, _Lock_policy _Lp1>
1046
        friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept;
1047
 
1048
      _Tp*                 _M_ptr;         // Contained pointer.
1049
      __shared_count<_Lp>  _M_refcount;    // Reference counter.
1050
    };
1051
 
1052
 
1053
  // 20.7.2.2.7 shared_ptr comparisons
1054
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1055
    inline bool
1056
    operator==(const __shared_ptr<_Tp1, _Lp>& __a,
1057
               const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1058
    { return __a.get() == __b.get(); }
1059
 
1060
  template<typename _Tp, _Lock_policy _Lp>
1061
    inline bool
1062
    operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1063
    { return !__a; }
1064
 
1065
  template<typename _Tp, _Lock_policy _Lp>
1066
    inline bool
1067
    operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1068
    { return !__a; }
1069
 
1070
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1071
    inline bool
1072
    operator!=(const __shared_ptr<_Tp1, _Lp>& __a,
1073
               const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1074
    { return __a.get() != __b.get(); }
1075
 
1076
  template<typename _Tp, _Lock_policy _Lp>
1077
    inline bool
1078
    operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1079
    { return (bool)__a; }
1080
 
1081
  template<typename _Tp, _Lock_policy _Lp>
1082
    inline bool
1083
    operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1084
    { return (bool)__a; }
1085
 
1086
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1087
    inline bool
1088
    operator<(const __shared_ptr<_Tp1, _Lp>& __a,
1089
              const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1090
    {
1091
      typedef typename std::common_type<_Tp1*, _Tp2*>::type _CT;
1092
      return std::less<_CT>()(__a.get(), __b.get());
1093
    }
1094
 
1095
  template<typename _Tp, _Lock_policy _Lp>
1096
    inline bool
1097
    operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1098
    { return std::less<_Tp*>()(__a.get(), nullptr); }
1099
 
1100
  template<typename _Tp, _Lock_policy _Lp>
1101
    inline bool
1102
    operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1103
    { return std::less<_Tp*>()(nullptr, __a.get()); }
1104
 
1105
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1106
    inline bool
1107
    operator<=(const __shared_ptr<_Tp1, _Lp>& __a,
1108
               const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1109
    { return !(__b < __a); }
1110
 
1111
  template<typename _Tp, _Lock_policy _Lp>
1112
    inline bool
1113
    operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1114
    { return !(nullptr < __a); }
1115
 
1116
  template<typename _Tp, _Lock_policy _Lp>
1117
    inline bool
1118
    operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1119
    { return !(__a < nullptr); }
1120
 
1121
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1122
    inline bool
1123
    operator>(const __shared_ptr<_Tp1, _Lp>& __a,
1124
              const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1125
    { return (__b < __a); }
1126
 
1127
  template<typename _Tp, _Lock_policy _Lp>
1128
    inline bool
1129
    operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1130
    { return std::less<_Tp*>()(nullptr, __a.get()); }
1131
 
1132
  template<typename _Tp, _Lock_policy _Lp>
1133
    inline bool
1134
    operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1135
    { return std::less<_Tp*>()(__a.get(), nullptr); }
1136
 
1137
  template<typename _Tp1, typename _Tp2, _Lock_policy _Lp>
1138
    inline bool
1139
    operator>=(const __shared_ptr<_Tp1, _Lp>& __a,
1140
               const __shared_ptr<_Tp2, _Lp>& __b) noexcept
1141
    { return !(__a < __b); }
1142
 
1143
  template<typename _Tp, _Lock_policy _Lp>
1144
    inline bool
1145
    operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
1146
    { return !(__a < nullptr); }
1147
 
1148
  template<typename _Tp, _Lock_policy _Lp>
1149
    inline bool
1150
    operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
1151
    { return !(nullptr < __a); }
1152
 
1153
  template<typename _Sp>
1154
    struct _Sp_less : public binary_function<_Sp, _Sp, bool>
1155
    {
1156
      bool
1157
      operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept
1158
      {
1159
        typedef typename _Sp::element_type element_type;
1160
        return std::less<element_type*>()(__lhs.get(), __rhs.get());
1161
      }
1162
    };
1163
 
1164
  template<typename _Tp, _Lock_policy _Lp>
1165
    struct less<__shared_ptr<_Tp, _Lp>>
1166
    : public _Sp_less<__shared_ptr<_Tp, _Lp>>
1167
    { };
1168
 
1169
  // 2.2.3.8 shared_ptr specialized algorithms.
1170
  template<typename _Tp, _Lock_policy _Lp>
1171
    inline void
1172
    swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept
1173
    { __a.swap(__b); }
1174
 
1175
  // 2.2.3.9 shared_ptr casts
1176
 
1177
  // The seemingly equivalent code:
1178
  // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get()))
1179
  // will eventually result in undefined behaviour, attempting to
1180
  // delete the same object twice.
1181
  /// static_pointer_cast
1182
  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1183
    inline __shared_ptr<_Tp, _Lp>
1184
    static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1185
    { return __shared_ptr<_Tp, _Lp>(__r, static_cast<_Tp*>(__r.get())); }
1186
 
1187
  // The seemingly equivalent code:
1188
  // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get()))
1189
  // will eventually result in undefined behaviour, attempting to
1190
  // delete the same object twice.
1191
  /// const_pointer_cast
1192
  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1193
    inline __shared_ptr<_Tp, _Lp>
1194
    const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1195
    { return __shared_ptr<_Tp, _Lp>(__r, const_cast<_Tp*>(__r.get())); }
1196
 
1197
  // The seemingly equivalent code:
1198
  // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get()))
1199
  // will eventually result in undefined behaviour, attempting to
1200
  // delete the same object twice.
1201
  /// dynamic_pointer_cast
1202
  template<typename _Tp, typename _Tp1, _Lock_policy _Lp>
1203
    inline __shared_ptr<_Tp, _Lp>
1204
    dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1205
    {
1206
      if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
1207
        return __shared_ptr<_Tp, _Lp>(__r, __p);
1208
      return __shared_ptr<_Tp, _Lp>();
1209
    }
1210
 
1211
 
1212
  template<typename _Tp, _Lock_policy _Lp>
1213
    class __weak_ptr
1214
    {
1215
    public:
1216
      typedef _Tp element_type;
1217
 
1218
      constexpr __weak_ptr() noexcept
1219
      : _M_ptr(0), _M_refcount()
1220
      { }
1221
 
1222
      __weak_ptr(const __weak_ptr&) noexcept = default;
1223
      __weak_ptr& operator=(const __weak_ptr&) noexcept = default;
1224
      ~__weak_ptr() = default;
1225
 
1226
      // The "obvious" converting constructor implementation:
1227
      //
1228
      //  template<typename _Tp1>
1229
      //    __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r)
1230
      //    : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
1231
      //    { }
1232
      //
1233
      // has a serious problem.
1234
      //
1235
      //  __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr)
1236
      //  conversion may require access to *__r._M_ptr (virtual inheritance).
1237
      //
1238
      // It is not possible to avoid spurious access violations since
1239
      // in multithreaded programs __r._M_ptr may be invalidated at any point.
1240
      template<typename _Tp1, typename = typename
1241
               std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1242
        __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1243
        : _M_refcount(__r._M_refcount)
1244
        { _M_ptr = __r.lock().get(); }
1245
 
1246
      template<typename _Tp1, typename = typename
1247
               std::enable_if<std::is_convertible<_Tp1*, _Tp*>::value>::type>
1248
        __weak_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1249
        : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
1250
        { }
1251
 
1252
      template<typename _Tp1>
1253
        __weak_ptr&
1254
        operator=(const __weak_ptr<_Tp1, _Lp>& __r) noexcept
1255
        {
1256
          _M_ptr = __r.lock().get();
1257
          _M_refcount = __r._M_refcount;
1258
          return *this;
1259
        }
1260
 
1261
      template<typename _Tp1>
1262
        __weak_ptr&
1263
        operator=(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
1264
        {
1265
          _M_ptr = __r._M_ptr;
1266
          _M_refcount = __r._M_refcount;
1267
          return *this;
1268
        }
1269
 
1270
      __shared_ptr<_Tp, _Lp>
1271
      lock() const noexcept
1272
      {
1273
#ifdef __GTHREADS
1274
        // Optimization: avoid throw overhead.
1275
        if (expired())
1276
          return __shared_ptr<element_type, _Lp>();
1277
 
1278
        __try
1279
          {
1280
            return __shared_ptr<element_type, _Lp>(*this);
1281
          }
1282
        __catch(const bad_weak_ptr&)
1283
          {
1284
            // Q: How can we get here?
1285
            // A: Another thread may have invalidated r after the
1286
            //    use_count test above.
1287
            return __shared_ptr<element_type, _Lp>();
1288
          }
1289
 
1290
#else
1291
        // Optimization: avoid try/catch overhead when single threaded.
1292
        return expired() ? __shared_ptr<element_type, _Lp>()
1293
                         : __shared_ptr<element_type, _Lp>(*this);
1294
 
1295
#endif
1296
      } // XXX MT
1297
 
1298
      long
1299
      use_count() const noexcept
1300
      { return _M_refcount._M_get_use_count(); }
1301
 
1302
      bool
1303
      expired() const noexcept
1304
      { return _M_refcount._M_get_use_count() == 0; }
1305
 
1306
      template<typename _Tp1>
1307
        bool
1308
        owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const
1309
        { return _M_refcount._M_less(__rhs._M_refcount); }
1310
 
1311
      template<typename _Tp1>
1312
        bool
1313
        owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const
1314
        { return _M_refcount._M_less(__rhs._M_refcount); }
1315
 
1316
      void
1317
      reset() noexcept
1318
      { __weak_ptr().swap(*this); }
1319
 
1320
      void
1321
      swap(__weak_ptr& __s) noexcept
1322
      {
1323
        std::swap(_M_ptr, __s._M_ptr);
1324
        _M_refcount._M_swap(__s._M_refcount);
1325
      }
1326
 
1327
    private:
1328
      // Used by __enable_shared_from_this.
1329
      void
1330
      _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept
1331
      {
1332
        _M_ptr = __ptr;
1333
        _M_refcount = __refcount;
1334
      }
1335
 
1336
      template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
1337
      template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
1338
      friend class __enable_shared_from_this<_Tp, _Lp>;
1339
      friend class enable_shared_from_this<_Tp>;
1340
 
1341
      _Tp*               _M_ptr;         // Contained pointer.
1342
      __weak_count<_Lp>  _M_refcount;    // Reference counter.
1343
    };
1344
 
1345
  // 20.7.2.3.6 weak_ptr specialized algorithms.
1346
  template<typename _Tp, _Lock_policy _Lp>
1347
    inline void
1348
    swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept
1349
    { __a.swap(__b); }
1350
 
1351
  template<typename _Tp, typename _Tp1>
1352
    struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool>
1353
    {
1354
      bool
1355
      operator()(const _Tp& __lhs, const _Tp& __rhs) const
1356
      { return __lhs.owner_before(__rhs); }
1357
 
1358
      bool
1359
      operator()(const _Tp& __lhs, const _Tp1& __rhs) const
1360
      { return __lhs.owner_before(__rhs); }
1361
 
1362
      bool
1363
      operator()(const _Tp1& __lhs, const _Tp& __rhs) const
1364
      { return __lhs.owner_before(__rhs); }
1365
    };
1366
 
1367
  template<typename _Tp, _Lock_policy _Lp>
1368
    struct owner_less<__shared_ptr<_Tp, _Lp>>
1369
    : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>>
1370
    { };
1371
 
1372
  template<typename _Tp, _Lock_policy _Lp>
1373
    struct owner_less<__weak_ptr<_Tp, _Lp>>
1374
    : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>>
1375
    { };
1376
 
1377
 
1378
  template<typename _Tp, _Lock_policy _Lp>
1379
    class __enable_shared_from_this
1380
    {
1381
    protected:
1382
      constexpr __enable_shared_from_this() noexcept { }
1383
 
1384
      __enable_shared_from_this(const __enable_shared_from_this&) noexcept { }
1385
 
1386
      __enable_shared_from_this&
1387
      operator=(const __enable_shared_from_this&) noexcept
1388
      { return *this; }
1389
 
1390
      ~__enable_shared_from_this() { }
1391
 
1392
    public:
1393
      __shared_ptr<_Tp, _Lp>
1394
      shared_from_this()
1395
      { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); }
1396
 
1397
      __shared_ptr<const _Tp, _Lp>
1398
      shared_from_this() const
1399
      { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }
1400
 
1401
    private:
1402
      template<typename _Tp1>
1403
        void
1404
        _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
1405
        { _M_weak_this._M_assign(__p, __n); }
1406
 
1407
      template<typename _Tp1>
1408
        friend void
1409
        __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
1410
                                         const __enable_shared_from_this* __pe,
1411
                                         const _Tp1* __px) noexcept
1412
        {
1413
          if (__pe != 0)
1414
            __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
1415
        }
1416
 
1417
      mutable __weak_ptr<_Tp, _Lp>  _M_weak_this;
1418
    };
1419
 
1420
 
1421
  template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
1422
    inline __shared_ptr<_Tp, _Lp>
1423
    __allocate_shared(const _Alloc& __a, _Args&&... __args)
1424
    {
1425
      return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a,
1426
                                    std::forward<_Args>(__args)...);
1427
    }
1428
 
1429
  template<typename _Tp, _Lock_policy _Lp, typename... _Args>
1430
    inline __shared_ptr<_Tp, _Lp>
1431
    __make_shared(_Args&&... __args)
1432
    {
1433
      typedef typename std::remove_const<_Tp>::type _Tp_nc;
1434
      return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(),
1435
                                              std::forward<_Args>(__args)...);
1436
    }
1437
 
1438
  /// std::hash specialization for __shared_ptr.
1439
  template<typename _Tp, _Lock_policy _Lp>
1440
    struct hash<__shared_ptr<_Tp, _Lp>>
1441
    : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>>
1442
    {
1443
      size_t
1444
      operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept
1445
      { return std::hash<_Tp*>()(__s.get()); }
1446
    };
1447
 
1448
_GLIBCXX_END_NAMESPACE_VERSION
1449
} // namespace
1450
 
1451
#endif // _SHARED_PTR_BASE_H

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.