1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ITERATOR_READABLE_TRAITS_H
11 #define _LIBCPP___ITERATOR_READABLE_TRAITS_H
12 
13 #include <__concepts/same_as.h>
14 #include <__config>
15 #include <__type_traits/conditional.h>
16 #include <__type_traits/is_array.h>
17 #include <__type_traits/is_object.h>
18 #include <__type_traits/is_primary_template.h>
19 #include <__type_traits/remove_cv.h>
20 #include <__type_traits/remove_cvref.h>
21 #include <__type_traits/remove_extent.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 #if _LIBCPP_STD_VER > 17
30 
31 // [readable.traits]
32 template<class> struct __cond_value_type {};
33 
34 template<class _Tp>
35 requires is_object_v<_Tp>
36 struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
37 
38 template<class _Tp>
39 concept __has_member_value_type = requires { typename _Tp::value_type; };
40 
41 template<class _Tp>
42 concept __has_member_element_type = requires { typename _Tp::element_type; };
43 
44 template<class> struct indirectly_readable_traits {};
45 
46 template<class _Ip>
47 requires is_array_v<_Ip>
48 struct indirectly_readable_traits<_Ip> {
49   using value_type = remove_cv_t<remove_extent_t<_Ip>>;
50 };
51 
52 template<class _Ip>
53 struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
54 
55 template<class _Tp>
56 struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
57 
58 template<__has_member_value_type _Tp>
59 struct indirectly_readable_traits<_Tp>
60   : __cond_value_type<typename _Tp::value_type> {};
61 
62 template<__has_member_element_type _Tp>
63 struct indirectly_readable_traits<_Tp>
64   : __cond_value_type<typename _Tp::element_type> {};
65 
66 template<__has_member_value_type _Tp>
67   requires __has_member_element_type<_Tp>
68 struct indirectly_readable_traits<_Tp> {};
69 
70 template<__has_member_value_type _Tp>
71   requires __has_member_element_type<_Tp> &&
72            same_as<remove_cv_t<typename _Tp::element_type>,
73                    remove_cv_t<typename _Tp::value_type>>
74 struct indirectly_readable_traits<_Tp>
75   : __cond_value_type<typename _Tp::value_type> {};
76 
77 template <class>
78 struct iterator_traits;
79 
80 // Let `RI` be `remove_cvref_t<I>`. The type `iter_value_t<I>` denotes
81 // `indirectly_readable_traits<RI>::value_type` if `iterator_traits<RI>` names a specialization
82 // generated from the primary template, and `iterator_traits<RI>::value_type` otherwise.
83 template <class _Ip>
84 using iter_value_t = typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
85                                             indirectly_readable_traits<remove_cvref_t<_Ip> >,
86                                             iterator_traits<remove_cvref_t<_Ip> > >::value_type;
87 
88 #endif // _LIBCPP_STD_VER > 17
89 
90 _LIBCPP_END_NAMESPACE_STD
91 
92 #endif // _LIBCPP___ITERATOR_READABLE_TRAITS_H
93