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___TYPE_TRAITS_CAN_EXTRACT_KEY_H
10 #define _LIBCPP___TYPE_TRAITS_CAN_EXTRACT_KEY_H
11 
12 #include <__config>
13 #include <__fwd/pair.h>
14 #include <__type_traits/conditional.h>
15 #include <__type_traits/integral_constant.h>
16 #include <__type_traits/is_same.h>
17 #include <__type_traits/remove_const.h>
18 #include <__type_traits/remove_const_ref.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 // These traits are used in __tree and __hash_table
27 struct __extract_key_fail_tag {};
28 struct __extract_key_self_tag {};
29 struct __extract_key_first_tag {};
30 
31 template <class _ValTy, class _Key, class _RawValTy = __remove_const_ref_t<_ValTy> >
32 struct __can_extract_key
33     : __conditional_t<_IsSame<_RawValTy, _Key>::value, __extract_key_self_tag, __extract_key_fail_tag> {};
34 
35 template <class _Pair, class _Key, class _First, class _Second>
36 struct __can_extract_key<_Pair, _Key, pair<_First, _Second> >
37     : __conditional_t<_IsSame<__remove_const_t<_First>, _Key>::value, __extract_key_first_tag, __extract_key_fail_tag> {
38 };
39 
40 // __can_extract_map_key uses true_type/false_type instead of the tags.
41 // It returns true if _Key != _ContainerValueTy (the container is a map not a set)
42 // and _ValTy == _Key.
43 template <class _ValTy, class _Key, class _ContainerValueTy, class _RawValTy = __remove_const_ref_t<_ValTy> >
44 struct __can_extract_map_key : integral_constant<bool, _IsSame<_RawValTy, _Key>::value> {};
45 
46 // This specialization returns __extract_key_fail_tag for non-map containers
47 // because _Key == _ContainerValueTy
48 template <class _ValTy, class _Key, class _RawValTy>
49 struct __can_extract_map_key<_ValTy, _Key, _Key, _RawValTy> : false_type {};
50 
51 _LIBCPP_END_NAMESPACE_STD
52 
53 #endif // _LIBCPP___TYPE_TRAITS_CAN_EXTRACT_KEY_H
54