You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Filip Matzner 2017
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_EXPERIMENTAL_VIEW_SHARED_HPP
  14. #define RANGES_V3_EXPERIMENTAL_VIEW_SHARED_HPP
  15. #include <memory>
  16. #include <type_traits>
  17. #include <meta/meta.hpp>
  18. #include <range/v3/range/access.hpp>
  19. #include <range/v3/range/concepts.hpp>
  20. #include <range/v3/range/primitives.hpp>
  21. #include <range/v3/view/all.hpp>
  22. namespace ranges
  23. {
  24. namespace experimental
  25. {
  26. template<typename Rng>
  27. struct shared_view
  28. : view_interface<shared_view<Rng>, range_cardinality<Rng>::value>
  29. {
  30. private:
  31. // shared storage
  32. std::shared_ptr<Rng> rng_ptr_;
  33. public:
  34. shared_view() = default;
  35. // construct from a range rvalue
  36. explicit shared_view(Rng && t)
  37. : rng_ptr_{std::make_shared<Rng>(std::move(t))}
  38. {}
  39. // use the stored range's begin and end
  40. iterator_t<Rng> begin() const
  41. {
  42. return ranges::begin(*rng_ptr_);
  43. }
  44. sentinel_t<Rng> end() const
  45. {
  46. return ranges::end(*rng_ptr_);
  47. }
  48. CPP_member
  49. auto CPP_fun(size)()(const requires sized_range<Rng>)
  50. {
  51. return ranges::size(*rng_ptr_);
  52. }
  53. };
  54. /// \relates all
  55. /// \addtogroup group-views
  56. /// @{
  57. namespace views
  58. {
  59. struct shared_fn : pipeable_base
  60. {
  61. public:
  62. template<typename Rng>
  63. auto operator()(Rng && t) const -> CPP_ret(shared_view<Rng>)( //
  64. requires range<Rng> &&
  65. (!view_<Rng>)&&(!std::is_reference<Rng>::value))
  66. {
  67. return shared_view<Rng>{std::move(t)};
  68. }
  69. };
  70. /// \relates all_fn
  71. /// \ingroup group-views
  72. RANGES_INLINE_VARIABLE(shared_fn, shared)
  73. template<typename Rng>
  74. using shared_t = detail::decay_t<decltype(shared(std::declval<Rng>()))>;
  75. } // namespace views
  76. /// @}
  77. } // namespace experimental
  78. } // namespace ranges
  79. #endif // include guard