| 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___CXX03___UTILITY_TRANSACTION_H |
| 10 |
#define _LIBCPP___CXX03___UTILITY_TRANSACTION_H |
| 11 |
|
| 12 |
#include <__cxx03/__assert> |
| 13 |
#include <__cxx03/__config> |
| 14 |
#include <__cxx03/__type_traits/is_nothrow_constructible.h> |
| 15 |
#include <__cxx03/__utility/move.h> |
| 16 |
|
| 17 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 18 |
# pragma GCC system_header |
| 19 |
#endif |
| 20 |
|
| 21 |
_LIBCPP_PUSH_MACROS |
| 22 |
#include <__cxx03/__undef_macros> |
| 23 |
|
| 24 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 25 |
|
| 26 |
// __exception_guard is a helper class for writing code with the strong exception guarantee. |
| 27 |
// |
| 28 |
// When writing code that can throw an exception, one can store rollback instructions in an |
| 29 |
// exception guard so that if an exception is thrown at any point during the lifetime of the |
| 30 |
// exception guard, it will be rolled back automatically. When the exception guard is done, one |
| 31 |
// must mark it as being complete so it isn't rolled back when the exception guard is destroyed. |
| 32 |
// |
| 33 |
// Exception guards are not default constructible, they can't be copied or assigned to, but |
| 34 |
// they can be moved around for convenience. |
| 35 |
// |
| 36 |
// __exception_guard is a no-op in -fno-exceptions mode to produce better code-gen. This means |
| 37 |
// that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup |
| 38 |
// code with exceptions disabled, so even if we wanted to provide the strong exception guarantees |
| 39 |
// we couldn't. This is also only relevant for constructs with a stack of |
| 40 |
// -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where |
| 41 |
// exceptions are disabled. While -fexceptions > -fno-exceptions is quite common |
| 42 |
// (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot |
| 43 |
// less common, especially one that tries to catch an exception through -fno-exceptions code. |
| 44 |
// |
| 45 |
// __exception_guard can help greatly simplify code that would normally be cluttered by |
| 46 |
// `#if _LIBCPP_HAS_NO_EXCEPTIONS`. For example: |
| 47 |
// |
| 48 |
// template <class Iterator, class Size, class OutputIterator> |
| 49 |
// Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) { |
| 50 |
// typedef typename iterator_traits<Iterator>::value_type value_type; |
| 51 |
// __exception_guard guard([start=out, &out] { |
| 52 |
// std::destroy(start, out); |
| 53 |
// }); |
| 54 |
// |
| 55 |
// for (; n > 0; ++iter, ++out, --n) { |
| 56 |
// ::new ((void*)std::addressof(*out)) value_type(*iter); |
| 57 |
// } |
| 58 |
// guard.__complete(); |
| 59 |
// return out; |
| 60 |
// } |
| 61 |
// |
| 62 |
|
| 63 |
template <class _Rollback> |
| 64 |
struct __exception_guard_exceptions { |
| 65 |
__exception_guard_exceptions() = delete; |
| 66 |
|
| 67 |
_LIBCPP_HIDE_FROM_ABI explicit __exception_guard_exceptions(_Rollback __rollback) |
| 68 |
: __rollback_(std::move(__rollback)), __completed_(false) {} |
| 69 |
|
| 70 |
_LIBCPP_HIDE_FROM_ABI __exception_guard_exceptions(__exception_guard_exceptions&& __other) |
| 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 |
|
| 79 |
_LIBCPP_HIDE_FROM_ABI void __complete() _NOEXCEPT { __completed_ = true; } |
| 80 |
|
| 81 |
_LIBCPP_HIDE_FROM_ABI ~__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_NODEBUG explicit __exception_guard_noexceptions(_Rollback) {} |
| 97 |
|
| 98 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NODEBUG __exception_guard_noexceptions(__exception_guard_noexceptions&& __other) |
| 99 |
: __completed_(__other.__completed_) { |
| 100 |
__other.__completed_ = true; |
| 101 |
} |
| 102 |
|
| 103 |
__exception_guard_noexceptions(__exception_guard_noexceptions const&) = delete; |
| 104 |
__exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete; |
| 105 |
__exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&) = delete; |
| 106 |
|
| 107 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NODEBUG void __complete() _NOEXCEPT { __completed_ = true; } |
| 108 |
|
| 109 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NODEBUG ~__exception_guard_noexceptions() { |
| 110 |
_LIBCPP_ASSERT_INTERNAL(__completed_, "__exception_guard not completed with exceptions disabled"); |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
bool __completed_ = false; |
| 115 |
}; |
| 116 |
|
| 117 |
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions); |
| 118 |
|
| 119 |
#ifdef _LIBCPP_HAS_NO_EXCEPTIONS |
| 120 |
template <class _Rollback> |
| 121 |
using __exception_guard = __exception_guard_noexceptions<_Rollback>; |
| 122 |
#else |
| 123 |
template <class _Rollback> |
| 124 |
using __exception_guard = __exception_guard_exceptions<_Rollback>; |
| 125 |
#endif |
| 126 |
|
| 127 |
template <class _Rollback> |
| 128 |
_LIBCPP_HIDE_FROM_ABI __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) { |
| 129 |
return __exception_guard<_Rollback>(std::move(__rollback)); |
| 130 |
} |
| 131 |
|
| 132 |
_LIBCPP_END_NAMESPACE_STD |
| 133 |
|
| 134 |
_LIBCPP_POP_MACROS |
| 135 |
|
| 136 |
#endif // _LIBCPP___CXX03___UTILITY_TRANSACTION_H |