Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:37

0001 //
0002 // detail/buffered_stream_storage.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
0012 #define BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/buffer.hpp>
0020 #include <boost/asio/detail/assert.hpp>
0021 #include <cstddef>
0022 #include <cstring>
0023 #include <vector>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 class buffered_stream_storage
0032 {
0033 public:
0034   // The type of the bytes stored in the buffer.
0035   typedef unsigned char byte_type;
0036 
0037   // The type used for offsets into the buffer.
0038   typedef std::size_t size_type;
0039 
0040   // Constructor.
0041   explicit buffered_stream_storage(std::size_t buffer_capacity)
0042     : begin_offset_(0),
0043       end_offset_(0),
0044       buffer_(buffer_capacity)
0045   {
0046   }
0047 
0048   /// Clear the buffer.
0049   void clear()
0050   {
0051     begin_offset_ = 0;
0052     end_offset_ = 0;
0053   }
0054 
0055   // Return a pointer to the beginning of the unread data.
0056   mutable_buffer data()
0057   {
0058     return boost::asio::buffer(buffer_) + begin_offset_;
0059   }
0060 
0061   // Return a pointer to the beginning of the unread data.
0062   const_buffer data() const
0063   {
0064     return boost::asio::buffer(buffer_) + begin_offset_;
0065   }
0066 
0067   // Is there no unread data in the buffer.
0068   bool empty() const
0069   {
0070     return begin_offset_ == end_offset_;
0071   }
0072 
0073   // Return the amount of unread data the is in the buffer.
0074   size_type size() const
0075   {
0076     return end_offset_ - begin_offset_;
0077   }
0078 
0079   // Resize the buffer to the specified length.
0080   void resize(size_type length)
0081   {
0082     BOOST_ASIO_ASSERT(length <= capacity());
0083     if (begin_offset_ + length <= capacity())
0084     {
0085       end_offset_ = begin_offset_ + length;
0086     }
0087     else
0088     {
0089       using namespace std; // For memmove.
0090       memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
0091       end_offset_ = length;
0092       begin_offset_ = 0;
0093     }
0094   }
0095 
0096   // Return the maximum size for data in the buffer.
0097   size_type capacity() const
0098   {
0099     return buffer_.size();
0100   }
0101 
0102   // Consume multiple bytes from the beginning of the buffer.
0103   void consume(size_type count)
0104   {
0105     BOOST_ASIO_ASSERT(begin_offset_ + count <= end_offset_);
0106     begin_offset_ += count;
0107     if (empty())
0108       clear();
0109   }
0110 
0111 private:
0112   // The offset to the beginning of the unread data.
0113   size_type begin_offset_;
0114 
0115   // The offset to the end of the unread data.
0116   size_type end_offset_;
0117   
0118   // The data in the buffer.
0119   std::vector<byte_type> buffer_;
0120 };
0121 
0122 } // namespace detail
0123 } // namespace asio
0124 } // namespace boost
0125 
0126 #include <boost/asio/detail/pop_options.hpp>
0127 
0128 #endif // BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP