fix doc example typo
[boost.git] / boost / asio / buffered_stream.hpp
blobe1b8d38d750bf242d263b917e23fce5309aa4b41
1 //
2 // buffered_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
11 #ifndef BOOST_ASIO_BUFFERED_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_STREAM_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <cstddef>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #include <boost/asio/buffered_read_stream.hpp>
26 #include <boost/asio/buffered_write_stream.hpp>
27 #include <boost/asio/buffered_stream_fwd.hpp>
28 #include <boost/asio/error.hpp>
29 #include <boost/asio/io_service.hpp>
30 #include <boost/asio/detail/noncopyable.hpp>
32 namespace boost {
33 namespace asio {
35 /// Adds buffering to the read- and write-related operations of a stream.
36 /**
37 * The buffered_stream class template can be used to add buffering to the
38 * synchronous and asynchronous read and write operations of a stream.
40 * @par Thread Safety
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
44 * @par Concepts:
45 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
47 template <typename Stream>
48 class buffered_stream
49 : private noncopyable
51 public:
52 /// The type of the next layer.
53 typedef typename boost::remove_reference<Stream>::type next_layer_type;
55 /// The type of the lowest layer.
56 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
58 /// Construct, passing the specified argument to initialise the next layer.
59 template <typename Arg>
60 explicit buffered_stream(Arg& a)
61 : inner_stream_impl_(a),
62 stream_impl_(inner_stream_impl_)
66 /// Construct, passing the specified argument to initialise the next layer.
67 template <typename Arg>
68 explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
69 std::size_t write_buffer_size)
70 : inner_stream_impl_(a, write_buffer_size),
71 stream_impl_(inner_stream_impl_, read_buffer_size)
75 /// Get a reference to the next layer.
76 next_layer_type& next_layer()
78 return stream_impl_.next_layer().next_layer();
81 /// Get a reference to the lowest layer.
82 lowest_layer_type& lowest_layer()
84 return stream_impl_.lowest_layer();
87 /// Get a const reference to the lowest layer.
88 const lowest_layer_type& lowest_layer() const
90 return stream_impl_.lowest_layer();
93 /// (Deprecated: use get_io_service().) Get the io_service associated with
94 /// the object.
95 boost::asio::io_service& io_service()
97 return stream_impl_.get_io_service();
100 /// Get the io_service associated with the object.
101 boost::asio::io_service& get_io_service()
103 return stream_impl_.get_io_service();
106 /// Close the stream.
107 void close()
109 stream_impl_.close();
112 /// Close the stream.
113 boost::system::error_code close(boost::system::error_code& ec)
115 return stream_impl_.close(ec);
118 /// Flush all data from the buffer to the next layer. Returns the number of
119 /// bytes written to the next layer on the last write operation. Throws an
120 /// exception on failure.
121 std::size_t flush()
123 return stream_impl_.next_layer().flush();
126 /// Flush all data from the buffer to the next layer. Returns the number of
127 /// bytes written to the next layer on the last write operation, or 0 if an
128 /// error occurred.
129 std::size_t flush(boost::system::error_code& ec)
131 return stream_impl_.next_layer().flush(ec);
134 /// Start an asynchronous flush.
135 template <typename WriteHandler>
136 void async_flush(WriteHandler handler)
138 return stream_impl_.next_layer().async_flush(handler);
141 /// Write the given data to the stream. Returns the number of bytes written.
142 /// Throws an exception on failure.
143 template <typename ConstBufferSequence>
144 std::size_t write_some(const ConstBufferSequence& buffers)
146 return stream_impl_.write_some(buffers);
149 /// Write the given data to the stream. Returns the number of bytes written,
150 /// or 0 if an error occurred.
151 template <typename ConstBufferSequence>
152 std::size_t write_some(const ConstBufferSequence& buffers,
153 boost::system::error_code& ec)
155 return stream_impl_.write_some(buffers, ec);
158 /// Start an asynchronous write. The data being written must be valid for the
159 /// lifetime of the asynchronous operation.
160 template <typename ConstBufferSequence, typename WriteHandler>
161 void async_write_some(const ConstBufferSequence& buffers,
162 WriteHandler handler)
164 stream_impl_.async_write_some(buffers, handler);
167 /// Fill the buffer with some data. Returns the number of bytes placed in the
168 /// buffer as a result of the operation. Throws an exception on failure.
169 std::size_t fill()
171 return stream_impl_.fill();
174 /// Fill the buffer with some data. Returns the number of bytes placed in the
175 /// buffer as a result of the operation, or 0 if an error occurred.
176 std::size_t fill(boost::system::error_code& ec)
178 return stream_impl_.fill(ec);
181 /// Start an asynchronous fill.
182 template <typename ReadHandler>
183 void async_fill(ReadHandler handler)
185 stream_impl_.async_fill(handler);
188 /// Read some data from the stream. Returns the number of bytes read. Throws
189 /// an exception on failure.
190 template <typename MutableBufferSequence>
191 std::size_t read_some(const MutableBufferSequence& buffers)
193 return stream_impl_.read_some(buffers);
196 /// Read some data from the stream. Returns the number of bytes read or 0 if
197 /// an error occurred.
198 template <typename MutableBufferSequence>
199 std::size_t read_some(const MutableBufferSequence& buffers,
200 boost::system::error_code& ec)
202 return stream_impl_.read_some(buffers, ec);
205 /// Start an asynchronous read. The buffer into which the data will be read
206 /// must be valid for the lifetime of the asynchronous operation.
207 template <typename MutableBufferSequence, typename ReadHandler>
208 void async_read_some(const MutableBufferSequence& buffers,
209 ReadHandler handler)
211 stream_impl_.async_read_some(buffers, handler);
214 /// Peek at the incoming data on the stream. Returns the number of bytes read.
215 /// Throws an exception on failure.
216 template <typename MutableBufferSequence>
217 std::size_t peek(const MutableBufferSequence& buffers)
219 return stream_impl_.peek(buffers);
222 /// Peek at the incoming data on the stream. Returns the number of bytes read,
223 /// or 0 if an error occurred.
224 template <typename MutableBufferSequence>
225 std::size_t peek(const MutableBufferSequence& buffers,
226 boost::system::error_code& ec)
228 return stream_impl_.peek(buffers, ec);
231 /// Determine the amount of data that may be read without blocking.
232 std::size_t in_avail()
234 return stream_impl_.in_avail();
237 /// Determine the amount of data that may be read without blocking.
238 std::size_t in_avail(boost::system::error_code& ec)
240 return stream_impl_.in_avail(ec);
243 private:
244 // The buffered write stream.
245 typedef buffered_write_stream<Stream> write_stream_type;
246 write_stream_type inner_stream_impl_;
248 // The buffered read stream.
249 typedef buffered_read_stream<write_stream_type&> read_stream_type;
250 read_stream_type stream_impl_;
253 } // namespace asio
254 } // namespace boost
256 #include <boost/asio/detail/pop_options.hpp>
258 #endif // BOOST_ASIO_BUFFERED_STREAM_HPP