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___PSTL_HANDLE_EXCEPTION_H
10#define _LIBCPP___PSTL_HANDLE_EXCEPTION_H
11
12#include <__config>
13#include <__new/exceptions.h>
14#include <__optional/comparison.h>
15#include <__optional/nullopt_t.h>
16#include <__optional/optional.h>
17#include <__utility/forward.h>
18#include <__utility/move.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27#if _LIBCPP_STD_VER >= 17
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30namespace __pstl {
31
32template <class _BackendFunction, class... _Args>
33_LIBCPP_HIDE_FROM_ABI auto __handle_exception_impl(_Args&&... __args) noexcept {
34 return _BackendFunction{}(std::forward<_Args>(__args)...);
35}
36
37// This function is used to call a backend PSTL algorithm from a frontend algorithm.
38//
39// All PSTL backend algorithms return an optional denoting whether there was an
40// "infrastructure"-level failure (aka failure to allocate). This function takes
41// care of unwrapping that and throwing `bad_alloc()` in case there was a problem
42// in the underlying implementation.
43//
44// We must also be careful not to call any user code that could throw an exception
45// (such as moving or copying iterators) in here since that should terminate the
46// program, which is why we delegate to a noexcept helper below.
47template <class _BackendFunction, class... _Args>
48_LIBCPP_HIDE_FROM_ABI auto __handle_exception(_Args&&... __args) {
49 auto __result = __pstl::__handle_exception_impl<_BackendFunction>(std::forward<_Args>(__args)...);
50 if (__result == nullopt)
51 std::__throw_bad_alloc();
52 else
53 return std::move(*__result);
54}
55
56} // namespace __pstl
57_LIBCPP_END_NAMESPACE_STD
58
59#endif // _LIBCPP_STD_VER >= 17
60
61_LIBCPP_POP_MACROS
62
63#endif // _LIBCPP___PSTL_HANDLE_EXCEPTION_H
64