master
  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
  9#ifndef _LIBCPP___UTILITY_EXCEPTION_GUARD_H
 10#define _LIBCPP___UTILITY_EXCEPTION_GUARD_H
 11
 12#include <__assert>
 13#include <__config>
 14#include <__type_traits/is_nothrow_constructible.h>
 15#include <__utility/move.h>
 16
 17#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 18#  pragma GCC system_header
 19#endif
 20
 21_LIBCPP_PUSH_MACROS
 22#include <__undef_macros>
 23
 24_LIBCPP_BEGIN_NAMESPACE_STD
 25
 26// __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 an
 29// exception guard so that if an exception is thrown at any point during the lifetime of the
 30// exception guard, it will be rolled back automatically. When the exception guard is done, one
 31// 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, but
 34// 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 means
 37// that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup
 38// code with exceptions disabled, so even if we wanted to provide the strong exception guarantees
 39// we couldn't. This is also only relevant for constructs with a stack of
 40// -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where
 41// exceptions are disabled. While -fexceptions > -fno-exceptions is quite common
 42// (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot
 43// 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 by
 46// `#if _LIBCPP_HAS_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//
 62
 63template <class _Rollback>
 64struct __exception_guard_exceptions {
 65  __exception_guard_exceptions() = delete;
 66
 67  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_exceptions(_Rollback __rollback)
 68      : __rollback_(std::move(__rollback)), __completed_(false) {}
 69
 70  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
 71  __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  }
 76
 77  __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;
 80
 81  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; }
 82
 83  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_exceptions() {
 84    if (!__completed_)
 85      __rollback_();
 86  }
 87
 88private:
 89  _Rollback __rollback_;
 90  bool __completed_;
 91};
 92
 93_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);
 94
 95template <class _Rollback>
 96struct __exception_guard_noexceptions {
 97  __exception_guard_noexceptions() = delete;
 98  _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI
 99  _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_noexceptions(_Rollback) {}
100
101  _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
102  __exception_guard_noexceptions(__exception_guard_noexceptions&& __other)
103      _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value)
104      : __completed_(__other.__completed_) {
105    __other.__completed_ = true;
106  }
107
108  __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;
111
112  _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT {
113    __completed_ = true;
114  }
115
116  _LIBCPP_NODEBUG _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_noexceptions() {
117    _LIBCPP_ASSERT_INTERNAL(__completed_, "__exception_guard not completed with exceptions disabled");
118  }
119
120private:
121  bool __completed_ = false;
122};
123
124_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions);
125
126#if !_LIBCPP_HAS_EXCEPTIONS
127template <class _Rollback>
128using __exception_guard _LIBCPP_NODEBUG = __exception_guard_noexceptions<_Rollback>;
129#else
130template <class _Rollback>
131using __exception_guard _LIBCPP_NODEBUG = __exception_guard_exceptions<_Rollback>;
132#endif
133
134template <class _Rollback>
135_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {
136  return __exception_guard<_Rollback>(std::move(__rollback));
137}
138
139_LIBCPP_END_NAMESPACE_STD
140
141_LIBCPP_POP_MACROS
142
143#endif // _LIBCPP___UTILITY_EXCEPTION_GUARD_H