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, class _Ret, class _WithKey, class _WithoutKey, class... _Args>
31_LIBCPP_HIDE_FROM_ABI _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 class _Ret,
38 class _WithKey,
39 class _WithoutKey,
40 class _Arg,
41 __enable_if_t<is_same<_KeyT, __remove_const_ref_t<_Arg> >::value, int> = 0>
42_LIBCPP_HIDE_FROM_ABI _Ret
43__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg&& __arg) {
44 return __with_key(__arg, std::forward<_Arg>(__arg));
45}
46
47template <class _KeyT,
48 class _Ret,
49 class _WithKey,
50 class _WithoutKey,
51 class _Arg,
52 __enable_if_t<__is_pair_v<__remove_const_ref_t<_Arg> > &&
53 is_same<__remove_const_t<typename __remove_const_ref_t<_Arg>::first_type>, _KeyT>::value,
54 int> = 0>
55_LIBCPP_HIDE_FROM_ABI _Ret
56__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg&& __arg) {
57 return __with_key(__arg.first, std::forward<_Arg>(__arg));
58}
59
60template <class _KeyT,
61 class _Ret,
62 class _WithKey,
63 class _WithoutKey,
64 class _Arg1,
65 class _Arg2,
66 __enable_if_t<is_same<_KeyT, __remove_const_ref_t<_Arg1> >::value, int> = 0>
67_LIBCPP_HIDE_FROM_ABI _Ret
68__try_key_extraction_impl(__priority_tag<1>, _WithKey __with_key, _WithoutKey, _Arg1&& __arg1, _Arg2&& __arg2) {
69 return __with_key(__arg1, std::forward<_Arg1>(__arg1), std::forward<_Arg2>(__arg2));
70}
71
72#ifndef _LIBCPP_CXX03_LANG
73template <class _KeyT,
74 class _Ret,
75 class _WithKey,
76 class _WithoutKey,
77 class _PiecewiseConstruct,
78 class _Tuple1,
79 class _Tuple2,
80 __enable_if_t<is_same<__remove_const_ref_t<_PiecewiseConstruct>, piecewise_construct_t>::value &&
81 __is_tuple_v<_Tuple1> && tuple_size<_Tuple1>::value == 1 &&
82 is_same<__remove_const_ref_t<typename tuple_element<0, _Tuple1>::type>, _KeyT>::value,
83 int> = 0>
84_LIBCPP_HIDE_FROM_ABI _Ret __try_key_extraction_impl(
85 __priority_tag<1>,
86 _WithKey __with_key,
87 _WithoutKey,
88 _PiecewiseConstruct&& __pc,
89 _Tuple1&& __tuple1,
90 _Tuple2&& __tuple2) {
91 return __with_key(
92 std::get<0>(__tuple1),
93 std::forward<_PiecewiseConstruct>(__pc),
94 std::forward<_Tuple1>(__tuple1),
95 std::forward<_Tuple2>(__tuple2));
96}
97#endif // _LIBCPP_CXX03_LANG
98
99// This function tries extracting the given _KeyT from _Args...
100// If it succeeds to extract the key, it calls the `__with_key` function with the extracted key and all of the
101// arguments. Otherwise it calls the `__without_key` function with all of the arguments.
102//
103// Both `__with_key` and `__without_key` must take all arguments by reference.
104template <class _KeyT, class _WithKey, class _WithoutKey, class... _Args>
105_LIBCPP_HIDE_FROM_ABI decltype(std::declval<_WithoutKey>()(std::declval<_Args>()...))
106__try_key_extraction(_WithKey __with_key, _WithoutKey __without_key, _Args&&... __args) {
107 using _Ret = decltype(__without_key(std::forward<_Args>(__args)...));
108 return std::__try_key_extraction_impl<_KeyT, _Ret>(
109 __priority_tag<1>(), __with_key, __without_key, std::forward<_Args>(__args)...);
110}
111
112_LIBCPP_END_NAMESPACE_STD
113
114#endif // _LIBCPP___UTILITY_TRY_EXTRACT_KEY_H
115