xref: /freebsd-src/contrib/llvm-project/libcxx/include/__utility/exception_guard.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
2*bdd1243dSDimitry Andric //
3*bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bdd1243dSDimitry Andric //
7*bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8*bdd1243dSDimitry Andric 
9*bdd1243dSDimitry Andric #ifndef _LIBCPP___UTILITY_TRANSACTION_H
10*bdd1243dSDimitry Andric #define _LIBCPP___UTILITY_TRANSACTION_H
11*bdd1243dSDimitry Andric 
12*bdd1243dSDimitry Andric #include <__assert>
13*bdd1243dSDimitry Andric #include <__config>
14*bdd1243dSDimitry Andric #include <__type_traits/is_nothrow_move_constructible.h>
15*bdd1243dSDimitry Andric #include <__utility/exchange.h>
16*bdd1243dSDimitry Andric #include <__utility/move.h>
17*bdd1243dSDimitry Andric 
18*bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19*bdd1243dSDimitry Andric #  pragma GCC system_header
20*bdd1243dSDimitry Andric #endif
21*bdd1243dSDimitry Andric 
22*bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
23*bdd1243dSDimitry Andric 
24*bdd1243dSDimitry Andric // __exception_guard is a helper class for writing code with the strong exception guarantee.
25*bdd1243dSDimitry Andric //
26*bdd1243dSDimitry Andric // When writing code that can throw an exception, one can store rollback instructions in an
27*bdd1243dSDimitry Andric // exception guard so that if an exception is thrown at any point during the lifetime of the
28*bdd1243dSDimitry Andric // exception guard, it will be rolled back automatically. When the exception guard is done, one
29*bdd1243dSDimitry Andric // must mark it as being complete so it isn't rolled back when the exception guard is destroyed.
30*bdd1243dSDimitry Andric //
31*bdd1243dSDimitry Andric // Exception guards are not default constructible, they can't be copied or assigned to, but
32*bdd1243dSDimitry Andric // they can be moved around for convenience.
33*bdd1243dSDimitry Andric //
34*bdd1243dSDimitry Andric // __exception_guard is a no-op in -fno-exceptions mode to produce better code-gen. This means
35*bdd1243dSDimitry Andric // that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup
36*bdd1243dSDimitry Andric // code with exceptions disabled, so even if we wanted to provide the strong exception guarantees
37*bdd1243dSDimitry Andric // we couldn't. This is also only relevant for constructs with a stack of
38*bdd1243dSDimitry Andric // -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where
39*bdd1243dSDimitry Andric // exceptions are disabled. While -fexceptions > -fno-exceptions is quite common
40*bdd1243dSDimitry Andric // (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot
41*bdd1243dSDimitry Andric // less common, especially one that tries to catch an exception through -fno-exceptions code.
42*bdd1243dSDimitry Andric //
43*bdd1243dSDimitry Andric // __exception_guard can help greatly simplify code that would normally be cluttered by
44*bdd1243dSDimitry Andric // `#if _LIBCPP_NO_EXCEPTIONS`. For example:
45*bdd1243dSDimitry Andric //
46*bdd1243dSDimitry Andric //    template <class Iterator, class Size, class OutputIterator>
47*bdd1243dSDimitry Andric //    Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) {
48*bdd1243dSDimitry Andric //        typedef typename iterator_traits<Iterator>::value_type value_type;
49*bdd1243dSDimitry Andric //        __exception_guard guard([start=out, &out] {
50*bdd1243dSDimitry Andric //            std::destroy(start, out);
51*bdd1243dSDimitry Andric //        });
52*bdd1243dSDimitry Andric //
53*bdd1243dSDimitry Andric //        for (; n > 0; ++iter, ++out, --n) {
54*bdd1243dSDimitry Andric //            ::new ((void*)std::addressof(*out)) value_type(*iter);
55*bdd1243dSDimitry Andric //        }
56*bdd1243dSDimitry Andric //        guard.__complete();
57*bdd1243dSDimitry Andric //        return out;
58*bdd1243dSDimitry Andric //    }
59*bdd1243dSDimitry Andric //
60*bdd1243dSDimitry Andric 
61*bdd1243dSDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
62*bdd1243dSDimitry Andric template <class _Rollback>
63*bdd1243dSDimitry Andric struct __exception_guard {
64*bdd1243dSDimitry Andric   __exception_guard() = delete;
65*bdd1243dSDimitry Andric 
66*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard(_Rollback __rollback)
67*bdd1243dSDimitry Andric       : __rollback_(std::move(__rollback)), __completed_(false) {}
68*bdd1243dSDimitry Andric 
69*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __exception_guard(__exception_guard&& __other)
70*bdd1243dSDimitry Andric       _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
71*bdd1243dSDimitry Andric       : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {
72*bdd1243dSDimitry Andric     __other.__completed_ = true;
73*bdd1243dSDimitry Andric   }
74*bdd1243dSDimitry Andric 
75*bdd1243dSDimitry Andric   __exception_guard(__exception_guard const&)            = delete;
76*bdd1243dSDimitry Andric   __exception_guard& operator=(__exception_guard const&) = delete;
77*bdd1243dSDimitry Andric   __exception_guard& operator=(__exception_guard&&)      = delete;
78*bdd1243dSDimitry Andric 
79*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }
80*bdd1243dSDimitry Andric 
81*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard() {
82*bdd1243dSDimitry Andric     if (!__completed_)
83*bdd1243dSDimitry Andric       __rollback_();
84*bdd1243dSDimitry Andric   }
85*bdd1243dSDimitry Andric 
86*bdd1243dSDimitry Andric private:
87*bdd1243dSDimitry Andric   _Rollback __rollback_;
88*bdd1243dSDimitry Andric   bool __completed_;
89*bdd1243dSDimitry Andric };
90*bdd1243dSDimitry Andric #else  // _LIBCPP_NO_EXCEPTIONS
91*bdd1243dSDimitry Andric template <class _Rollback>
92*bdd1243dSDimitry Andric struct __exception_guard {
93*bdd1243dSDimitry Andric   __exception_guard() = delete;
94*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard(_Rollback) {}
95*bdd1243dSDimitry Andric 
96*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG __exception_guard(__exception_guard&& __other)
97*bdd1243dSDimitry Andric       _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
98*bdd1243dSDimitry Andric       : __completed_(__other.__completed_) {
99*bdd1243dSDimitry Andric     __other.__completed_ = true;
100*bdd1243dSDimitry Andric   }
101*bdd1243dSDimitry Andric 
102*bdd1243dSDimitry Andric   __exception_guard(__exception_guard const&)            = delete;
103*bdd1243dSDimitry Andric   __exception_guard& operator=(__exception_guard const&) = delete;
104*bdd1243dSDimitry Andric   __exception_guard& operator=(__exception_guard&&)      = delete;
105*bdd1243dSDimitry Andric 
106*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT {
107*bdd1243dSDimitry Andric     __completed_ = true;
108*bdd1243dSDimitry Andric   }
109*bdd1243dSDimitry Andric 
110*bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG ~__exception_guard() {
111*bdd1243dSDimitry Andric     _LIBCPP_ASSERT(__completed_, "__exception_guard not completed with exceptions disabled");
112*bdd1243dSDimitry Andric   }
113*bdd1243dSDimitry Andric 
114*bdd1243dSDimitry Andric private:
115*bdd1243dSDimitry Andric   bool __completed_ = false;
116*bdd1243dSDimitry Andric };
117*bdd1243dSDimitry Andric #endif // _LIBCPP_NO_EXCEPTIONS
118*bdd1243dSDimitry Andric 
119*bdd1243dSDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard);
120*bdd1243dSDimitry Andric 
121*bdd1243dSDimitry Andric template <class _Rollback>
122*bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {
123*bdd1243dSDimitry Andric   return __exception_guard<_Rollback>(std::move(__rollback));
124*bdd1243dSDimitry Andric }
125*bdd1243dSDimitry Andric 
126*bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
127*bdd1243dSDimitry Andric 
128*bdd1243dSDimitry Andric #endif // _LIBCPP___UTILITY_TRANSACTION_H
129