]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/buffered_stream.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / buffered_stream.hpp
1 //
2 // buffered_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 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 //
10
11 #ifndef BOOST_ASIO_BUFFERED_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_STREAM_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <cstddef>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/buffered_read_stream.hpp>
22 #include <boost/asio/buffered_write_stream.hpp>
23 #include <boost/asio/buffered_stream_fwd.hpp>
24 #include <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/error.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31
32 /// Adds buffering to the read- and write-related operations of a stream.
33 /**
34 * The buffered_stream class template can be used to add buffering to the
35 * synchronous and asynchronous read and write operations of a stream.
36 *
37 * @par Thread Safety
38 * @e Distinct @e objects: Safe.@n
39 * @e Shared @e objects: Unsafe.
40 *
41 * @par Concepts:
42 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
43 */
44 template <typename Stream>
45 class buffered_stream
46 : private noncopyable
47 {
48 public:
49 /// The type of the next layer.
50 typedef typename remove_reference<Stream>::type next_layer_type;
51
52 /// The type of the lowest layer.
53 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
54
55 /// The type of the executor associated with the object.
56 typedef typename lowest_layer_type::executor_type executor_type;
57
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_)
63 {
64 }
65
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)
72 {
73 }
74
75 /// Get a reference to the next layer.
76 next_layer_type& next_layer()
77 {
78 return stream_impl_.next_layer().next_layer();
79 }
80
81 /// Get a reference to the lowest layer.
82 lowest_layer_type& lowest_layer()
83 {
84 return stream_impl_.lowest_layer();
85 }
86
87 /// Get a const reference to the lowest layer.
88 const lowest_layer_type& lowest_layer() const
89 {
90 return stream_impl_.lowest_layer();
91 }
92
93 /// Get the executor associated with the object.
94 executor_type get_executor() BOOST_ASIO_NOEXCEPT
95 {
96 return stream_impl_.lowest_layer().get_executor();
97 }
98
99 /// Close the stream.
100 void close()
101 {
102 stream_impl_.close();
103 }
104
105 /// Close the stream.
106 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
107 {
108 stream_impl_.close(ec);
109 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
110 }
111
112 /// Flush all data from the buffer to the next layer. Returns the number of
113 /// bytes written to the next layer on the last write operation. Throws an
114 /// exception on failure.
115 std::size_t flush()
116 {
117 return stream_impl_.next_layer().flush();
118 }
119
120 /// Flush all data from the buffer to the next layer. Returns the number of
121 /// bytes written to the next layer on the last write operation, or 0 if an
122 /// error occurred.
123 std::size_t flush(boost::system::error_code& ec)
124 {
125 return stream_impl_.next_layer().flush(ec);
126 }
127
128 /// Start an asynchronous flush.
129 /**
130 * @par Completion Signature
131 * @code void(boost::system::error_code, std::size_t) @endcode
132 */
133 template <
134 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
135 std::size_t)) WriteHandler
136 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
137 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
138 void (boost::system::error_code, std::size_t))
139 async_flush(
140 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
141 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
142 {
143 return stream_impl_.next_layer().async_flush(
144 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
145 }
146
147 /// Write the given data to the stream. Returns the number of bytes written.
148 /// Throws an exception on failure.
149 template <typename ConstBufferSequence>
150 std::size_t write_some(const ConstBufferSequence& buffers)
151 {
152 return stream_impl_.write_some(buffers);
153 }
154
155 /// Write the given data to the stream. Returns the number of bytes written,
156 /// or 0 if an error occurred.
157 template <typename ConstBufferSequence>
158 std::size_t write_some(const ConstBufferSequence& buffers,
159 boost::system::error_code& ec)
160 {
161 return stream_impl_.write_some(buffers, ec);
162 }
163
164 /// Start an asynchronous write. The data being written must be valid for the
165 /// lifetime of the asynchronous operation.
166 /**
167 * @par Completion Signature
168 * @code void(boost::system::error_code, std::size_t) @endcode
169 */
170 template <typename ConstBufferSequence,
171 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
172 std::size_t)) WriteHandler
173 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
174 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
175 void (boost::system::error_code, std::size_t))
176 async_write_some(const ConstBufferSequence& buffers,
177 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
178 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
179 {
180 return stream_impl_.async_write_some(buffers,
181 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
182 }
183
184 /// Fill the buffer with some data. Returns the number of bytes placed in the
185 /// buffer as a result of the operation. Throws an exception on failure.
186 std::size_t fill()
187 {
188 return stream_impl_.fill();
189 }
190
191 /// Fill the buffer with some data. Returns the number of bytes placed in the
192 /// buffer as a result of the operation, or 0 if an error occurred.
193 std::size_t fill(boost::system::error_code& ec)
194 {
195 return stream_impl_.fill(ec);
196 }
197
198 /// Start an asynchronous fill.
199 /**
200 * @par Completion Signature
201 * @code void(boost::system::error_code, std::size_t) @endcode
202 */
203 template <
204 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
205 std::size_t)) ReadHandler
206 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
207 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
208 void (boost::system::error_code, std::size_t))
209 async_fill(
210 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
211 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
212 {
213 return stream_impl_.async_fill(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
214 }
215
216 /// Read some data from the stream. Returns the number of bytes read. Throws
217 /// an exception on failure.
218 template <typename MutableBufferSequence>
219 std::size_t read_some(const MutableBufferSequence& buffers)
220 {
221 return stream_impl_.read_some(buffers);
222 }
223
224 /// Read some data from the stream. Returns the number of bytes read or 0 if
225 /// an error occurred.
226 template <typename MutableBufferSequence>
227 std::size_t read_some(const MutableBufferSequence& buffers,
228 boost::system::error_code& ec)
229 {
230 return stream_impl_.read_some(buffers, ec);
231 }
232
233 /// Start an asynchronous read. The buffer into which the data will be read
234 /// must be valid for the lifetime of the asynchronous operation.
235 /**
236 * @par Completion Signature
237 * @code void(boost::system::error_code, std::size_t) @endcode
238 */
239 template <typename MutableBufferSequence,
240 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
241 std::size_t)) ReadHandler
242 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
243 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
244 void (boost::system::error_code, std::size_t))
245 async_read_some(const MutableBufferSequence& buffers,
246 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
247 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
248 {
249 return stream_impl_.async_read_some(buffers,
250 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
251 }
252
253 /// Peek at the incoming data on the stream. Returns the number of bytes read.
254 /// Throws an exception on failure.
255 template <typename MutableBufferSequence>
256 std::size_t peek(const MutableBufferSequence& buffers)
257 {
258 return stream_impl_.peek(buffers);
259 }
260
261 /// Peek at the incoming data on the stream. Returns the number of bytes read,
262 /// or 0 if an error occurred.
263 template <typename MutableBufferSequence>
264 std::size_t peek(const MutableBufferSequence& buffers,
265 boost::system::error_code& ec)
266 {
267 return stream_impl_.peek(buffers, ec);
268 }
269
270 /// Determine the amount of data that may be read without blocking.
271 std::size_t in_avail()
272 {
273 return stream_impl_.in_avail();
274 }
275
276 /// Determine the amount of data that may be read without blocking.
277 std::size_t in_avail(boost::system::error_code& ec)
278 {
279 return stream_impl_.in_avail(ec);
280 }
281
282 private:
283 // The buffered write stream.
284 typedef buffered_write_stream<Stream> write_stream_type;
285 write_stream_type inner_stream_impl_;
286
287 // The buffered read stream.
288 typedef buffered_read_stream<write_stream_type&> read_stream_type;
289 read_stream_type stream_impl_;
290 };
291
292 } // namespace asio
293 } // namespace boost
294
295 #include <boost/asio/detail/pop_options.hpp>
296
297 #endif // BOOST_ASIO_BUFFERED_STREAM_HPP