/// \file // Range v3 library // // Copyright Filip Matzner 2017 // // Use, modification and distribution is subject to 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) // // Project home: https://github.com/ericniebler/range-v3 // #ifndef RANGES_V3_EXPERIMENTAL_VIEW_SHARED_HPP #define RANGES_V3_EXPERIMENTAL_VIEW_SHARED_HPP #include #include #include #include #include #include #include #include #include namespace ranges { /// \addtogroup group-views /// @{ namespace experimental { template struct shared_view : view_interface, range_cardinality::value> { private: // shared storage std::shared_ptr rng_ptr_; public: shared_view() = default; // construct from a range rvalue explicit shared_view(Rng rng) : rng_ptr_{std::make_shared(std::move(rng))} {} // use the stored range's begin and end iterator_t begin() const { return ranges::begin(*rng_ptr_); } sentinel_t end() const { return ranges::end(*rng_ptr_); } CPP_auto_member auto CPP_fun(size)()(const requires sized_range) { return ranges::size(*rng_ptr_); } }; template struct shared_closure; struct RANGES_STRUCT_WITH_ADL_BARRIER(shared_closure_base) { // Piping requires viewable_ranges. template(typename Rng, typename SharedFn)( requires range AND (!viewable_range) AND constructible_from, Rng>) friend constexpr auto operator|(Rng && rng, shared_closure vw) { return static_cast(vw)(static_cast(rng)); } template friend constexpr auto operator|(shared_closure sh, Pipeable pipe) -> CPP_broken_friend_ret(shared_closure>)( requires (is_pipeable_v)) { return shared_closure>{compose( static_cast(pipe), static_cast(sh))}; } }; template struct shared_closure : shared_closure_base , SharedFn { shared_closure() = default; constexpr explicit shared_closure(SharedFn fn) : SharedFn(static_cast(fn)) {} }; namespace views { struct shared_fn { template(typename Rng)( requires range AND (!viewable_range)AND constructible_from, Rng>) shared_view> operator()(Rng && rng) const { return shared_view>{static_cast(rng)}; } }; /// \relates shared_fn /// \ingroup group-views RANGES_INLINE_VARIABLE(shared_closure, shared) } // namespace views } // namespace experimental template RANGES_INLINE_VAR constexpr bool is_pipeable_v> = true; /// @} } // namespace ranges #include #endif