1*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===// 2*0fca6ea1SDimitry Andric // 3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0fca6ea1SDimitry Andric // 7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===// 8*0fca6ea1SDimitry Andric 9*0fca6ea1SDimitry Andric #ifndef _LIBCPP___PSTL_HANDLE_EXCEPTION_H 10*0fca6ea1SDimitry Andric #define _LIBCPP___PSTL_HANDLE_EXCEPTION_H 11*0fca6ea1SDimitry Andric 12*0fca6ea1SDimitry Andric #include <__config> 13*0fca6ea1SDimitry Andric #include <__utility/forward.h> 14*0fca6ea1SDimitry Andric #include <__utility/move.h> 15*0fca6ea1SDimitry Andric #include <new> // __throw_bad_alloc 16*0fca6ea1SDimitry Andric #include <optional> 17*0fca6ea1SDimitry Andric 18*0fca6ea1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19*0fca6ea1SDimitry Andric # pragma GCC system_header 20*0fca6ea1SDimitry Andric #endif 21*0fca6ea1SDimitry Andric 22*0fca6ea1SDimitry Andric _LIBCPP_PUSH_MACROS 23*0fca6ea1SDimitry Andric #include <__undef_macros> 24*0fca6ea1SDimitry Andric 25*0fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 26*0fca6ea1SDimitry Andric namespace __pstl { 27*0fca6ea1SDimitry Andric 28*0fca6ea1SDimitry Andric template <class _BackendFunction, class... _Args> 29*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI auto __handle_exception_impl(_Args&&... __args) noexcept { 30*0fca6ea1SDimitry Andric return _BackendFunction{}(std::forward<_Args>(__args)...); 31*0fca6ea1SDimitry Andric } 32*0fca6ea1SDimitry Andric 33*0fca6ea1SDimitry Andric // This function is used to call a backend PSTL algorithm from a frontend algorithm. 34*0fca6ea1SDimitry Andric // 35*0fca6ea1SDimitry Andric // All PSTL backend algorithms return an optional denoting whether there was an 36*0fca6ea1SDimitry Andric // "infrastructure"-level failure (aka failure to allocate). This function takes 37*0fca6ea1SDimitry Andric // care of unwrapping that and throwing `bad_alloc()` in case there was a problem 38*0fca6ea1SDimitry Andric // in the underlying implementation. 39*0fca6ea1SDimitry Andric // 40*0fca6ea1SDimitry Andric // We must also be careful not to call any user code that could throw an exception 41*0fca6ea1SDimitry Andric // (such as moving or copying iterators) in here since that should terminate the 42*0fca6ea1SDimitry Andric // program, which is why we delegate to a noexcept helper below. 43*0fca6ea1SDimitry Andric template <class _BackendFunction, class... _Args> 44*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI auto __handle_exception(_Args&&... __args) { 45*0fca6ea1SDimitry Andric auto __result = __pstl::__handle_exception_impl<_BackendFunction>(std::forward<_Args>(__args)...); 46*0fca6ea1SDimitry Andric if (__result == nullopt) 47*0fca6ea1SDimitry Andric std::__throw_bad_alloc(); 48*0fca6ea1SDimitry Andric else 49*0fca6ea1SDimitry Andric return std::move(*__result); 50*0fca6ea1SDimitry Andric } 51*0fca6ea1SDimitry Andric 52*0fca6ea1SDimitry Andric } // namespace __pstl 53*0fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 54*0fca6ea1SDimitry Andric 55*0fca6ea1SDimitry Andric _LIBCPP_POP_MACROS 56*0fca6ea1SDimitry Andric 57*0fca6ea1SDimitry Andric #endif // _LIBCPP___PSTL_HANDLE_EXCEPTION_H 58