Chris@16
|
1 //
|
Chris@16
|
2 // detail/buffered_stream_storage.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/buffer.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/assert.hpp>
|
Chris@16
|
21 #include <cstddef>
|
Chris@16
|
22 #include <cstring>
|
Chris@16
|
23 #include <vector>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace asio {
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30
|
Chris@16
|
31 class buffered_stream_storage
|
Chris@16
|
32 {
|
Chris@16
|
33 public:
|
Chris@16
|
34 // The type of the bytes stored in the buffer.
|
Chris@16
|
35 typedef unsigned char byte_type;
|
Chris@16
|
36
|
Chris@16
|
37 // The type used for offsets into the buffer.
|
Chris@16
|
38 typedef std::size_t size_type;
|
Chris@16
|
39
|
Chris@16
|
40 // Constructor.
|
Chris@16
|
41 explicit buffered_stream_storage(std::size_t buffer_capacity)
|
Chris@16
|
42 : begin_offset_(0),
|
Chris@16
|
43 end_offset_(0),
|
Chris@16
|
44 buffer_(buffer_capacity)
|
Chris@16
|
45 {
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 /// Clear the buffer.
|
Chris@16
|
49 void clear()
|
Chris@16
|
50 {
|
Chris@16
|
51 begin_offset_ = 0;
|
Chris@16
|
52 end_offset_ = 0;
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 // Return a pointer to the beginning of the unread data.
|
Chris@16
|
56 mutable_buffer data()
|
Chris@16
|
57 {
|
Chris@16
|
58 return boost::asio::buffer(buffer_) + begin_offset_;
|
Chris@16
|
59 }
|
Chris@16
|
60
|
Chris@16
|
61 // Return a pointer to the beginning of the unread data.
|
Chris@16
|
62 const_buffer data() const
|
Chris@16
|
63 {
|
Chris@16
|
64 return boost::asio::buffer(buffer_) + begin_offset_;
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 // Is there no unread data in the buffer.
|
Chris@16
|
68 bool empty() const
|
Chris@16
|
69 {
|
Chris@16
|
70 return begin_offset_ == end_offset_;
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 // Return the amount of unread data the is in the buffer.
|
Chris@16
|
74 size_type size() const
|
Chris@16
|
75 {
|
Chris@16
|
76 return end_offset_ - begin_offset_;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 // Resize the buffer to the specified length.
|
Chris@16
|
80 void resize(size_type length)
|
Chris@16
|
81 {
|
Chris@16
|
82 BOOST_ASIO_ASSERT(length <= capacity());
|
Chris@16
|
83 if (begin_offset_ + length <= capacity())
|
Chris@16
|
84 {
|
Chris@16
|
85 end_offset_ = begin_offset_ + length;
|
Chris@16
|
86 }
|
Chris@16
|
87 else
|
Chris@16
|
88 {
|
Chris@16
|
89 using namespace std; // For memmove.
|
Chris@16
|
90 memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
|
Chris@16
|
91 end_offset_ = length;
|
Chris@16
|
92 begin_offset_ = 0;
|
Chris@16
|
93 }
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 // Return the maximum size for data in the buffer.
|
Chris@16
|
97 size_type capacity() const
|
Chris@16
|
98 {
|
Chris@16
|
99 return buffer_.size();
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 // Consume multiple bytes from the beginning of the buffer.
|
Chris@16
|
103 void consume(size_type count)
|
Chris@16
|
104 {
|
Chris@16
|
105 BOOST_ASIO_ASSERT(begin_offset_ + count <= end_offset_);
|
Chris@16
|
106 begin_offset_ += count;
|
Chris@16
|
107 if (empty())
|
Chris@16
|
108 clear();
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 private:
|
Chris@16
|
112 // The offset to the beginning of the unread data.
|
Chris@16
|
113 size_type begin_offset_;
|
Chris@16
|
114
|
Chris@16
|
115 // The offset to the end of the unread data.
|
Chris@16
|
116 size_type end_offset_;
|
Chris@16
|
117
|
Chris@16
|
118 // The data in the buffer.
|
Chris@16
|
119 std::vector<byte_type> buffer_;
|
Chris@16
|
120 };
|
Chris@16
|
121
|
Chris@16
|
122 } // namespace detail
|
Chris@16
|
123 } // namespace asio
|
Chris@16
|
124 } // namespace boost
|
Chris@16
|
125
|
Chris@16
|
126 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
127
|
Chris@16
|
128 #endif // BOOST_ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
|