rows.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_HPP
  8. #define BOOST_MYSQL_ROWS_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/rows_view.hpp>
  13. #include <boost/mysql/detail/row_impl.hpp>
  14. #include <boost/mysql/detail/rows_iterator.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <stdexcept>
  17. namespace boost {
  18. namespace mysql {
  19. /**
  20. * \brief An owning, read-only sequence of rows.
  21. * \details
  22. * Models an owning, matrix-like container. Indexing a `rows` object (by using iterators,
  23. * \ref rows::at or \ref rows::operator[]) returns a \ref row_view object, representing a
  24. * single row. All rows in the collection are the same size (as given by \ref num_columns).
  25. * \n
  26. * A `rows` object owns a chunk of memory in which it stores its elements. The \ref rows_view
  27. * objects obtained on element access point into the `rows`' internal storage. These views (and any
  28. * \ref row_view and \ref field_view obtained from the former) behave
  29. * like references, and are valid as long as pointers, iterators and references into the `rows`
  30. * object remain valid.
  31. * \n
  32. * Although owning, `rows` is read-only. It's optimized for memory re-use.
  33. */
  34. class rows
  35. {
  36. public:
  37. #ifdef BOOST_MYSQL_DOXYGEN
  38. /**
  39. * \brief A random access iterator to an element.
  40. * \details The exact type of the iterator is unspecified.
  41. */
  42. using iterator = __see_below__;
  43. #else
  44. using iterator = detail::rows_iterator;
  45. #endif
  46. /// \copydoc iterator
  47. using const_iterator = iterator;
  48. /**
  49. * \brief A type that can hold elements in this collection with value semantics.
  50. * \details Note that element accesors (like \ref rows_view::operator[]) return \ref reference
  51. * objects instead of `value_type` objects. You can use this type if you need an owning class.
  52. */
  53. using value_type = row;
  54. /// The reference type.
  55. using reference = row_view;
  56. /// \copydoc reference
  57. using const_reference = row_view;
  58. /// An unsigned integer type to represent sizes.
  59. using size_type = std::size_t;
  60. /// A signed integer type used to represent differences.
  61. using difference_type = std::ptrdiff_t;
  62. /**
  63. * \brief Construct an empty `rows` object.
  64. * \par Exception safety
  65. * No-throw guarantee.
  66. */
  67. rows() = default;
  68. /**
  69. * \brief Copy constructor.
  70. * \par Exception safety
  71. * Strong guarantee. Internal allocations may throw.
  72. *
  73. * \par Object lifetimes
  74. * `*this` lifetime will be independent of `other`'s.
  75. *
  76. * \par Complexity
  77. * Linear on `other.size() * other.num_columns()`.
  78. */
  79. rows(const rows& other) = default;
  80. /**
  81. * \brief Move constructor.
  82. * \par Exception safety
  83. * No-throw guarantee.
  84. *
  85. * \par Object lifetimes
  86. * Iterators and references (including \ref rows_view's, \ref row_view's and \ref
  87. * field_view's) to elements in `other` remain valid.
  88. *
  89. * \par Complexity
  90. * Constant.
  91. */
  92. rows(rows&& other) = default;
  93. /**
  94. * \brief Copy assignment.
  95. * \par Exception safety
  96. * Basic guarantee. Internal allocations may throw.
  97. *
  98. * \par Object lifetimes
  99. * `*this` lifetime will be independent of `other`'s. Iterators and references
  100. * (including \ref rows_view's, \ref row_view's and \ref field_view's) to elements in `*this`
  101. * are invalidated.
  102. *
  103. * \par Complexity
  104. * Linear on `this->size() * this->num_columns()` and `other.size() * other.num_columns()`.
  105. */
  106. rows& operator=(const rows& other) = default;
  107. /**
  108. * \brief Move assignment.
  109. * \par Exception safety
  110. * No-throw guarantee.
  111. *
  112. * \par Object lifetimes
  113. * Iterators and references (including \ref rows_view's \ref row_view's and \ref
  114. * field_view's) to elements in `*this` are invalidated. Iterators and references to elements in
  115. * `other` remain valid.
  116. *
  117. * \par Complexity
  118. * Constant.
  119. */
  120. rows& operator=(rows&& other) = default;
  121. /**
  122. * \brief Destructor.
  123. */
  124. ~rows() = default;
  125. /**
  126. * \brief Constructs a rows object from a \ref rows_view.
  127. * \par Exception safety
  128. * Strong guarantee. Internal allocations may throw.
  129. *
  130. * \par Object lifetimes
  131. * `*this` lifetime will be independent of `r`'s (the contents of `r` will be copied
  132. * into `*this`).
  133. *
  134. * \par Complexity
  135. * Linear on `r.size() * r.num_columns()`.
  136. */
  137. rows(const rows_view& r) : impl_(r.fields_, r.num_fields_), num_columns_(r.num_columns_) {}
  138. /**
  139. * \brief Replaces the contents of `*this` with a \ref rows_view.
  140. * \par Exception safety
  141. * Basic guarantee. Internal allocations may throw.
  142. *
  143. * \par Object lifetimes
  144. * `*this` lifetime will be independent of `r`'s (the contents of `r` will be copied
  145. * into `*this`). Iterators and references (including \ref rows_view's \ref row_view's and \ref
  146. * field_view's) to elements in `*this` are invalidated.
  147. *
  148. * \par Complexity
  149. * Linear on `r.size() * r.num_columns()`.
  150. */
  151. rows& operator=(const rows_view& rhs)
  152. {
  153. impl_.assign(rhs.fields_, rhs.num_fields_);
  154. num_columns_ = rhs.num_columns_;
  155. return *this;
  156. }
  157. /// \copydoc rows_view::begin
  158. iterator begin() const noexcept { return iterator(impl_.fields().data(), num_columns_, 0); }
  159. /// \copydoc rows_view::end
  160. iterator end() const noexcept { return iterator(impl_.fields().data(), num_columns_, size()); }
  161. /// \copydoc rows_view::at
  162. row_view at(std::size_t i) const
  163. {
  164. if (i >= size())
  165. BOOST_THROW_EXCEPTION(std::out_of_range("rows::at"));
  166. return detail::row_slice(impl_.fields().data(), num_columns_, i);
  167. }
  168. /// \copydoc rows_view::operator[]
  169. row_view operator[](std::size_t i) const noexcept
  170. {
  171. BOOST_ASSERT(i < size());
  172. return detail::row_slice(impl_.fields().data(), num_columns_, i);
  173. }
  174. /// \copydoc rows_view::front
  175. row_view front() const noexcept { return (*this)[0]; }
  176. /// \copydoc rows_view::back
  177. row_view back() const noexcept { return (*this)[size() - 1]; }
  178. /// \copydoc rows_view::empty
  179. bool empty() const noexcept { return impl_.fields().empty(); }
  180. /// \copydoc rows_view::size
  181. std::size_t size() const noexcept { return num_columns_ == 0 ? 0 : impl_.fields().size() / num_columns_; }
  182. /// \copydoc rows_view::num_columns
  183. std::size_t num_columns() const noexcept { return num_columns_; }
  184. /**
  185. * \brief Creates a \ref rows_view that references `*this`.
  186. * \par Exception safety
  187. * No-throw guarantee.
  188. *
  189. * \par Object lifetimes
  190. * The returned view will be valid until any function that invalidates iterators and
  191. * references is invoked on `*this` or `*this` is destroyed.
  192. *
  193. * \par Complexity
  194. * Constant.
  195. */
  196. operator rows_view() const noexcept
  197. {
  198. return rows_view(impl_.fields().data(), impl_.fields().size(), num_columns_);
  199. }
  200. private:
  201. detail::row_impl impl_;
  202. std::size_t num_columns_{};
  203. };
  204. /**
  205. * \relates rows
  206. * \brief Equality operator.
  207. * \details The containers are considered equal if they have the same number of rows and
  208. * they all compare equal, as defined by \ref row_view::operator==.
  209. *
  210. * \par Exception safety
  211. * No-throw guarantee.
  212. *
  213. * \par Complexity
  214. * Linear in `lhs.size() * lhs.num_columns()` and `rhs.size() * rhs.num_columns()`.
  215. */
  216. inline bool operator==(const rows& lhs, const rows& rhs) noexcept { return rows_view(lhs) == rows_view(rhs); }
  217. /**
  218. * \relates rows
  219. * \brief Inequality operator.
  220. *
  221. * \par Exception safety
  222. * No-throw guarantee.
  223. *
  224. * \par Complexity
  225. * Linear in `lhs.size() * lhs.num_columns()` and `rhs.size() * rhs.num_columns()`.
  226. */
  227. inline bool operator!=(const rows& lhs, const rows& rhs) noexcept { return !(lhs == rhs); }
  228. /**
  229. * \relates rows
  230. * \copydoc rows::operator==(const rows&, const rows&)
  231. */
  232. inline bool operator==(const rows_view& lhs, const rows& rhs) noexcept { return lhs == rows_view(rhs); }
  233. /**
  234. * \relates rows
  235. * \copydoc rows::operator!=(const rows&, const rows&)
  236. */
  237. inline bool operator!=(const rows_view& lhs, const rows& rhs) noexcept { return !(lhs == rhs); }
  238. /**
  239. * \relates rows
  240. * \copydoc rows::operator==(const rows&, const rows&)
  241. */
  242. inline bool operator==(const rows& lhs, const rows_view& rhs) noexcept { return rows_view(lhs) == rhs; }
  243. /**
  244. * \relates rows
  245. * \copydoc rows::operator!=(const rows&, const rows&)
  246. */
  247. inline bool operator!=(const rows& lhs, const rows_view& rhs) noexcept { return !(lhs == rhs); }
  248. } // namespace mysql
  249. } // namespace boost
  250. #endif