54
54
#include < __type_traits/remove_extent.h>
55
55
#include < __type_traits/remove_reference.h>
56
56
#include < __utility/declval.h>
57
+ #include < __utility/exception_guard.h>
57
58
#include < __utility/forward.h>
58
59
#include < __utility/move.h>
59
60
#include < __utility/swap.h>
@@ -352,52 +353,38 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
352
353
353
354
template <class _Yp , class _Dp , __enable_if_t <__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int > = 0 >
354
355
_LIBCPP_HIDE_FROM_ABI shared_ptr (_Yp* __p, _Dp __d) : __ptr_(__p) {
355
- #if _LIBCPP_HAS_EXCEPTIONS
356
- try {
357
- #endif // _LIBCPP_HAS_EXCEPTIONS
358
- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
359
- typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
356
+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
357
+ typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
358
+ typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
360
359
#ifndef _LIBCPP_CXX03_LANG
361
- __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
360
+ __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
362
361
#else
363
362
__cntrl_ = new _CntrlBlk (__p, __d, _AllocT ());
364
363
#endif // not _LIBCPP_CXX03_LANG
365
- __enable_weak_this (__p, __p);
366
- #if _LIBCPP_HAS_EXCEPTIONS
367
- } catch (...) {
368
- __d (__p);
369
- throw ;
370
- }
371
- #endif // _LIBCPP_HAS_EXCEPTIONS
364
+ __enable_weak_this (__p, __p);
365
+ __guard.__complete ();
372
366
}
373
367
374
368
template <class _Yp ,
375
369
class _Dp ,
376
370
class _Alloc ,
377
371
__enable_if_t <__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int > = 0 >
378
372
_LIBCPP_HIDE_FROM_ABI shared_ptr (_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
379
- #if _LIBCPP_HAS_EXCEPTIONS
380
- try {
381
- #endif // _LIBCPP_HAS_EXCEPTIONS
382
- typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
383
- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
384
- typedef __allocator_destructor<_A2> _D2;
385
- _A2 __a2 (__a);
386
- unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
387
- ::new ((void *)std::addressof (*__hold2.get ()))
373
+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
374
+ typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
375
+ typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
376
+ typedef __allocator_destructor<_A2> _D2;
377
+ _A2 __a2 (__a);
378
+ unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
379
+ ::new ((void *)std::addressof (*__hold2.get ()))
388
380
#ifndef _LIBCPP_CXX03_LANG
389
- _CntrlBlk (__p, std::move (__d), __a);
381
+ _CntrlBlk (__p, std::move (__d), __a);
390
382
#else
391
383
_CntrlBlk (__p, __d, __a);
392
384
#endif // not _LIBCPP_CXX03_LANG
393
- __cntrl_ = std::addressof (*__hold2.release ());
394
- __enable_weak_this (__p, __p);
395
- #if _LIBCPP_HAS_EXCEPTIONS
396
- } catch (...) {
397
- __d (__p);
398
- throw ;
399
- }
400
- #endif // _LIBCPP_HAS_EXCEPTIONS
385
+ __cntrl_ = std::addressof (*__hold2.release ());
386
+ __enable_weak_this (__p, __p);
387
+ __guard.__complete ();
401
388
}
402
389
403
390
template <class _Dp >
@@ -406,22 +393,15 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
406
393
_Dp __d,
407
394
__enable_if_t <__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
408
395
: __ptr_(nullptr ) {
409
- #if _LIBCPP_HAS_EXCEPTIONS
410
- try {
411
- #endif // _LIBCPP_HAS_EXCEPTIONS
412
- typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
413
- typedef __shared_ptr_pointer<nullptr_t , _Dp, _AllocT> _CntrlBlk;
396
+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
397
+ typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
398
+ typedef __shared_ptr_pointer<nullptr_t , _Dp, _AllocT> _CntrlBlk;
414
399
#ifndef _LIBCPP_CXX03_LANG
415
- __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
400
+ __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
416
401
#else
417
402
__cntrl_ = new _CntrlBlk (__p, __d, _AllocT ());
418
403
#endif // not _LIBCPP_CXX03_LANG
419
- #if _LIBCPP_HAS_EXCEPTIONS
420
- } catch (...) {
421
- __d (__p);
422
- throw ;
423
- }
424
- #endif // _LIBCPP_HAS_EXCEPTIONS
404
+ __guard.__complete ();
425
405
}
426
406
427
407
template <class _Dp , class _Alloc >
@@ -431,27 +411,20 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
431
411
_Alloc __a,
432
412
__enable_if_t <__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
433
413
: __ptr_(nullptr ) {
434
- #if _LIBCPP_HAS_EXCEPTIONS
435
- try {
436
- #endif // _LIBCPP_HAS_EXCEPTIONS
437
- typedef __shared_ptr_pointer<nullptr_t , _Dp, _Alloc> _CntrlBlk;
438
- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
439
- typedef __allocator_destructor<_A2> _D2;
440
- _A2 __a2 (__a);
441
- unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
442
- ::new ((void *)std::addressof (*__hold2.get ()))
414
+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
415
+ typedef __shared_ptr_pointer<nullptr_t , _Dp, _Alloc> _CntrlBlk;
416
+ typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
417
+ typedef __allocator_destructor<_A2> _D2;
418
+ _A2 __a2 (__a);
419
+ unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
420
+ ::new ((void *)std::addressof (*__hold2.get ()))
443
421
#ifndef _LIBCPP_CXX03_LANG
444
- _CntrlBlk (__p, std::move (__d), __a);
422
+ _CntrlBlk (__p, std::move (__d), __a);
445
423
#else
446
424
_CntrlBlk (__p, __d, __a);
447
425
#endif // not _LIBCPP_CXX03_LANG
448
- __cntrl_ = std::addressof (*__hold2.release ());
449
- #if _LIBCPP_HAS_EXCEPTIONS
450
- } catch (...) {
451
- __d (__p);
452
- throw ;
453
- }
454
- #endif // _LIBCPP_HAS_EXCEPTIONS
426
+ __cntrl_ = std::addressof (*__hold2.release ());
427
+ __guard.__complete ();
455
428
}
456
429
457
430
template <class _Yp >
0 commit comments