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