// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP___RANGES_SIZE_H #define _LIBCPP___RANGES_SIZE_H #include <__config> #include <__iterator/concepts.h> #include <__iterator/iterator_traits.h> #include <__ranges/access.h> #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_STD #if !defined(_LIBCPP_HAS_NO_RANGES) // clang-format off namespace ranges { template inline constexpr bool disable_sized_range = false; // [range.prim.size] namespace __size { void size(auto&) = delete; void size(const auto&) = delete; template concept __size_enabled = !disable_sized_range>; template concept __member_size = __size_enabled<_Tp> && requires(_Tp&& __t) { { _VSTD::__decay_copy(_VSTD::forward<_Tp>(__t).size()) } -> __integer_like; }; template concept __unqualified_size = __size_enabled<_Tp> && !__member_size<_Tp> && __class_or_enum> && requires(_Tp&& __t) { { _VSTD::__decay_copy(size(_VSTD::forward<_Tp>(__t))) } -> __integer_like; }; template concept __difference = !__member_size<_Tp> && !__unqualified_size<_Tp> && __class_or_enum> && requires(_Tp&& __t) { { ranges::begin(__t) } -> forward_iterator; { ranges::end(__t) } -> sized_sentinel_for()))>; }; struct __fn { template [[nodiscard]] constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept { return _Sz; } template [[nodiscard]] constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept { return _Sz; } template <__member_size _Tp> [[nodiscard]] constexpr __integer_like auto operator()(_Tp&& __t) const noexcept(noexcept(_VSTD::forward<_Tp>(__t).size())) { return _VSTD::forward<_Tp>(__t).size(); } template <__unqualified_size _Tp> [[nodiscard]] constexpr __integer_like auto operator()(_Tp&& __t) const noexcept(noexcept(size(_VSTD::forward<_Tp>(__t)))) { return size(_VSTD::forward<_Tp>(__t)); } template<__difference _Tp> [[nodiscard]] constexpr __integer_like auto operator()(_Tp&& __t) const noexcept(noexcept(ranges::end(__t) - ranges::begin(__t))) { return _VSTD::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t)); } }; } // end namespace __size inline namespace __cpo { inline constexpr auto size = __size::__fn{}; } // namespace __cpo namespace __ssize { struct __fn { template requires requires (_Tp&& __t) { ranges::size(__t); } [[nodiscard]] constexpr integral auto operator()(_Tp&& __t) const noexcept(noexcept(ranges::size(__t))) { using _Signed = make_signed_t; if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed)) return static_cast(ranges::size(__t)); else return static_cast<_Signed>(ranges::size(__t)); } }; } inline namespace __cpo { inline constexpr const auto ssize = __ssize::__fn{}; } // namespace __cpo } // namespace ranges // clang-format off #endif // !defined(_LIBCPP_HAS_NO_RANGES) _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS #endif // _LIBCPP___RANGES_SIZE_H