transaction.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <__config>
  11. #include <__utility/exchange.h>
  12. #include <__utility/move.h>
  13. #include <type_traits>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. // __transaction is a helper class for writing code with the strong exception guarantee.
  19. //
  20. // When writing code that can throw an exception, one can store rollback instructions in a
  21. // transaction so that if an exception is thrown at any point during the lifetime of the
  22. // transaction, it will be rolled back automatically. When the transaction is done, one
  23. // must mark it as being complete so it isn't rolled back when the transaction is destroyed.
  24. //
  25. // Transactions are not default constructible, they can't be copied or assigned to, but
  26. // they can be moved around for convenience.
  27. //
  28. // __transaction can help greatly simplify code that would normally be cluttered by
  29. // `#if _LIBCPP_NO_EXCEPTIONS`. For example:
  30. //
  31. // template <class Iterator, class Size, class OutputIterator>
  32. // Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) {
  33. // typedef typename iterator_traits<Iterator>::value_type value_type;
  34. // __transaction transaction([start=out, &out] {
  35. // std::destroy(start, out);
  36. // });
  37. //
  38. // for (; n > 0; ++iter, ++out, --n) {
  39. // ::new ((void*)std::addressof(*out)) value_type(*iter);
  40. // }
  41. // transaction.__complete();
  42. // return out;
  43. // }
  44. //
  45. template <class _Rollback>
  46. struct __transaction {
  47. __transaction() = delete;
  48. _LIBCPP_HIDE_FROM_ABI
  49. _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit __transaction(_Rollback __rollback)
  50. : __rollback_(_VSTD::move(__rollback))
  51. , __completed_(false)
  52. { }
  53. _LIBCPP_HIDE_FROM_ABI
  54. _LIBCPP_CONSTEXPR_AFTER_CXX17 __transaction(__transaction&& __other)
  55. _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
  56. : __rollback_(_VSTD::move(__other.__rollback_))
  57. , __completed_(__other.__completed_)
  58. {
  59. __other.__completed_ = true;
  60. }
  61. __transaction(__transaction const&) = delete;
  62. __transaction& operator=(__transaction const&) = delete;
  63. __transaction& operator=(__transaction&&) = delete;
  64. _LIBCPP_HIDE_FROM_ABI
  65. _LIBCPP_CONSTEXPR_AFTER_CXX17 void __complete() _NOEXCEPT {
  66. __completed_ = true;
  67. }
  68. _LIBCPP_HIDE_FROM_ABI
  69. #if !defined(_LIBCPP_COMPILER_MSVC)
  70. _LIBCPP_CONSTEXPR_AFTER_CXX17
  71. #endif
  72. ~__transaction() {
  73. if (!__completed_)
  74. __rollback_();
  75. }
  76. private:
  77. _Rollback __rollback_;
  78. bool __completed_;
  79. };
  80. _LIBCPP_END_NAMESPACE_STD
  81. #endif // _LIBCPP___UTILITY_TRANSACTION_H