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___UTILITY_TRY_EXTRACT_KEY_H
10#define _LIBCPP___UTILITY_TRY_EXTRACT_KEY_H
11
12#include <__config>
13#include <__fwd/pair.h>
14#include <__fwd/tuple.h>
15#include <__type_traits/enable_if.h>
16#include <__type_traits/is_same.h>
17#include <__type_traits/remove_const.h>
18#include <__type_traits/remove_const_ref.h>
19#include <__utility/declval.h>
20#include <__utility/forward.h>
21#include <__utility/piecewise_construct.h>
22#include <__utility/priority_tag.h>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25# pragma GCC system_header
26#endif
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30template <class _KeyT, bool, class _Ret, class _WithKey, class _WithoutKey, class... _Args>
31_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Ret
32__try_key_extraction_impl(__priority_tag<0>, _WithKey, _WithoutKey __without_key, _Args&&... __args) {
33 return __without_key(std::forward<_Args>(__args)...);
34}
35
36template <class _KeyT,
37 bool,
38 class _Ret,
39 class _WithKey,
40 class _WithoutKey,
41 class _Arg,
42 __enable_if_t<is_same<_KeyT, __remove_const_ref_t<_Arg> >::value, int> = 0>
43_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Ret
44__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg&& __arg) {
45 return __with_key(__arg, std::forward<_Arg>(__arg));
46}
47
48template <class _KeyT,
49 bool __is_map,
50 class _Ret,
51 class _WithKey,
52 class _WithoutKey,
53 class _Arg,
54 __enable_if_t<__is_map && __is_pair_v<__remove_const_ref_t<_Arg> > &&
55 is_same<__remove_const_t<typename __remove_const_ref_t<_Arg>::first_type>, _KeyT>::value,
56 int> = 0>
57_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Ret
58__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg&& __arg) {
59 return __with_key(__arg.first, std::forward<_Arg>(__arg));
60}
61
62template <class _KeyT,
63 bool __is_map,
64 class _Ret,
65 class _WithKey,
66 class _WithoutKey,
67 class _Arg1,
68 class _Arg2,
69 __enable_if_t<__is_map && is_same<_KeyT, __remove_const_ref_t<_Arg1> >::value, int> = 0>
70_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Ret
71__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg1&& __arg1, _Arg2&& __arg2) {
72 return __with_key(__arg1, std::forward<_Arg1>(__arg1), std::forward<_Arg2>(__arg2));
73}
74
75#ifndef _LIBCPP_CXX03_LANG
76template <class _KeyT,
77 bool __is_map,
78 class _Ret,
79 class _WithKey,
80 class _WithoutKey,
81 class _PiecewiseConstruct,
82 class _Tuple1,
83 class _Tuple2,
84 __enable_if_t<__is_map && is_same<__remove_const_ref_t<_PiecewiseConstruct>, piecewise_construct_t>::value &&
85 __is_tuple_v<_Tuple1> && tuple_size<_Tuple1>::value == 1 &&
86 is_same<__remove_const_ref_t<typename tuple_element<0, _Tuple1>::type>, _KeyT>::value,
87 int> = 0>
88_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Ret __try_key_extraction_impl(
89 __priority_tag<1>,
90 _WithKey __with_key,
91 _WithoutKey,
92 _PiecewiseConstruct&& __pc,
93 _Tuple1&& __tuple1,
94 _Tuple2&& __tuple2) {
95 return __with_key(
96 std::get<0>(__tuple1),
97 std::forward<_PiecewiseConstruct>(__pc),
98 std::forward<_Tuple1>(__tuple1),
99 std::forward<_Tuple2>(__tuple2));
100}
101#endif // _LIBCPP_CXX03_LANG
102
103// This function tries extracting the given _KeyT from _Args...
104// If it succeeds to extract the key, it calls the `__with_key` function with the extracted key and all of the
105// arguments. Otherwise it calls the `__without_key` function with all of the arguments.
106//
107// Both `__with_key` and `__without_key` must take all arguments by reference.
108template <class _KeyT, class _ValT, class _WithKey, class _WithoutKey, class... _Args>
109_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 decltype(std::declval<_WithoutKey>()(std::declval<_Args>()...))
110__try_key_extraction(_WithKey __with_key, _WithoutKey __without_key, _Args&&... __args) {
111 using _Ret = decltype(__without_key(std::forward<_Args>(__args)...));
112 return std::__try_key_extraction_impl<_KeyT, !is_same<_KeyT, _ValT>::value, _Ret>(
113 __priority_tag<1>(), __with_key, __without_key, std::forward<_Args>(__args)...);
114}
115
116_LIBCPP_END_NAMESPACE_STD
117
118#endif // _LIBCPP___UTILITY_TRY_EXTRACT_KEY_H
119