Path: blob/main/contrib/llvm-project/libcxx/include/__utility/exception_guard.h
35236 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___UTILITY_TRANSACTION_H9#define _LIBCPP___UTILITY_TRANSACTION_H1011#include <__assert>12#include <__config>13#include <__type_traits/is_nothrow_constructible.h>14#include <__utility/exchange.h>15#include <__utility/move.h>1617#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)18# pragma GCC system_header19#endif2021_LIBCPP_PUSH_MACROS22#include <__undef_macros>2324_LIBCPP_BEGIN_NAMESPACE_STD2526// __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 an29// exception guard so that if an exception is thrown at any point during the lifetime of the30// exception guard, it will be rolled back automatically. When the exception guard is done, one31// 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, but34// 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 means37// that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup38// code with exceptions disabled, so even if we wanted to provide the strong exception guarantees39// we couldn't. This is also only relevant for constructs with a stack of40// -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where41// exceptions are disabled. While -fexceptions > -fno-exceptions is quite common42// (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot43// 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 by46// `#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//6263template <class _Rollback>64struct __exception_guard_exceptions {65__exception_guard_exceptions() = delete;6667_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_exceptions(_Rollback __rollback)68: __rollback_(std::move(__rollback)), __completed_(false) {}6970_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX2071__exception_guard_exceptions(__exception_guard_exceptions&& __other)72_NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)73: __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) {74__other.__completed_ = true;75}7677__exception_guard_exceptions(__exception_guard_exceptions const&) = delete;78__exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete;79__exception_guard_exceptions& operator=(__exception_guard_exceptions&&) = delete;8081_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }8283_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_exceptions() {84if (!__completed_)85__rollback_();86}8788private:89_Rollback __rollback_;90bool __completed_;91};9293_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);9495template <class _Rollback>96struct __exception_guard_noexceptions {97__exception_guard_noexceptions() = delete;98_LIBCPP_HIDE_FROM_ABI99_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard_noexceptions(_Rollback) {}100101_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG102__exception_guard_noexceptions(__exception_guard_noexceptions&& __other)103_NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)104: __completed_(__other.__completed_) {105__other.__completed_ = true;106}107108__exception_guard_noexceptions(__exception_guard_noexceptions const&) = delete;109__exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete;110__exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&) = delete;111112_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT {113__completed_ = true;114}115116_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG ~__exception_guard_noexceptions() {117_LIBCPP_ASSERT_INTERNAL(__completed_, "__exception_guard not completed with exceptions disabled");118}119120private:121bool __completed_ = false;122};123124_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions);125126#ifdef _LIBCPP_HAS_NO_EXCEPTIONS127template <class _Rollback>128using __exception_guard = __exception_guard_noexceptions<_Rollback>;129#else130template <class _Rollback>131using __exception_guard = __exception_guard_exceptions<_Rollback>;132#endif133134template <class _Rollback>135_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {136return __exception_guard<_Rollback>(std::move(__rollback));137}138139_LIBCPP_END_NAMESPACE_STD140141_LIBCPP_POP_MACROS142143#endif // _LIBCPP___UTILITY_TRANSACTION_H144145146