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