Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:06

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___UTILITY_TRANSACTION_H
0010 #define _LIBCPP___UTILITY_TRANSACTION_H
0011 
0012 #include <__assert>
0013 #include <__config>
0014 #include <__type_traits/is_nothrow_constructible.h>
0015 #include <__utility/exchange.h>
0016 #include <__utility/move.h>
0017 
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 #  pragma GCC system_header
0020 #endif
0021 
0022 _LIBCPP_PUSH_MACROS
0023 #include <__undef_macros>
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 // __exception_guard is a helper class for writing code with the strong exception guarantee.
0028 //
0029 // When writing code that can throw an exception, one can store rollback instructions in an
0030 // exception guard so that if an exception is thrown at any point during the lifetime of the
0031 // exception guard, it will be rolled back automatically. When the exception guard is done, one
0032 // must mark it as being complete so it isn't rolled back when the exception guard is destroyed.
0033 //
0034 // Exception guards are not default constructible, they can't be copied or assigned to, but
0035 // they can be moved around for convenience.
0036 //
0037 // __exception_guard is a no-op in -fno-exceptions mode to produce better code-gen. This means
0038 // that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup
0039 // code with exceptions disabled, so even if we wanted to provide the strong exception guarantees
0040 // we couldn't. This is also only relevant for constructs with a stack of
0041 // -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where
0042 // exceptions are disabled. While -fexceptions > -fno-exceptions is quite common
0043 // (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot
0044 // less common, especially one that tries to catch an exception through -fno-exceptions code.
0045 //
0046 // __exception_guard can help greatly simplify code that would normally be cluttered by
0047 // `#if _LIBCPP_HAS_EXCEPTIONS`. For example:
0048 //
0049 //    template <class Iterator, class Size, class OutputIterator>
0050 //    Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) {
0051 //        typedef typename iterator_traits<Iterator>::value_type value_type;
0052 //        __exception_guard guard([start=out, &out] {
0053 //            std::destroy(start, out);
0054 //        });
0055 //
0056 //        for (; n > 0; ++iter, ++out, --n) {
0057 //            ::new ((void*)std::addressof(*out)) value_type(*iter);
0058 //        }
0059 //        guard.__complete();
0060 //        return out;
0061 //    }
0062 //
0063 
0064 template <class _Rollback>
0065 struct __exception_guard_exceptions {
0066   __exception_guard_exceptions() = delete;
0067 
0068   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_exceptions(_Rollback __rollback)
0069       : __rollback_(std::move(__rollback)), __completed_(false) {}
0070 
0071   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0072   __exception_guard_exceptions(__exception_guard_exceptions&& __other)
0073       _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
0074       : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {
0075     __other.__completed_ = true;
0076   }
0077 
0078   __exception_guard_exceptions(__exception_guard_exceptions const&)            = delete;
0079   __exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete;
0080   __exception_guard_exceptions& operator=(__exception_guard_exceptions&&)      = delete;
0081 
0082   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }
0083 
0084   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_exceptions() {
0085     if (!__completed_)
0086       __rollback_();
0087   }
0088 
0089 private:
0090   _Rollback __rollback_;
0091   bool __completed_;
0092 };
0093 
0094 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);
0095 
0096 template <class _Rollback>
0097 struct __exception_guard_noexceptions {
0098   __exception_guard_noexceptions() = delete;
0099   _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI
0100   _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_noexceptions(_Rollback) {}
0101 
0102   _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0103   __exception_guard_noexceptions(__exception_guard_noexceptions&& __other)
0104       _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
0105       : __completed_(__other.__completed_) {
0106     __other.__completed_ = true;
0107   }
0108 
0109   __exception_guard_noexceptions(__exception_guard_noexceptions const&)            = delete;
0110   __exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete;
0111   __exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&)      = delete;
0112 
0113   _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT {
0114     __completed_ = true;
0115   }
0116 
0117   _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_noexceptions() {
0118     _LIBCPP_ASSERT_INTERNAL(__completed_, "__exception_guard not completed with exceptions disabled");
0119   }
0120 
0121 private:
0122   bool __completed_ = false;
0123 };
0124 
0125 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions);
0126 
0127 #if !_LIBCPP_HAS_EXCEPTIONS
0128 template <class _Rollback>
0129 using __exception_guard _LIBCPP_NODEBUG = __exception_guard_noexceptions<_Rollback>;
0130 #else
0131 template <class _Rollback>
0132 using __exception_guard _LIBCPP_NODEBUG = __exception_guard_exceptions<_Rollback>;
0133 #endif
0134 
0135 template <class _Rollback>
0136 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {
0137   return __exception_guard<_Rollback>(std::move(__rollback));
0138 }
0139 
0140 _LIBCPP_END_NAMESPACE_STD
0141 
0142 _LIBCPP_POP_MACROS
0143 
0144 #endif // _LIBCPP___UTILITY_TRANSACTION_H