Chris@16
|
1 //
|
Chris@16
|
2 // basic_streambuf.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_BASIC_STREAMBUF_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_STREAMBUF_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
|
Chris@16
|
20 #if !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
21
|
Chris@16
|
22 #include <algorithm>
|
Chris@16
|
23 #include <cstring>
|
Chris@16
|
24 #include <stdexcept>
|
Chris@16
|
25 #include <streambuf>
|
Chris@16
|
26 #include <vector>
|
Chris@16
|
27 #include <boost/asio/basic_streambuf_fwd.hpp>
|
Chris@16
|
28 #include <boost/asio/buffer.hpp>
|
Chris@16
|
29 #include <boost/asio/detail/limits.hpp>
|
Chris@16
|
30 #include <boost/asio/detail/noncopyable.hpp>
|
Chris@16
|
31 #include <boost/asio/detail/throw_exception.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace asio {
|
Chris@16
|
37
|
Chris@16
|
38 /// Automatically resizable buffer class based on std::streambuf.
|
Chris@16
|
39 /**
|
Chris@16
|
40 * The @c basic_streambuf class is derived from @c std::streambuf to associate
|
Chris@16
|
41 * the streambuf's input and output sequences with one or more character
|
Chris@16
|
42 * arrays. These character arrays are internal to the @c basic_streambuf
|
Chris@16
|
43 * object, but direct access to the array elements is provided to permit them
|
Chris@16
|
44 * to be used efficiently with I/O operations. Characters written to the output
|
Chris@16
|
45 * sequence of a @c basic_streambuf object are appended to the input sequence
|
Chris@16
|
46 * of the same object.
|
Chris@16
|
47 *
|
Chris@16
|
48 * The @c basic_streambuf class's public interface is intended to permit the
|
Chris@16
|
49 * following implementation strategies:
|
Chris@16
|
50 *
|
Chris@16
|
51 * @li A single contiguous character array, which is reallocated as necessary
|
Chris@16
|
52 * to accommodate changes in the size of the character sequence. This is the
|
Chris@16
|
53 * implementation approach currently used in Asio.
|
Chris@16
|
54 *
|
Chris@16
|
55 * @li A sequence of one or more character arrays, where each array is of the
|
Chris@16
|
56 * same size. Additional character array objects are appended to the sequence
|
Chris@16
|
57 * to accommodate changes in the size of the character sequence.
|
Chris@16
|
58 *
|
Chris@16
|
59 * @li A sequence of one or more character arrays of varying sizes. Additional
|
Chris@16
|
60 * character array objects are appended to the sequence to accommodate changes
|
Chris@16
|
61 * in the size of the character sequence.
|
Chris@16
|
62 *
|
Chris@16
|
63 * The constructor for basic_streambuf accepts a @c size_t argument specifying
|
Chris@16
|
64 * the maximum of the sum of the sizes of the input sequence and output
|
Chris@16
|
65 * sequence. During the lifetime of the @c basic_streambuf object, the following
|
Chris@16
|
66 * invariant holds:
|
Chris@16
|
67 * @code size() <= max_size()@endcode
|
Chris@16
|
68 * Any member function that would, if successful, cause the invariant to be
|
Chris@16
|
69 * violated shall throw an exception of class @c std::length_error.
|
Chris@16
|
70 *
|
Chris@16
|
71 * The constructor for @c basic_streambuf takes an Allocator argument. A copy
|
Chris@16
|
72 * of this argument is used for any memory allocation performed, by the
|
Chris@16
|
73 * constructor and by all member functions, during the lifetime of each @c
|
Chris@16
|
74 * basic_streambuf object.
|
Chris@16
|
75 *
|
Chris@16
|
76 * @par Examples
|
Chris@16
|
77 * Writing directly from an streambuf to a socket:
|
Chris@16
|
78 * @code
|
Chris@16
|
79 * boost::asio::streambuf b;
|
Chris@16
|
80 * std::ostream os(&b);
|
Chris@16
|
81 * os << "Hello, World!\n";
|
Chris@16
|
82 *
|
Chris@16
|
83 * // try sending some data in input sequence
|
Chris@16
|
84 * size_t n = sock.send(b.data());
|
Chris@16
|
85 *
|
Chris@16
|
86 * b.consume(n); // sent data is removed from input sequence
|
Chris@16
|
87 * @endcode
|
Chris@16
|
88 *
|
Chris@16
|
89 * Reading from a socket directly into a streambuf:
|
Chris@16
|
90 * @code
|
Chris@16
|
91 * boost::asio::streambuf b;
|
Chris@16
|
92 *
|
Chris@16
|
93 * // reserve 512 bytes in output sequence
|
Chris@16
|
94 * boost::asio::streambuf::mutable_buffers_type bufs = b.prepare(512);
|
Chris@16
|
95 *
|
Chris@16
|
96 * size_t n = sock.receive(bufs);
|
Chris@16
|
97 *
|
Chris@16
|
98 * // received data is "committed" from output sequence to input sequence
|
Chris@16
|
99 * b.commit(n);
|
Chris@16
|
100 *
|
Chris@16
|
101 * std::istream is(&b);
|
Chris@16
|
102 * std::string s;
|
Chris@16
|
103 * is >> s;
|
Chris@16
|
104 * @endcode
|
Chris@16
|
105 */
|
Chris@16
|
106 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
107 template <typename Allocator = std::allocator<char> >
|
Chris@16
|
108 #else
|
Chris@16
|
109 template <typename Allocator>
|
Chris@16
|
110 #endif
|
Chris@16
|
111 class basic_streambuf
|
Chris@16
|
112 : public std::streambuf,
|
Chris@16
|
113 private noncopyable
|
Chris@16
|
114 {
|
Chris@16
|
115 public:
|
Chris@16
|
116 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
117 /// The type used to represent the input sequence as a list of buffers.
|
Chris@16
|
118 typedef implementation_defined const_buffers_type;
|
Chris@16
|
119
|
Chris@16
|
120 /// The type used to represent the output sequence as a list of buffers.
|
Chris@16
|
121 typedef implementation_defined mutable_buffers_type;
|
Chris@16
|
122 #else
|
Chris@16
|
123 typedef boost::asio::const_buffers_1 const_buffers_type;
|
Chris@16
|
124 typedef boost::asio::mutable_buffers_1 mutable_buffers_type;
|
Chris@16
|
125 #endif
|
Chris@16
|
126
|
Chris@16
|
127 /// Construct a basic_streambuf object.
|
Chris@16
|
128 /**
|
Chris@16
|
129 * Constructs a streambuf with the specified maximum size. The initial size
|
Chris@16
|
130 * of the streambuf's input sequence is 0.
|
Chris@16
|
131 */
|
Chris@16
|
132 explicit basic_streambuf(
|
Chris@16
|
133 std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
|
Chris@16
|
134 const Allocator& allocator = Allocator())
|
Chris@16
|
135 : max_size_(maximum_size),
|
Chris@16
|
136 buffer_(allocator)
|
Chris@16
|
137 {
|
Chris@16
|
138 std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);
|
Chris@16
|
139 buffer_.resize((std::max<std::size_t>)(pend, 1));
|
Chris@16
|
140 setg(&buffer_[0], &buffer_[0], &buffer_[0]);
|
Chris@16
|
141 setp(&buffer_[0], &buffer_[0] + pend);
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 /// Get the size of the input sequence.
|
Chris@16
|
145 /**
|
Chris@16
|
146 * @returns The size of the input sequence. The value is equal to that
|
Chris@16
|
147 * calculated for @c s in the following code:
|
Chris@16
|
148 * @code
|
Chris@16
|
149 * size_t s = 0;
|
Chris@16
|
150 * const_buffers_type bufs = data();
|
Chris@16
|
151 * const_buffers_type::const_iterator i = bufs.begin();
|
Chris@16
|
152 * while (i != bufs.end())
|
Chris@16
|
153 * {
|
Chris@16
|
154 * const_buffer buf(*i++);
|
Chris@16
|
155 * s += buffer_size(buf);
|
Chris@16
|
156 * }
|
Chris@16
|
157 * @endcode
|
Chris@16
|
158 */
|
Chris@16
|
159 std::size_t size() const
|
Chris@16
|
160 {
|
Chris@16
|
161 return pptr() - gptr();
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 /// Get the maximum size of the basic_streambuf.
|
Chris@16
|
165 /**
|
Chris@16
|
166 * @returns The allowed maximum of the sum of the sizes of the input sequence
|
Chris@16
|
167 * and output sequence.
|
Chris@16
|
168 */
|
Chris@16
|
169 std::size_t max_size() const
|
Chris@16
|
170 {
|
Chris@16
|
171 return max_size_;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 /// Get a list of buffers that represents the input sequence.
|
Chris@16
|
175 /**
|
Chris@16
|
176 * @returns An object of type @c const_buffers_type that satisfies
|
Chris@16
|
177 * ConstBufferSequence requirements, representing all character arrays in the
|
Chris@16
|
178 * input sequence.
|
Chris@16
|
179 *
|
Chris@16
|
180 * @note The returned object is invalidated by any @c basic_streambuf member
|
Chris@16
|
181 * function that modifies the input sequence or output sequence.
|
Chris@16
|
182 */
|
Chris@16
|
183 const_buffers_type data() const
|
Chris@16
|
184 {
|
Chris@16
|
185 return boost::asio::buffer(boost::asio::const_buffer(gptr(),
|
Chris@16
|
186 (pptr() - gptr()) * sizeof(char_type)));
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 /// Get a list of buffers that represents the output sequence, with the given
|
Chris@16
|
190 /// size.
|
Chris@16
|
191 /**
|
Chris@16
|
192 * Ensures that the output sequence can accommodate @c n characters,
|
Chris@16
|
193 * reallocating character array objects as necessary.
|
Chris@16
|
194 *
|
Chris@16
|
195 * @returns An object of type @c mutable_buffers_type that satisfies
|
Chris@16
|
196 * MutableBufferSequence requirements, representing character array objects
|
Chris@16
|
197 * at the start of the output sequence such that the sum of the buffer sizes
|
Chris@16
|
198 * is @c n.
|
Chris@16
|
199 *
|
Chris@16
|
200 * @throws std::length_error If <tt>size() + n > max_size()</tt>.
|
Chris@16
|
201 *
|
Chris@16
|
202 * @note The returned object is invalidated by any @c basic_streambuf member
|
Chris@16
|
203 * function that modifies the input sequence or output sequence.
|
Chris@16
|
204 */
|
Chris@16
|
205 mutable_buffers_type prepare(std::size_t n)
|
Chris@16
|
206 {
|
Chris@16
|
207 reserve(n);
|
Chris@16
|
208 return boost::asio::buffer(boost::asio::mutable_buffer(
|
Chris@16
|
209 pptr(), n * sizeof(char_type)));
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /// Move characters from the output sequence to the input sequence.
|
Chris@16
|
213 /**
|
Chris@16
|
214 * Appends @c n characters from the start of the output sequence to the input
|
Chris@16
|
215 * sequence. The beginning of the output sequence is advanced by @c n
|
Chris@16
|
216 * characters.
|
Chris@16
|
217 *
|
Chris@16
|
218 * Requires a preceding call <tt>prepare(x)</tt> where <tt>x >= n</tt>, and
|
Chris@16
|
219 * no intervening operations that modify the input or output sequence.
|
Chris@16
|
220 *
|
Chris@101
|
221 * @note If @c n is greater than the size of the output sequence, the entire
|
Chris@101
|
222 * output sequence is moved to the input sequence and no error is issued.
|
Chris@16
|
223 */
|
Chris@16
|
224 void commit(std::size_t n)
|
Chris@16
|
225 {
|
Chris@16
|
226 if (pptr() + n > epptr())
|
Chris@16
|
227 n = epptr() - pptr();
|
Chris@16
|
228 pbump(static_cast<int>(n));
|
Chris@16
|
229 setg(eback(), gptr(), pptr());
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 /// Remove characters from the input sequence.
|
Chris@16
|
233 /**
|
Chris@16
|
234 * Removes @c n characters from the beginning of the input sequence.
|
Chris@16
|
235 *
|
Chris@101
|
236 * @note If @c n is greater than the size of the input sequence, the entire
|
Chris@101
|
237 * input sequence is consumed and no error is issued.
|
Chris@16
|
238 */
|
Chris@16
|
239 void consume(std::size_t n)
|
Chris@16
|
240 {
|
Chris@16
|
241 if (egptr() < pptr())
|
Chris@16
|
242 setg(&buffer_[0], gptr(), pptr());
|
Chris@16
|
243 if (gptr() + n > pptr())
|
Chris@16
|
244 n = pptr() - gptr();
|
Chris@16
|
245 gbump(static_cast<int>(n));
|
Chris@16
|
246 }
|
Chris@16
|
247
|
Chris@16
|
248 protected:
|
Chris@16
|
249 enum { buffer_delta = 128 };
|
Chris@16
|
250
|
Chris@16
|
251 /// Override std::streambuf behaviour.
|
Chris@16
|
252 /**
|
Chris@16
|
253 * Behaves according to the specification of @c std::streambuf::underflow().
|
Chris@16
|
254 */
|
Chris@16
|
255 int_type underflow()
|
Chris@16
|
256 {
|
Chris@16
|
257 if (gptr() < pptr())
|
Chris@16
|
258 {
|
Chris@16
|
259 setg(&buffer_[0], gptr(), pptr());
|
Chris@16
|
260 return traits_type::to_int_type(*gptr());
|
Chris@16
|
261 }
|
Chris@16
|
262 else
|
Chris@16
|
263 {
|
Chris@16
|
264 return traits_type::eof();
|
Chris@16
|
265 }
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 /// Override std::streambuf behaviour.
|
Chris@16
|
269 /**
|
Chris@16
|
270 * Behaves according to the specification of @c std::streambuf::overflow(),
|
Chris@16
|
271 * with the specialisation that @c std::length_error is thrown if appending
|
Chris@16
|
272 * the character to the input sequence would require the condition
|
Chris@16
|
273 * <tt>size() > max_size()</tt> to be true.
|
Chris@16
|
274 */
|
Chris@16
|
275 int_type overflow(int_type c)
|
Chris@16
|
276 {
|
Chris@16
|
277 if (!traits_type::eq_int_type(c, traits_type::eof()))
|
Chris@16
|
278 {
|
Chris@16
|
279 if (pptr() == epptr())
|
Chris@16
|
280 {
|
Chris@16
|
281 std::size_t buffer_size = pptr() - gptr();
|
Chris@16
|
282 if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
|
Chris@16
|
283 {
|
Chris@16
|
284 reserve(max_size_ - buffer_size);
|
Chris@16
|
285 }
|
Chris@16
|
286 else
|
Chris@16
|
287 {
|
Chris@16
|
288 reserve(buffer_delta);
|
Chris@16
|
289 }
|
Chris@16
|
290 }
|
Chris@16
|
291
|
Chris@16
|
292 *pptr() = traits_type::to_char_type(c);
|
Chris@16
|
293 pbump(1);
|
Chris@16
|
294 return c;
|
Chris@16
|
295 }
|
Chris@16
|
296
|
Chris@16
|
297 return traits_type::not_eof(c);
|
Chris@16
|
298 }
|
Chris@16
|
299
|
Chris@16
|
300 void reserve(std::size_t n)
|
Chris@16
|
301 {
|
Chris@16
|
302 // Get current stream positions as offsets.
|
Chris@16
|
303 std::size_t gnext = gptr() - &buffer_[0];
|
Chris@16
|
304 std::size_t pnext = pptr() - &buffer_[0];
|
Chris@16
|
305 std::size_t pend = epptr() - &buffer_[0];
|
Chris@16
|
306
|
Chris@16
|
307 // Check if there is already enough space in the put area.
|
Chris@16
|
308 if (n <= pend - pnext)
|
Chris@16
|
309 {
|
Chris@16
|
310 return;
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313 // Shift existing contents of get area to start of buffer.
|
Chris@16
|
314 if (gnext > 0)
|
Chris@16
|
315 {
|
Chris@16
|
316 pnext -= gnext;
|
Chris@16
|
317 std::memmove(&buffer_[0], &buffer_[0] + gnext, pnext);
|
Chris@16
|
318 }
|
Chris@16
|
319
|
Chris@16
|
320 // Ensure buffer is large enough to hold at least the specified size.
|
Chris@16
|
321 if (n > pend - pnext)
|
Chris@16
|
322 {
|
Chris@16
|
323 if (n <= max_size_ && pnext <= max_size_ - n)
|
Chris@16
|
324 {
|
Chris@16
|
325 pend = pnext + n;
|
Chris@16
|
326 buffer_.resize((std::max<std::size_t>)(pend, 1));
|
Chris@16
|
327 }
|
Chris@16
|
328 else
|
Chris@16
|
329 {
|
Chris@16
|
330 std::length_error ex("boost::asio::streambuf too long");
|
Chris@16
|
331 boost::asio::detail::throw_exception(ex);
|
Chris@16
|
332 }
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 // Update stream positions.
|
Chris@16
|
336 setg(&buffer_[0], &buffer_[0], &buffer_[0] + pnext);
|
Chris@16
|
337 setp(&buffer_[0] + pnext, &buffer_[0] + pend);
|
Chris@16
|
338 }
|
Chris@16
|
339
|
Chris@16
|
340 private:
|
Chris@16
|
341 std::size_t max_size_;
|
Chris@16
|
342 std::vector<char_type, Allocator> buffer_;
|
Chris@16
|
343
|
Chris@16
|
344 // Helper function to get the preferred size for reading data.
|
Chris@16
|
345 friend std::size_t read_size_helper(
|
Chris@16
|
346 basic_streambuf& sb, std::size_t max_size)
|
Chris@16
|
347 {
|
Chris@16
|
348 return std::min<std::size_t>(
|
Chris@16
|
349 std::max<std::size_t>(512, sb.buffer_.capacity() - sb.size()),
|
Chris@16
|
350 std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
|
Chris@16
|
351 }
|
Chris@16
|
352 };
|
Chris@16
|
353
|
Chris@16
|
354 // Helper function to get the preferred size for reading data. Used for any
|
Chris@16
|
355 // user-provided specialisations of basic_streambuf.
|
Chris@16
|
356 template <typename Allocator>
|
Chris@16
|
357 inline std::size_t read_size_helper(
|
Chris@16
|
358 basic_streambuf<Allocator>& sb, std::size_t max_size)
|
Chris@16
|
359 {
|
Chris@16
|
360 return std::min<std::size_t>(512,
|
Chris@16
|
361 std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
|
Chris@16
|
362 }
|
Chris@16
|
363
|
Chris@16
|
364 } // namespace asio
|
Chris@16
|
365 } // namespace boost
|
Chris@16
|
366
|
Chris@16
|
367 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
368
|
Chris@16
|
369 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
|
Chris@16
|
370
|
Chris@16
|
371 #endif // BOOST_ASIO_BASIC_STREAMBUF_HPP
|