xref: /freebsd/contrib/llvm-project/libcxx/include/__pstl/dispatch.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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_DISPATCH_H
10 #define _LIBCPP___PSTL_DISPATCH_H
11 
12 #include <__config>
13 #include <__pstl/backend_fwd.h>
14 #include <__type_traits/conditional.h>
15 #include <__type_traits/enable_if.h>
16 #include <__type_traits/integral_constant.h>
17 #include <__type_traits/type_identity.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 namespace __pstl {
28 
29 template <template <class, class> class _Algorithm, class _Backend, class _ExecutionPolicy, class = void>
30 constexpr bool __is_implemented_v = false;
31 
32 template <template <class, class> class _Algorithm, class _Backend, class _ExecutionPolicy>
33 constexpr bool __is_implemented_v<_Algorithm,
34                                   _Backend,
35                                   _ExecutionPolicy,
36                                   __enable_if_t<sizeof(_Algorithm<_Backend, _ExecutionPolicy>)>> = true;
37 
38 // Helpful to provide better error messages. This will show the algorithm and the execution policy
39 // in the compiler diagnostic.
40 template <template <class, class> class _Algorithm, class _ExecutionPolicy>
41 constexpr bool __cant_find_backend_for = false;
42 
43 template <template <class, class> class _Algorithm, class _BackendConfiguration, class _ExecutionPolicy>
44 struct __find_first_implemented;
45 
46 template <template <class, class> class _Algorithm, class _ExecutionPolicy>
47 struct __find_first_implemented<_Algorithm, __backend_configuration<>, _ExecutionPolicy> {
48   static_assert(__cant_find_backend_for<_Algorithm, _ExecutionPolicy>,
49                 "Could not find a PSTL backend for the given algorithm and execution policy");
50 };
51 
52 template <template <class, class> class _Algorithm, class _B1, class... _Bn, class _ExecutionPolicy>
53 struct __find_first_implemented<_Algorithm, __backend_configuration<_B1, _Bn...>, _ExecutionPolicy>
54     : _If<__is_implemented_v<_Algorithm, _B1, _ExecutionPolicy>,
55           __type_identity<_Algorithm<_B1, _ExecutionPolicy>>,
56           __find_first_implemented<_Algorithm, __backend_configuration<_Bn...>, _ExecutionPolicy> > {};
57 
58 template <template <class, class> class _Algorithm, class _BackendConfiguration, class _ExecutionPolicy>
59 using __dispatch = typename __find_first_implemented<_Algorithm, _BackendConfiguration, _ExecutionPolicy>::type;
60 
61 } // namespace __pstl
62 _LIBCPP_END_NAMESPACE_STD
63 
64 _LIBCPP_POP_MACROS
65 
66 #endif // _LIBCPP___PSTL_DISPATCH_H
67