row.hpp 7.7 KB

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