results_iterator.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DETAIL_RESULTS_ITERATOR_HPP
  8. #define BOOST_MYSQL_DETAIL_RESULTS_ITERATOR_HPP
  9. #include <boost/mysql/resultset.hpp>
  10. #include <boost/mysql/resultset_view.hpp>
  11. #include <boost/mysql/detail/access.hpp>
  12. #include <boost/mysql/detail/execution_processor/results_impl.hpp>
  13. namespace boost {
  14. namespace mysql {
  15. namespace detail {
  16. class results_iterator
  17. {
  18. const results_impl* self_{};
  19. std::size_t index_{};
  20. public:
  21. using value_type = resultset;
  22. using reference = resultset_view;
  23. using pointer = resultset_view;
  24. using difference_type = std::ptrdiff_t;
  25. using iterator_category = std::random_access_iterator_tag;
  26. results_iterator() = default;
  27. results_iterator(const results_impl* self, std::size_t index) noexcept : self_(self), index_(index) {}
  28. results_iterator& operator++() noexcept
  29. {
  30. ++index_;
  31. return *this;
  32. }
  33. results_iterator operator++(int) noexcept
  34. {
  35. auto res = *this;
  36. ++(*this);
  37. return res;
  38. }
  39. results_iterator& operator--() noexcept
  40. {
  41. --index_;
  42. return *this;
  43. }
  44. results_iterator operator--(int) noexcept
  45. {
  46. auto res = *this;
  47. --(*this);
  48. return res;
  49. }
  50. results_iterator& operator+=(std::ptrdiff_t n) noexcept
  51. {
  52. index_ += n;
  53. return *this;
  54. }
  55. results_iterator& operator-=(std::ptrdiff_t n) noexcept
  56. {
  57. index_ -= n;
  58. return *this;
  59. }
  60. results_iterator operator+(std::ptrdiff_t n) const noexcept
  61. {
  62. return results_iterator(self_, index_ + n);
  63. }
  64. results_iterator operator-(std::ptrdiff_t n) const noexcept { return *this + (-n); }
  65. std::ptrdiff_t operator-(results_iterator rhs) const noexcept { return index_ - rhs.index_; }
  66. pointer operator->() const noexcept { return **this; }
  67. reference operator*() const noexcept { return (*this)[0]; }
  68. reference operator[](std::ptrdiff_t i) const noexcept
  69. {
  70. return access::construct<resultset_view>(*self_, index_ + i);
  71. }
  72. bool operator==(results_iterator rhs) const noexcept { return index_ == rhs.index_; }
  73. bool operator!=(results_iterator rhs) const noexcept { return !(*this == rhs); }
  74. bool operator<(results_iterator rhs) const noexcept { return index_ < rhs.index_; }
  75. bool operator<=(results_iterator rhs) const noexcept { return index_ <= rhs.index_; }
  76. bool operator>(results_iterator rhs) const noexcept { return index_ > rhs.index_; }
  77. bool operator>=(results_iterator rhs) const noexcept { return index_ >= rhs.index_; }
  78. std::size_t index() const noexcept { return index_; }
  79. const results_impl* obj() const noexcept { return self_; }
  80. };
  81. inline results_iterator operator+(std::ptrdiff_t n, results_iterator it) noexcept { return it + n; }
  82. } // namespace detail
  83. } // namespace mysql
  84. } // namespace boost
  85. #endif