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