range_adaptor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_RANGE_ADAPTOR_H
  10. #define _LIBCPP___RANGES_RANGE_ADAPTOR_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/derived_from.h>
  13. #include <__concepts/invocable.h>
  14. #include <__concepts/same_as.h>
  15. #include <__config>
  16. #include <__functional/compose.h>
  17. #include <__functional/invoke.h>
  18. #include <__ranges/concepts.h>
  19. #include <__type_traits/decay.h>
  20. #include <__type_traits/is_nothrow_constructible.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__utility/forward.h>
  23. #include <__utility/move.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. #if _LIBCPP_STD_VER >= 20
  31. // CRTP base that one can derive from in order to be considered a range adaptor closure
  32. // by the library. When deriving from this class, a pipe operator will be provided to
  33. // make the following hold:
  34. // - `x | f` is equivalent to `f(x)`
  35. // - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))`
  36. template <class _Tp>
  37. struct __range_adaptor_closure;
  38. // Type that wraps an arbitrary function object and makes it into a range adaptor closure,
  39. // i.e. something that can be called via the `x | f` notation.
  40. template <class _Fn>
  41. struct __range_adaptor_closure_t : _Fn, __range_adaptor_closure<__range_adaptor_closure_t<_Fn>> {
  42. _LIBCPP_HIDE_FROM_ABI constexpr explicit __range_adaptor_closure_t(_Fn&& __f) : _Fn(std::move(__f)) { }
  43. };
  44. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__range_adaptor_closure_t);
  45. template <class _Tp>
  46. concept _RangeAdaptorClosure = derived_from<remove_cvref_t<_Tp>, __range_adaptor_closure<remove_cvref_t<_Tp>>>;
  47. template <class _Tp>
  48. struct __range_adaptor_closure {
  49. template <ranges::viewable_range _View, _RangeAdaptorClosure _Closure>
  50. requires same_as<_Tp, remove_cvref_t<_Closure>> &&
  51. invocable<_Closure, _View>
  52. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  53. friend constexpr decltype(auto) operator|(_View&& __view, _Closure&& __closure)
  54. noexcept(is_nothrow_invocable_v<_Closure, _View>)
  55. { return std::invoke(std::forward<_Closure>(__closure), std::forward<_View>(__view)); }
  56. template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>
  57. requires same_as<_Tp, remove_cvref_t<_Closure>> &&
  58. constructible_from<decay_t<_Closure>, _Closure> &&
  59. constructible_from<decay_t<_OtherClosure>, _OtherClosure>
  60. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  61. friend constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2)
  62. noexcept(is_nothrow_constructible_v<decay_t<_Closure>, _Closure> &&
  63. is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>)
  64. { return __range_adaptor_closure_t(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1))); }
  65. };
  66. #endif // _LIBCPP_STD_VER >= 20
  67. _LIBCPP_END_NAMESPACE_STD
  68. _LIBCPP_POP_MACROS
  69. #endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H