rows.hpp 8.4 KB

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