data.h 3.2 KB

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