readable_traits.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef _LIBCPP___ITERATOR_READABLE_TRAITS_H
  10. #define _LIBCPP___ITERATOR_READABLE_TRAITS_H
  11. #include <__concepts/same_as.h>
  12. #include <__config>
  13. #include <__type_traits/conditional.h>
  14. #include <__type_traits/is_array.h>
  15. #include <__type_traits/is_object.h>
  16. #include <__type_traits/is_primary_template.h>
  17. #include <__type_traits/remove_cv.h>
  18. #include <__type_traits/remove_cvref.h>
  19. #include <__type_traits/remove_extent.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. #if _LIBCPP_STD_VER > 17
  25. // [readable.traits]
  26. template<class> struct __cond_value_type {};
  27. template<class _Tp>
  28. requires is_object_v<_Tp>
  29. struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
  30. template<class _Tp>
  31. concept __has_member_value_type = requires { typename _Tp::value_type; };
  32. template<class _Tp>
  33. concept __has_member_element_type = requires { typename _Tp::element_type; };
  34. template<class> struct indirectly_readable_traits {};
  35. template<class _Ip>
  36. requires is_array_v<_Ip>
  37. struct indirectly_readable_traits<_Ip> {
  38. using value_type = remove_cv_t<remove_extent_t<_Ip>>;
  39. };
  40. template<class _Ip>
  41. struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
  42. template<class _Tp>
  43. struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
  44. template<__has_member_value_type _Tp>
  45. struct indirectly_readable_traits<_Tp>
  46. : __cond_value_type<typename _Tp::value_type> {};
  47. template<__has_member_element_type _Tp>
  48. struct indirectly_readable_traits<_Tp>
  49. : __cond_value_type<typename _Tp::element_type> {};
  50. template<__has_member_value_type _Tp>
  51. requires __has_member_element_type<_Tp>
  52. struct indirectly_readable_traits<_Tp> {};
  53. template<__has_member_value_type _Tp>
  54. requires __has_member_element_type<_Tp> &&
  55. same_as<remove_cv_t<typename _Tp::element_type>,
  56. remove_cv_t<typename _Tp::value_type>>
  57. struct indirectly_readable_traits<_Tp>
  58. : __cond_value_type<typename _Tp::value_type> {};
  59. template <class>
  60. struct iterator_traits;
  61. // Let `RI` be `remove_cvref_t<I>`. The type `iter_value_t<I>` denotes
  62. // `indirectly_readable_traits<RI>::value_type` if `iterator_traits<RI>` names a specialization
  63. // generated from the primary template, and `iterator_traits<RI>::value_type` otherwise.
  64. template <class _Ip>
  65. using iter_value_t = typename conditional_t<__is_primary_template<iterator_traits<remove_cvref_t<_Ip> > >::value,
  66. indirectly_readable_traits<remove_cvref_t<_Ip> >,
  67. iterator_traits<remove_cvref_t<_Ip> > >::value_type;
  68. #endif // _LIBCPP_STD_VER > 17
  69. _LIBCPP_END_NAMESPACE_STD
  70. #endif // _LIBCPP___ITERATOR_READABLE_TRAITS_H