buffered_stream_storage.hpp
Go to the documentation of this file.
00001 //
00002 // buffered_stream_storage.hpp
00003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #ifndef ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
00012 #define ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
00013 
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017 
00018 #include "asio/detail/push_options.hpp"
00019 
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include <cassert>
00023 #include <cstddef>
00024 #include <cstring>
00025 #include <vector>
00026 #include "asio/detail/pop_options.hpp"
00027 
00028 namespace asio {
00029 namespace detail {
00030 
00031 class buffered_stream_storage
00032 {
00033 public:
00034   // The type of the bytes stored in the buffer.
00035   typedef unsigned char byte_type;
00036 
00037   // The type used for offsets into the buffer.
00038   typedef std::size_t size_type;
00039 
00040   // Constructor.
00041   explicit buffered_stream_storage(std::size_t capacity)
00042     : begin_offset_(0),
00043       end_offset_(0),
00044       buffer_(capacity)
00045   {
00046   }
00047 
00049   void clear()
00050   {
00051     begin_offset_ = 0;
00052     end_offset_ = 0;
00053   }
00054 
00055   // Return a pointer to the beginning of the unread data.
00056   byte_type* data()
00057   {
00058     return &buffer_[0] + begin_offset_;
00059   }
00060 
00061   // Return a pointer to the beginning of the unread data.
00062   const byte_type* data() const
00063   {
00064     return &buffer_[0] + begin_offset_;
00065   }
00066 
00067   // Is there no unread data in the buffer.
00068   bool empty() const
00069   {
00070     return begin_offset_ == end_offset_;
00071   }
00072 
00073   // Return the amount of unread data the is in the buffer.
00074   size_type size() const
00075   {
00076     return end_offset_ - begin_offset_;
00077   }
00078 
00079   // Resize the buffer to the specified length.
00080   void resize(size_type length)
00081   {
00082     assert(length <= capacity());
00083     if (begin_offset_ + length <= capacity())
00084     {
00085       end_offset_ = begin_offset_ + length;
00086     }
00087     else
00088     {
00089       using namespace std; // For memmove.
00090       memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
00091       end_offset_ = length;
00092       begin_offset_ = 0;
00093     }
00094   }
00095 
00096   // Return the maximum size for data in the buffer.
00097   size_type capacity() const
00098   {
00099     return buffer_.size();
00100   }
00101 
00102   // Consume multiple bytes from the beginning of the buffer.
00103   void consume(size_type count)
00104   {
00105     assert(begin_offset_ + count <= end_offset_);
00106     begin_offset_ += count;
00107     if (empty())
00108       clear();
00109   }
00110 
00111 private:
00112   // The offset to the beginning of the unread data.
00113   size_type begin_offset_;
00114 
00115   // The offset to the end of the unread data.
00116   size_type end_offset_;
00117   
00118   // The data in the buffer.
00119   std::vector<byte_type> buffer_;
00120 };
00121 
00122 } // namespace detail
00123 } // namespace asio
00124 
00125 #include "asio/detail/pop_options.hpp"
00126 
00127 #endif // ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39