Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/range_adaptor.h
35236 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___RANGES_RANGE_ADAPTOR_H10#define _LIBCPP___RANGES_RANGE_ADAPTOR_H1112#include <__concepts/constructible.h>13#include <__concepts/derived_from.h>14#include <__concepts/invocable.h>15#include <__concepts/same_as.h>16#include <__config>17#include <__functional/compose.h>18#include <__functional/invoke.h>19#include <__ranges/concepts.h>20#include <__type_traits/decay.h>21#include <__type_traits/is_class.h>22#include <__type_traits/is_nothrow_constructible.h>23#include <__type_traits/remove_cvref.h>24#include <__utility/forward.h>25#include <__utility/move.h>2627#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)28# pragma GCC system_header29#endif3031_LIBCPP_PUSH_MACROS32#include <__undef_macros>3334_LIBCPP_BEGIN_NAMESPACE_STD3536#if _LIBCPP_STD_VER >= 203738namespace ranges {3940// CRTP base that one can derive from in order to be considered a range adaptor closure41// by the library. When deriving from this class, a pipe operator will be provided to42// make the following hold:43// - `x | f` is equivalent to `f(x)`44// - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))`45template <class _Tp>46requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>47struct __range_adaptor_closure;4849// Type that wraps an arbitrary function object and makes it into a range adaptor closure,50// i.e. something that can be called via the `x | f` notation.51template <class _Fn>52struct __range_adaptor_closure_t : _Fn, __range_adaptor_closure<__range_adaptor_closure_t<_Fn>> {53_LIBCPP_HIDE_FROM_ABI constexpr explicit __range_adaptor_closure_t(_Fn&& __f) : _Fn(std::move(__f)) {}54};55_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__range_adaptor_closure_t);5657template <class _Tp>58_Tp __derived_from_range_adaptor_closure(__range_adaptor_closure<_Tp>*);5960template <class _Tp>61concept _RangeAdaptorClosure = !ranges::range<remove_cvref_t<_Tp>> && requires {62// Ensure that `remove_cvref_t<_Tp>` is derived from `__range_adaptor_closure<remove_cvref_t<_Tp>>` and isn't derived63// from `__range_adaptor_closure<U>` for any other type `U`.64{ ranges::__derived_from_range_adaptor_closure((remove_cvref_t<_Tp>*)nullptr) } -> same_as<remove_cvref_t<_Tp>>;65};6667template <ranges::range _Range, _RangeAdaptorClosure _Closure>68requires invocable<_Closure, _Range>69[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto)70operator|(_Range&& __range, _Closure&& __closure) noexcept(is_nothrow_invocable_v<_Closure, _Range>) {71return std::invoke(std::forward<_Closure>(__closure), std::forward<_Range>(__range));72}7374template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>75requires constructible_from<decay_t<_Closure>, _Closure> && constructible_from<decay_t<_OtherClosure>, _OtherClosure>76[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2) noexcept(77is_nothrow_constructible_v<decay_t<_Closure>, _Closure> &&78is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>) {79return __range_adaptor_closure_t(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1)));80}8182template <class _Tp>83requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>84struct __range_adaptor_closure {};8586# if _LIBCPP_STD_VER >= 2387template <class _Tp>88requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>89class range_adaptor_closure : public __range_adaptor_closure<_Tp> {};90# endif // _LIBCPP_STD_VER >= 239192} // namespace ranges9394#endif // _LIBCPP_STD_VER >= 209596_LIBCPP_END_NAMESPACE_STD9798_LIBCPP_POP_MACROS99100#endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H101102103