62 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___PSTL_HANDLE_EXCEPTION_H10#define _LIBCPP___PSTL_HANDLE_EXCEPTION_H11 12#include <__config>13#include <__new/exceptions.h>14#include <__utility/forward.h>15#include <__utility/move.h>16#include <optional>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif21 22_LIBCPP_PUSH_MACROS23#include <__undef_macros>24 25#if _LIBCPP_STD_VER >= 1726 27_LIBCPP_BEGIN_NAMESPACE_STD28namespace __pstl {29 30template <class _BackendFunction, class... _Args>31_LIBCPP_HIDE_FROM_ABI auto __handle_exception_impl(_Args&&... __args) noexcept {32 return _BackendFunction{}(std::forward<_Args>(__args)...);33}34 35// This function is used to call a backend PSTL algorithm from a frontend algorithm.36//37// All PSTL backend algorithms return an optional denoting whether there was an38// "infrastructure"-level failure (aka failure to allocate). This function takes39// care of unwrapping that and throwing `bad_alloc()` in case there was a problem40// in the underlying implementation.41//42// We must also be careful not to call any user code that could throw an exception43// (such as moving or copying iterators) in here since that should terminate the44// program, which is why we delegate to a noexcept helper below.45template <class _BackendFunction, class... _Args>46_LIBCPP_HIDE_FROM_ABI auto __handle_exception(_Args&&... __args) {47 auto __result = __pstl::__handle_exception_impl<_BackendFunction>(std::forward<_Args>(__args)...);48 if (__result == nullopt)49 std::__throw_bad_alloc();50 else51 return std::move(*__result);52}53 54} // namespace __pstl55_LIBCPP_END_NAMESPACE_STD56 57#endif // _LIBCPP_STD_VER >= 1758 59_LIBCPP_POP_MACROS60 61#endif // _LIBCPP___PSTL_HANDLE_EXCEPTION_H62