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