range_adaptor.h 3.0 KB

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