Chris@16: // Chris@16: // detail/buffered_stream_storage.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP Chris@16: #define BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class buffered_stream_storage Chris@16: { Chris@16: public: Chris@16: // The type of the bytes stored in the buffer. Chris@16: typedef unsigned char byte_type; Chris@16: Chris@16: // The type used for offsets into the buffer. Chris@16: typedef std::size_t size_type; Chris@16: Chris@16: // Constructor. Chris@16: explicit buffered_stream_storage(std::size_t buffer_capacity) Chris@16: : begin_offset_(0), Chris@16: end_offset_(0), Chris@16: buffer_(buffer_capacity) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Clear the buffer. Chris@16: void clear() Chris@16: { Chris@16: begin_offset_ = 0; Chris@16: end_offset_ = 0; Chris@16: } Chris@16: Chris@16: // Return a pointer to the beginning of the unread data. Chris@16: mutable_buffer data() Chris@16: { Chris@16: return boost::asio::buffer(buffer_) + begin_offset_; Chris@16: } Chris@16: Chris@16: // Return a pointer to the beginning of the unread data. Chris@16: const_buffer data() const Chris@16: { Chris@16: return boost::asio::buffer(buffer_) + begin_offset_; Chris@16: } Chris@16: Chris@16: // Is there no unread data in the buffer. Chris@16: bool empty() const Chris@16: { Chris@16: return begin_offset_ == end_offset_; Chris@16: } Chris@16: Chris@16: // Return the amount of unread data the is in the buffer. Chris@16: size_type size() const Chris@16: { Chris@16: return end_offset_ - begin_offset_; Chris@16: } Chris@16: Chris@16: // Resize the buffer to the specified length. Chris@16: void resize(size_type length) Chris@16: { Chris@16: BOOST_ASIO_ASSERT(length <= capacity()); Chris@16: if (begin_offset_ + length <= capacity()) Chris@16: { Chris@16: end_offset_ = begin_offset_ + length; Chris@16: } Chris@16: else Chris@16: { Chris@16: using namespace std; // For memmove. Chris@16: memmove(&buffer_[0], &buffer_[0] + begin_offset_, size()); Chris@16: end_offset_ = length; Chris@16: begin_offset_ = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: // Return the maximum size for data in the buffer. Chris@16: size_type capacity() const Chris@16: { Chris@16: return buffer_.size(); Chris@16: } Chris@16: Chris@16: // Consume multiple bytes from the beginning of the buffer. Chris@16: void consume(size_type count) Chris@16: { Chris@16: BOOST_ASIO_ASSERT(begin_offset_ + count <= end_offset_); Chris@16: begin_offset_ += count; Chris@16: if (empty()) Chris@16: clear(); Chris@16: } Chris@16: Chris@16: private: Chris@16: // The offset to the beginning of the unread data. Chris@16: size_type begin_offset_; Chris@16: Chris@16: // The offset to the end of the unread data. Chris@16: size_type end_offset_; Chris@16: Chris@16: // The data in the buffer. Chris@16: std::vector buffer_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP