// Copyright (C) 2022 T. Zachary Laine // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_STL_INTERFACES_VIEW_ADAPTOR_HPP #define BOOST_STL_INTERFACES_VIEW_ADAPTOR_HPP #include #include #include #include #include #if !defined(BOOST_STL_INTERFACES_DOXYGEN) #if defined(__cpp_lib_ranges) && 202202L <= __cpp_lib_ranges #define BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE 1 #else #define BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE 0 #endif #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \ BOOST_STL_INTERFACES_USE_CONCEPTS && defined(__GNUC__) && 12 <= __GNUC__ #define BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE 1 #else #define BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE 0 #endif #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \ defined(_MSC_VER) && _MSC_VER <= 1929 #define BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE 1 #else #define BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE 0 #endif #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \ !BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE && \ !BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE #define BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE 1 #else #define BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE 0 #endif #endif namespace boost { namespace stl_interfaces { namespace detail { template using invocable_expr = decltype(std::declval()(std::declval()...)); template constexpr bool is_invocable_v = is_detected_v; template struct bind_back_t { static_assert(std::is_move_constructible::value, ""); #if defined(__cpp_fold_expressions) static_assert( (std::is_move_constructible::value && ...), ""); #endif template explicit constexpr bind_back_t(int, F && f, Args &&... args) : f_((F &&) f), bound_args_((Args &&) args...) { static_assert(sizeof...(Args) == sizeof...(CapturedArgs), ""); } template constexpr decltype(auto) operator()(Args &&... args) & { return call_impl(*this, indices(), (Args &&) args...); } template constexpr decltype(auto) operator()(Args &&... args) const & { return call_impl(*this, indices(), (Args &&) args...); } template constexpr decltype(auto) operator()(Args &&... args) && { return call_impl( std::move(*this), indices(), (Args &&) args...); } template constexpr decltype(auto) operator()(Args &&... args) const && { return call_impl( std::move(*this), indices(), (Args &&) args...); } private: using indices = std::index_sequence_for; template static constexpr decltype(auto) call_impl(T && this_, std::index_sequence, Args &&... args) { return ((T &&) this_) .f_((Args &&) args..., std::get(((T &&) this_).bound_args_)...); } Func f_; std::tuple bound_args_; }; template using bind_back_result = bind_back_t, std::decay_t...>; } /** An implementation of `std::bind_back()` from C++23. */ template constexpr auto bind_back(Func && f, Args &&... args) { return detail::bind_back_result( 0, (Func &&) f, (Args &&) args...); } #if BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE || \ defined(BOOST_STL_INTERFACES_DOXYGEN) /** A backwards-compatible implementation of C++23's `std::ranges::range_adaptor_closure`. `range_adaptor_closure` may be a struct template or may be an alias, as required to maintain compatability with the standard library's view adaptors. */ #if BOOST_STL_INTERFACES_USE_CONCEPTS template requires std::is_class_v && std::same_as> #else template< typename D, typename Enable = std::enable_if_t< std::is_class::value && std::is_same>::value>> #endif struct range_adaptor_closure; namespace detail { #if BOOST_STL_INTERFACES_USE_CONCEPTS template concept range_adaptor_closure_ = std::derived_from< std::remove_cvref_t, range_adaptor_closure>>; #else template using range_adaptor_closure_tag_expr = typename range_adaptor_closure< T>::inheritance_tag_with_an_unlikely_name_; template constexpr bool range_adaptor_closure_ = is_detected_v>; #endif } #endif #if BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE template using range_adaptor_closure = std::ranges::range_adaptor_closure; #elif BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE template using range_adaptor_closure = std::views::__adaptor::_RangeAdaptorClosure; #elif BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE template using range_adaptor_closure = detail::pipeable; #else #if BOOST_STL_INTERFACES_USE_CONCEPTS template requires std::is_class_v && std::same_as> #else template #endif struct range_adaptor_closure { #if BOOST_STL_INTERFACES_USE_CONCEPTS template requires std::invocable #else template< typename T, typename Enable = std::enable_if_t>> #endif [[nodiscard]] friend constexpr decltype(auto) operator|(T && t, D && d) { return std::move(d)((T &&) t); } #if BOOST_STL_INTERFACES_USE_CONCEPTS template requires std::invocable #else template< typename T, typename Enable = std::enable_if_t>> #endif [[nodiscard]] friend constexpr decltype(auto) operator|(T && t, D const & d) { return d((T &&) t); } using inheritance_tag_with_an_unlikely_name_ = int; }; #endif //[closure_defn /** An invocable consisting of a contained invocable `f`. Calling `operator()` with some argument `t` calls `f(t)` and returns the result. This type is typically used to capture a the result of a call to `bind_back()`. */ template struct closure : range_adaptor_closure> { constexpr closure(F f) : f_(f) {} #if BOOST_STL_INTERFACES_USE_CONCEPTS template requires std::invocable #else template< typename T, typename Enable = std::enable_if_t>> #endif constexpr decltype(auto) operator()(T && t) const { return f_((T &&) t); } private: F f_; }; //] namespace detail { #if !BOOST_STL_INTERFACES_USE_CONCEPTS template struct adaptor_impl { static constexpr decltype(auto) call(F const & f, Args &&... args) { return f((Args &&) args...); } }; template struct adaptor_impl { static constexpr auto call(F const & f, Args &&... args) { using closure_func = std::decay_t; return closure( stl_interfaces::bind_back(f, (Args &&) args...)); } }; #endif } //[adaptor_defn /** Adapts an invocable `f` as a view adaptor. Calling `operator(args...)` will either: call `f(args...)` and return the result, if `f(args...)` is well-formed; or return `closure(stl_interfaces::bind_back(f, args...))` otherwise. */ template struct adaptor { constexpr adaptor(F f) : f_(f) {} // clang-format off template constexpr auto operator()(Args &&... args) const // clang-format on { #if BOOST_STL_INTERFACES_USE_CONCEPTS if constexpr (std::is_invocable_v) { return f((Args &&) args...); } else { return closure( stl_interfaces::bind_back(f_, (Args &&) args...)); } #else return detail::adaptor_impl< F const &, detail::is_invocable_v, Args...>::call(f_, (Args &&) args...); #endif } private: F f_; }; //] }} #endif