rows_view.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_ROWS_VIEW_HPP
  8. #define BOOST_MYSQL_ROWS_VIEW_HPP
  9. #include <boost/mysql/field_view.hpp>
  10. #include <boost/mysql/row.hpp>
  11. #include <boost/mysql/row_view.hpp>
  12. #include <boost/mysql/detail/access.hpp>
  13. #include <boost/mysql/detail/rows_iterator.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <cstddef>
  17. #include <stdexcept>
  18. namespace boost {
  19. namespace mysql {
  20. /**
  21. * \brief A non-owning read-only reference to a sequence of rows.
  22. * \details
  23. * Models a non-owning matrix-like container. Indexing a `rows_view` object (by using iterators,
  24. * \ref rows_view::at or \ref rows_view::operator[]) returns a \ref row_view object, representing a
  25. * single row. All rows in the collection are the same size (as given by \ref num_columns).
  26. * \n
  27. * A `rows_view` object points to memory owned by an external entity (like `string_view` does). The
  28. * validity of a `rows_view` object depends on how it was obtained:
  29. * \n
  30. * \li If it was constructed from a \ref rows object (by calling \ref rows::operator rows_view()),
  31. * the view acts as a reference to the `rows`' allocated memory, and is valid as long as
  32. * references to that `rows` elements are valid.
  33. * \li If it was obtained by calling \ref connection::read_some_rows it's valid until the
  34. * `connection` performs the next network call or is destroyed.
  35. * \n
  36. * \ref row_view's and \ref field_view's obtained by using a `rows_view` object are valid as long as
  37. * the underlying storage that `*this` points to is valid. Destroying `*this` doesn't invalidate
  38. * such references.
  39. * \n
  40. * Calling any member function on an invalid view results in undefined behavior.
  41. * \n
  42. * Instances of this class are usually created by the library, not by the user.
  43. */
  44. class rows_view
  45. {
  46. public:
  47. #ifdef BOOST_MYSQL_DOXYGEN
  48. /**
  49. * \brief A random access iterator to an element.
  50. * \details The exact type of the iterator is unspecified.
  51. */
  52. using iterator = __see_below__;
  53. #else
  54. using iterator = detail::rows_iterator;
  55. #endif
  56. /// \copydoc iterator
  57. using const_iterator = iterator;
  58. /// A type that can hold elements in this collection with value semantics.
  59. using value_type = row;
  60. /// The reference type.
  61. using reference = row_view;
  62. /// \copydoc reference
  63. using const_reference = row_view;
  64. /// An unsigned integer type to represent sizes.
  65. using size_type = std::size_t;
  66. /// A signed integer type used to represent differences.
  67. using difference_type = std::ptrdiff_t;
  68. /**
  69. * \brief Construct an empty (but valid) view.
  70. * \par Exception safety
  71. * No-throw guarantee.
  72. */
  73. rows_view() = default;
  74. /**
  75. * \brief Returns an iterator to the first element in the collection.
  76. * \par Exception safety
  77. * No-throw guarantee.
  78. *
  79. * \par Complexity
  80. * Constant.
  81. */
  82. const_iterator begin() const noexcept { return iterator(fields_, num_columns_, 0); }
  83. /**
  84. * \brief Returns an iterator to one-past-the-last element in the collection.
  85. * \par Exception safety
  86. * No-throw guarantee.
  87. *
  88. * \par Complexity
  89. * Constant.
  90. */
  91. const_iterator end() const noexcept { return iterator(fields_, num_columns_, size()); }
  92. /**
  93. * \brief Returns the i-th row or throws an exception.
  94. * \par Exception safety
  95. * Strong guranatee. Throws on invalid input.
  96. * \throws std::out_of_range `i >= this->size()`
  97. *
  98. * \par Complexity
  99. * Constant.
  100. */
  101. row_view at(std::size_t i) const
  102. {
  103. if (i >= size())
  104. BOOST_THROW_EXCEPTION(std::out_of_range("rows_view::at"));
  105. return detail::row_slice(fields_, num_columns_, i);
  106. }
  107. /**
  108. * \brief Returns the i-th row (unchecked access).
  109. * \par Preconditions
  110. * `i < this->size()`
  111. *
  112. * \par Exception safety
  113. * No-throw guarantee.
  114. *
  115. * \par Complexity
  116. * Constant.
  117. */
  118. row_view operator[](std::size_t i) const noexcept
  119. {
  120. BOOST_ASSERT(i < size());
  121. return detail::row_slice(fields_, num_columns_, i);
  122. }
  123. /**
  124. * \brief Returns the first row.
  125. * \par Preconditions
  126. * `this->size() > 0`
  127. *
  128. * \par Exception safety
  129. * No-throw guarantee.
  130. *
  131. * \par Complexity
  132. * Constant.
  133. */
  134. row_view front() const noexcept { return (*this)[0]; }
  135. /**
  136. * \brief Returns the last row.
  137. * \par Preconditions
  138. * `this->size() > 0`
  139. *
  140. * \par Exception safety
  141. * No-throw guarantee.
  142. *
  143. * \par Complexity
  144. * Constant.
  145. */
  146. row_view back() const noexcept { return (*this)[size() - 1]; }
  147. /**
  148. * \brief Returns true if there are no rows in the collection (i.e. `this->size() == 0`)
  149. * \par Exception safety
  150. * No-throw guarantee.
  151. *
  152. * \par Complexity
  153. * Constant.
  154. */
  155. bool empty() const noexcept { return num_fields_ == 0; }
  156. /**
  157. * \brief Returns the number of rows in the collection.
  158. * \par Exception safety
  159. * No-throw guarantee.
  160. *
  161. * \par Complexity
  162. * Constant.
  163. */
  164. std::size_t size() const noexcept { return (num_columns_ == 0) ? 0 : (num_fields_ / num_columns_); }
  165. /**
  166. * \brief Returns the number of elements each row in the collection has.
  167. * \details For every \ref row_view object r obtained from this collection,
  168. * `r.size() == this->num_columns()`.
  169. *
  170. * \par Exception safety
  171. * No-throw guarantee.
  172. *
  173. * \par Complexity
  174. * Constant.
  175. */
  176. std::size_t num_columns() const noexcept { return num_columns_; }
  177. /**
  178. * \brief Equality operator.
  179. * \details The containers are considered equal if they have the same number of rows and
  180. * they all compare equal, as defined by \ref row_view::operator==.
  181. *
  182. * \par Exception safety
  183. * No-throw guarantee.
  184. *
  185. * \par Complexity
  186. * Linear on `this->size() * this->num_columns()`.
  187. */
  188. bool operator==(const rows_view& rhs) const noexcept
  189. {
  190. if (num_fields_ != rhs.num_fields_ || num_columns_ != rhs.num_columns_)
  191. return false;
  192. for (std::size_t i = 0; i < num_fields_; ++i)
  193. {
  194. if (fields_[i] != rhs.fields_[i])
  195. return false;
  196. }
  197. return true;
  198. }
  199. /**
  200. * \brief Inequality operator.
  201. *
  202. * \par Exception safety
  203. * No-throw guarantee.
  204. *
  205. * \par Complexity
  206. * Linear on `this->size() * this->num_columns()`.
  207. */
  208. inline bool operator!=(const rows_view& rhs) const noexcept { return !(*this == rhs); }
  209. private:
  210. const field_view* fields_{};
  211. std::size_t num_fields_{};
  212. std::size_t num_columns_{};
  213. rows_view(const field_view* fields, std::size_t num_fields, std::size_t num_columns) noexcept
  214. : fields_(fields), num_fields_(num_fields), num_columns_(num_columns)
  215. {
  216. BOOST_ASSERT(fields != nullptr || num_fields == 0); // fields null => num_fields 0
  217. BOOST_ASSERT(num_fields == 0 || num_columns != 0); // num_fields != 0 => num_columns != 0
  218. BOOST_ASSERT(num_columns == 0 || (num_fields % num_columns == 0));
  219. }
  220. #ifndef BOOST_MYSQL_DOXYGEN
  221. friend struct detail::access;
  222. friend class rows;
  223. #endif
  224. };
  225. } // namespace mysql
  226. } // namespace boost
  227. #endif