facade.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <vector>
  12. #include <range/v3/core.hpp>
  13. #include <range/v3/utility/copy.hpp>
  14. #include "../simple_test.hpp"
  15. #include "../test_utils.hpp"
  16. struct MyRange
  17. : ranges::view_facade<MyRange>
  18. {
  19. private:
  20. friend ranges::range_access;
  21. std::vector<int> ints_;
  22. struct cursor
  23. {
  24. private:
  25. std::vector<int>::const_iterator iter;
  26. public:
  27. using contiguous = std::true_type;
  28. cursor() = default;
  29. cursor(std::vector<int>::const_iterator it)
  30. : iter(it)
  31. {}
  32. int const & read() const
  33. {
  34. return *iter;
  35. }
  36. bool equal(cursor const &that) const
  37. {
  38. return iter == that.iter;
  39. }
  40. void next()
  41. {
  42. ++iter;
  43. }
  44. void prev()
  45. {
  46. --iter;
  47. }
  48. std::ptrdiff_t distance_to(cursor const &that) const
  49. {
  50. return that.iter - iter;
  51. }
  52. void advance(std::ptrdiff_t n)
  53. {
  54. iter += n;
  55. }
  56. };
  57. cursor begin_cursor() const
  58. {
  59. return {ints_.begin()};
  60. }
  61. cursor end_cursor() const
  62. {
  63. return {ints_.end()};
  64. }
  65. public:
  66. MyRange()
  67. : ints_{1, 2, 3, 4, 5, 6, 7}
  68. {}
  69. };
  70. int main()
  71. {
  72. using namespace ranges;
  73. {
  74. auto r = MyRange{};
  75. CPP_assert(view_<decltype(r)>);
  76. CPP_assert(common_range<decltype(r)>);
  77. CPP_assert(sized_range<decltype(r)>);
  78. CPP_assert(contiguous_range<decltype(r)>);
  79. ::check_equal(r, {1, 2, 3, 4, 5, 6, 7});
  80. CHECK(7u == r.size());
  81. CHECK(1 == r.front());
  82. CHECK(7 == r.back());
  83. CHECK(&*r.begin() == r.data());
  84. CHECK(r[1] == 2);
  85. CHECK(r[5] == 6);
  86. }
  87. {
  88. const auto r = MyRange{};
  89. CPP_assert(common_range<decltype(r)>);
  90. CPP_assert(sized_range<decltype(r)>);
  91. CPP_assert(random_access_range<decltype(r)>);
  92. CPP_assert(contiguous_range<decltype(r)>);
  93. ::check_equal(r, {1, 2, 3, 4, 5, 6, 7});
  94. CHECK(7u == r.size());
  95. CHECK(1 == r.front());
  96. CHECK(7 == r.back());
  97. CHECK(&*r.begin() == r.data());
  98. CHECK(r[1] == 2);
  99. CHECK(r[5] == 6);
  100. }
  101. return test_result();
  102. }