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