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