read_buffer.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (c) 2018-2025 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_READ_BUFFER_HPP
  7. #define BOOST_REDIS_READ_BUFFER_HPP
  8. #include <boost/core/span.hpp>
  9. #include <boost/system/error_code.hpp>
  10. #include <cstddef>
  11. #include <string_view>
  12. #include <utility>
  13. #include <vector>
  14. namespace boost::redis::detail {
  15. class read_buffer {
  16. public:
  17. using span_type = span<char>;
  18. struct consume_result {
  19. std::size_t consumed;
  20. std::size_t rotated;
  21. };
  22. // See config.hpp for the meaning of these parameters.
  23. struct config {
  24. std::size_t read_buffer_append_size = 4096u;
  25. std::size_t max_read_size = static_cast<std::size_t>(-1);
  26. };
  27. // Prepare the buffer to receive more data.
  28. [[nodiscard]]
  29. auto prepare() -> system::error_code;
  30. [[nodiscard]]
  31. auto get_prepared() noexcept -> span_type;
  32. void commit(std::size_t read_size);
  33. [[nodiscard]]
  34. auto get_commited() const noexcept -> std::string_view;
  35. void clear();
  36. // Consumes committed data by rotating the remaining data to the
  37. // front of the buffer.
  38. auto consume(std::size_t size) -> consume_result;
  39. void reserve(std::size_t n);
  40. friend bool operator==(read_buffer const& lhs, read_buffer const& rhs);
  41. friend bool operator!=(read_buffer const& lhs, read_buffer const& rhs);
  42. void set_config(config const& cfg) noexcept { cfg_ = cfg; };
  43. private:
  44. config cfg_ = config{};
  45. std::vector<char> buffer_;
  46. std::size_t append_buf_begin_ = 0;
  47. };
  48. } // namespace boost::redis::detail
  49. #endif // BOOST_REDIS_READ_BUFFER_HPP