exception_guard.h 5.7 KB

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