static_buffer.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_IMPL_INTERNAL_PROTOCOL_STATIC_BUFFER_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_PROTOCOL_STATIC_BUFFER_HPP
  9. // A very simplified variable-length buffer with fixed max-size
  10. #include <boost/assert.hpp>
  11. #include <boost/core/span.hpp>
  12. #include <array>
  13. #include <cstring>
  14. namespace boost {
  15. namespace mysql {
  16. namespace detail {
  17. template <std::size_t max_size>
  18. class static_buffer
  19. {
  20. std::array<std::uint8_t, max_size> buffer_{};
  21. std::size_t size_{};
  22. public:
  23. static_buffer() noexcept = default;
  24. span<const std::uint8_t> to_span() const noexcept { return {buffer_.data(), size_}; }
  25. void append(const void* data, std::size_t data_size) noexcept
  26. {
  27. std::size_t new_size = size_ + data_size;
  28. BOOST_ASSERT(new_size <= max_size);
  29. std::memcpy(buffer_.data() + size_, data, data_size);
  30. size_ = new_size;
  31. }
  32. void clear() noexcept { size_ = 0; }
  33. };
  34. } // namespace detail
  35. } // namespace mysql
  36. } // namespace boost
  37. #endif