buffered_stream_storage.hpp 2.9 KB

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