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