exception_guard.h 5.7 KB

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