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