row.hpp 8.0 KB

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