row_impl.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_DETAIL_ROW_IMPL_HPP
  8. #define BOOST_MYSQL_DETAIL_ROW_IMPL_HPP
  9. #include <boost/mysql/field_view.hpp>
  10. #include <boost/mysql/detail/config.hpp>
  11. #include <boost/core/span.hpp>
  12. #include <cstddef>
  13. #include <vector>
  14. namespace boost {
  15. namespace mysql {
  16. namespace detail {
  17. // Adds num_fields default-constructed fields to the vector, return pointer to the first
  18. // allocated value. Used to allocate fields before deserialization
  19. inline span<field_view> add_fields(std::vector<field_view>& storage, std::size_t num_fields)
  20. {
  21. std::size_t old_size = storage.size();
  22. storage.resize(old_size + num_fields);
  23. return span<field_view>(storage.data() + old_size, num_fields);
  24. }
  25. // A field_view vector with strings pointing into a
  26. // single character buffer. Used to implement owning row types
  27. class row_impl
  28. {
  29. public:
  30. row_impl() = default;
  31. BOOST_MYSQL_DECL
  32. row_impl(const row_impl&);
  33. row_impl(row_impl&&) = default;
  34. BOOST_MYSQL_DECL
  35. row_impl& operator=(const row_impl&);
  36. row_impl& operator=(row_impl&&) = default;
  37. ~row_impl() = default;
  38. // Copies the given span into *this
  39. BOOST_MYSQL_DECL
  40. row_impl(const field_view* fields, std::size_t size);
  41. // Copies the given span into *this, used by row/rows in assignment from view
  42. BOOST_MYSQL_DECL
  43. void assign(const field_view* fields, std::size_t size);
  44. // Adds new default constructed fields to provide storage to deserialization
  45. span<field_view> add_fields(std::size_t num_fields)
  46. {
  47. return ::boost::mysql::detail::add_fields(fields_, num_fields);
  48. }
  49. // Saves strings in the [first, first+num_fields) range into the string buffer, used by execute
  50. BOOST_MYSQL_DECL
  51. void copy_strings_as_offsets(std::size_t first, std::size_t num_fields);
  52. // Restores any offsets into string views, used by execute
  53. BOOST_MYSQL_DECL
  54. void offsets_to_string_views();
  55. const std::vector<field_view>& fields() const noexcept { return fields_; }
  56. void clear() noexcept
  57. {
  58. fields_.clear();
  59. string_buffer_.clear();
  60. }
  61. private:
  62. std::vector<field_view> fields_;
  63. std::vector<unsigned char> string_buffer_;
  64. };
  65. } // namespace detail
  66. } // namespace mysql
  67. } // namespace boost
  68. #ifdef BOOST_MYSQL_HEADER_ONLY
  69. #include <boost/mysql/impl/row_impl.ipp>
  70. #endif
  71. #endif