2 // detail/buffered_stream_storage.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
12 #define BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/buffer.hpp>
20 #include <boost/asio/detail/assert.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 class buffered_stream_storage
34 // The type of the bytes stored in the buffer.
35 typedef unsigned char byte_type;
37 // The type used for offsets into the buffer.
38 typedef std::size_t size_type;
41 explicit buffered_stream_storage(std::size_t buffer_capacity)
44 buffer_(buffer_capacity)
55 // Return a pointer to the beginning of the unread data.
58 return boost::asio::buffer(buffer_) + begin_offset_;
61 // Return a pointer to the beginning of the unread data.
62 const_buffer data() const
64 return boost::asio::buffer(buffer_) + begin_offset_;
67 // Is there no unread data in the buffer.
70 return begin_offset_ == end_offset_;
73 // Return the amount of unread data the is in the buffer.
74 size_type size() const
76 return end_offset_ - begin_offset_;
79 // Resize the buffer to the specified length.
80 void resize(size_type length)
82 BOOST_ASIO_ASSERT(length <= capacity());
83 if (begin_offset_ + length <= capacity())
85 end_offset_ = begin_offset_ + length;
89 using namespace std; // For memmove.
90 memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
96 // Return the maximum size for data in the buffer.
97 size_type capacity() const
99 return buffer_.size();
102 // Consume multiple bytes from the beginning of the buffer.
103 void consume(size_type count)
105 BOOST_ASIO_ASSERT(begin_offset_ + count <= end_offset_);
106 begin_offset_ += count;
112 // The offset to the beginning of the unread data.
113 size_type begin_offset_;
115 // The offset to the end of the unread data.
116 size_type end_offset_;
118 // The data in the buffer.
119 std::vector<byte_type> buffer_;
122 } // namespace detail
126 #include <boost/asio/detail/pop_options.hpp>
128 #endif // BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP