buffered_stream_storage.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // detail/buffered_stream_storage.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
  11. #define ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/buffer.hpp"
  17. #include "asio/detail/assert.hpp"
  18. #include <cstddef>
  19. #include <cstring>
  20. #include <vector>
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. class buffered_stream_storage
  25. {
  26. public:
  27. // The type of the bytes stored in the buffer.
  28. typedef unsigned char byte_type;
  29. // The type used for offsets into the buffer.
  30. typedef std::size_t size_type;
  31. // Constructor.
  32. explicit buffered_stream_storage(std::size_t buffer_capacity)
  33. : begin_offset_(0),
  34. end_offset_(0),
  35. buffer_(buffer_capacity)
  36. {
  37. }
  38. /// Clear the buffer.
  39. void clear()
  40. {
  41. begin_offset_ = 0;
  42. end_offset_ = 0;
  43. }
  44. // Return a pointer to the beginning of the unread data.
  45. mutable_buffer data()
  46. {
  47. return asio::buffer(buffer_) + begin_offset_;
  48. }
  49. // Return a pointer to the beginning of the unread data.
  50. const_buffer data() const
  51. {
  52. return asio::buffer(buffer_) + begin_offset_;
  53. }
  54. // Is there no unread data in the buffer.
  55. bool empty() const
  56. {
  57. return begin_offset_ == end_offset_;
  58. }
  59. // Return the amount of unread data the is in the buffer.
  60. size_type size() const
  61. {
  62. return end_offset_ - begin_offset_;
  63. }
  64. // Resize the buffer to the specified length.
  65. void resize(size_type length)
  66. {
  67. ASIO_ASSERT(length <= capacity());
  68. if (begin_offset_ + length <= capacity())
  69. {
  70. end_offset_ = begin_offset_ + length;
  71. }
  72. else
  73. {
  74. using namespace std; // For memmove.
  75. memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
  76. end_offset_ = length;
  77. begin_offset_ = 0;
  78. }
  79. }
  80. // Return the maximum size for data in the buffer.
  81. size_type capacity() const
  82. {
  83. return buffer_.size();
  84. }
  85. // Consume multiple bytes from the beginning of the buffer.
  86. void consume(size_type count)
  87. {
  88. ASIO_ASSERT(begin_offset_ + count <= end_offset_);
  89. begin_offset_ += count;
  90. if (empty())
  91. clear();
  92. }
  93. private:
  94. // The offset to the beginning of the unread data.
  95. size_type begin_offset_;
  96. // The offset to the end of the unread data.
  97. size_type end_offset_;
  98. // The data in the buffer.
  99. std::vector<byte_type> buffer_;
  100. };
  101. } // namespace detail
  102. } // namespace asio
  103. #include "asio/detail/pop_options.hpp"
  104. #endif // ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP