xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/data.h (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 // -*- C++ -*-
2 //===------------------------ __ranges/data.h ------------------------------===//
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 
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/forward.h>
18 #include <concepts>
19 #include <type_traits>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
23 #endif
24 
25 _LIBCPP_PUSH_MACROS
26 #include <__undef_macros>
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 #if !defined(_LIBCPP_HAS_NO_RANGES)
31 
32 // clang-format off
33 namespace ranges {
34 // [range.prim.data]
35 namespace __data {
36   template <class _Tp>
37   concept __ptr_to_object = is_pointer_v<_Tp> && is_object_v<remove_pointer_t<_Tp>>;
38 
39   template <class _Tp>
40   concept __member_data =
41     requires(_Tp&& __t) {
42       { _VSTD::forward<_Tp>(__t) } -> __can_borrow;
43       { __t.data() } -> __ptr_to_object;
44     };
45 
46   template <class _Tp>
47   concept __ranges_begin_invocable =
48     !__member_data<_Tp> &&
49     requires(_Tp&& __t) {
50       { _VSTD::forward<_Tp>(__t) } -> __can_borrow;
51       { ranges::begin(_VSTD::forward<_Tp>(__t)) } -> contiguous_iterator;
52     };
53 
54   struct __fn {
55     template <__member_data _Tp>
56       requires __can_borrow<_Tp>
57     _LIBCPP_HIDE_FROM_ABI
58     constexpr __ptr_to_object auto operator()(_Tp&& __t) const
59         noexcept(noexcept(__t.data())) {
60       return __t.data();
61     }
62 
63     template<__ranges_begin_invocable _Tp>
64       requires __can_borrow<_Tp>
65     _LIBCPP_HIDE_FROM_ABI
66     constexpr __ptr_to_object auto operator()(_Tp&& __t) const
67         noexcept(noexcept(_VSTD::to_address(ranges::begin(_VSTD::forward<_Tp>(__t))))) {
68       return _VSTD::to_address(ranges::begin(_VSTD::forward<_Tp>(__t)));
69     }
70   };
71 } // end namespace __data
72 
73 inline namespace __cpo {
74   inline constexpr const auto data = __data::__fn{};
75 } // namespace __cpo
76 } // namespace ranges
77 
78 // clang-format off
79 
80 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
81 
82 _LIBCPP_END_NAMESPACE_STD
83 
84 _LIBCPP_POP_MACROS
85 
86 #endif // _LIBCPP___RANGES_DATA_H
87