data.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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___RANGES_DATA_H
  10. #define _LIBCPP___RANGES_DATA_H
  11. #include <__concepts/class_or_enum.h>
  12. #include <__config>
  13. #include <__iterator/concepts.h>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__memory/pointer_traits.h>
  16. #include <__ranges/access.h>
  17. #include <__utility/auto_cast.h>
  18. #include <type_traits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  24. // [range.prim.data]
  25. namespace ranges {
  26. namespace __data {
  27. template <class _Tp>
  28. concept __ptr_to_object = is_pointer_v<_Tp> && is_object_v<remove_pointer_t<_Tp>>;
  29. template <class _Tp>
  30. concept __member_data =
  31. __can_borrow<_Tp> &&
  32. __workaround_52970<_Tp> &&
  33. requires(_Tp&& __t) {
  34. { _LIBCPP_AUTO_CAST(__t.data()) } -> __ptr_to_object;
  35. };
  36. template <class _Tp>
  37. concept __ranges_begin_invocable =
  38. !__member_data<_Tp> &&
  39. __can_borrow<_Tp> &&
  40. requires(_Tp&& __t) {
  41. { ranges::begin(__t) } -> contiguous_iterator;
  42. };
  43. struct __fn {
  44. template <__member_data _Tp>
  45. _LIBCPP_HIDE_FROM_ABI
  46. constexpr auto operator()(_Tp&& __t) const
  47. noexcept(noexcept(__t.data())) {
  48. return __t.data();
  49. }
  50. template<__ranges_begin_invocable _Tp>
  51. _LIBCPP_HIDE_FROM_ABI
  52. constexpr auto operator()(_Tp&& __t) const
  53. noexcept(noexcept(std::to_address(ranges::begin(__t)))) {
  54. return std::to_address(ranges::begin(__t));
  55. }
  56. };
  57. } // namespace __data
  58. inline namespace __cpo {
  59. inline constexpr auto data = __data::__fn{};
  60. } // namespace __cpo
  61. } // namespace ranges
  62. // [range.prim.cdata]
  63. namespace ranges {
  64. namespace __cdata {
  65. struct __fn {
  66. template <class _Tp>
  67. requires is_lvalue_reference_v<_Tp&&>
  68. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  69. constexpr auto operator()(_Tp&& __t) const
  70. noexcept(noexcept(ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t))))
  71. -> decltype( ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t)))
  72. { return ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t)); }
  73. template <class _Tp>
  74. requires is_rvalue_reference_v<_Tp&&>
  75. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  76. constexpr auto operator()(_Tp&& __t) const
  77. noexcept(noexcept(ranges::data(static_cast<const _Tp&&>(__t))))
  78. -> decltype( ranges::data(static_cast<const _Tp&&>(__t)))
  79. { return ranges::data(static_cast<const _Tp&&>(__t)); }
  80. };
  81. } // namespace __cdata
  82. inline namespace __cpo {
  83. inline constexpr auto cdata = __cdata::__fn{};
  84. } // namespace __cpo
  85. } // namespace ranges
  86. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  87. _LIBCPP_END_NAMESPACE_STD
  88. #endif // _LIBCPP___RANGES_DATA_H