buffered_stream_storage.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // detail/buffered_stream_storage.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/buffer.hpp>
  17. #include <boost/asio/detail/assert.hpp>
  18. #include <cstddef>
  19. #include <cstring>
  20. #include <vector>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. class buffered_stream_storage
  26. {
  27. public:
  28. // The type of the bytes stored in the buffer.
  29. typedef unsigned char byte_type;
  30. // The type used for offsets into the buffer.
  31. typedef std::size_t size_type;
  32. // Constructor.
  33. explicit buffered_stream_storage(std::size_t buffer_capacity)
  34. : begin_offset_(0),
  35. end_offset_(0),
  36. buffer_(buffer_capacity)
  37. {
  38. }
  39. /// Clear the buffer.
  40. void clear()
  41. {
  42. begin_offset_ = 0;
  43. end_offset_ = 0;
  44. }
  45. // Return a pointer to the beginning of the unread data.
  46. mutable_buffer data()
  47. {
  48. return boost::asio::buffer(buffer_) + begin_offset_;
  49. }
  50. // Return a pointer to the beginning of the unread data.
  51. const_buffer data() const
  52. {
  53. return boost::asio::buffer(buffer_) + begin_offset_;
  54. }
  55. // Is there no unread data in the buffer.
  56. bool empty() const
  57. {
  58. return begin_offset_ == end_offset_;
  59. }
  60. // Return the amount of unread data the is in the buffer.
  61. size_type size() const
  62. {
  63. return end_offset_ - begin_offset_;
  64. }
  65. // Resize the buffer to the specified length.
  66. void resize(size_type length)
  67. {
  68. BOOST_ASIO_ASSERT(length <= capacity());
  69. if (begin_offset_ + length <= capacity())
  70. {
  71. end_offset_ = begin_offset_ + length;
  72. }
  73. else
  74. {
  75. using namespace std; // For memmove.
  76. memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
  77. end_offset_ = length;
  78. begin_offset_ = 0;
  79. }
  80. }
  81. // Return the maximum size for data in the buffer.
  82. size_type capacity() const
  83. {
  84. return buffer_.size();
  85. }
  86. // Consume multiple bytes from the beginning of the buffer.
  87. void consume(size_type count)
  88. {
  89. BOOST_ASIO_ASSERT(begin_offset_ + count <= end_offset_);
  90. begin_offset_ += count;
  91. if (empty())
  92. clear();
  93. }
  94. private:
  95. // The offset to the beginning of the unread data.
  96. size_type begin_offset_;
  97. // The offset to the end of the unread data.
  98. size_type end_offset_;
  99. // The data in the buffer.
  100. std::vector<byte_type> buffer_;
  101. };
  102. } // namespace detail
  103. } // namespace asio
  104. } // namespace boost
  105. #include <boost/asio/detail/pop_options.hpp>
  106. #endif // BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP