empty.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_EMPTY_H
  10. #define _LIBCPP___RANGES_EMPTY_H
  11. #include <__concepts/class_or_enum.h>
  12. #include <__config>
  13. #include <__iterator/concepts.h>
  14. #include <__ranges/access.h>
  15. #include <__ranges/size.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. #if _LIBCPP_STD_VER > 17
  21. // [range.prim.empty]
  22. namespace ranges {
  23. namespace __empty {
  24. template <class _Tp>
  25. concept __member_empty =
  26. __workaround_52970<_Tp> &&
  27. requires(_Tp&& __t) {
  28. bool(__t.empty());
  29. };
  30. template<class _Tp>
  31. concept __can_invoke_size =
  32. !__member_empty<_Tp> &&
  33. requires(_Tp&& __t) { ranges::size(__t); };
  34. template <class _Tp>
  35. concept __can_compare_begin_end =
  36. !__member_empty<_Tp> &&
  37. !__can_invoke_size<_Tp> &&
  38. requires(_Tp&& __t) {
  39. bool(ranges::begin(__t) == ranges::end(__t));
  40. { ranges::begin(__t) } -> forward_iterator;
  41. };
  42. struct __fn {
  43. template <__member_empty _Tp>
  44. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
  45. noexcept(noexcept(bool(__t.empty()))) {
  46. return bool(__t.empty());
  47. }
  48. template <__can_invoke_size _Tp>
  49. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
  50. noexcept(noexcept(ranges::size(__t))) {
  51. return ranges::size(__t) == 0;
  52. }
  53. template<__can_compare_begin_end _Tp>
  54. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(_Tp&& __t) const
  55. noexcept(noexcept(bool(ranges::begin(__t) == ranges::end(__t)))) {
  56. return ranges::begin(__t) == ranges::end(__t);
  57. }
  58. };
  59. } // namespace __empty
  60. inline namespace __cpo {
  61. inline constexpr auto empty = __empty::__fn{};
  62. } // namespace __cpo
  63. } // namespace ranges
  64. #endif // _LIBCPP_STD_VER > 17
  65. _LIBCPP_END_NAMESPACE_STD
  66. #endif // _LIBCPP___RANGES_EMPTY_H