]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/buffered_write_stream.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / asio / buffered_write_stream.hpp
1 //
2 // buffered_write_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2018 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_WRITE_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_WRITE_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/buffered_write_stream_fwd.hpp>
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/completion_condition.hpp>
23 #include <boost/asio/detail/bind_handler.hpp>
24 #include <boost/asio/detail/buffered_stream_storage.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/type_traits.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/io_context.hpp>
29 #include <boost/asio/write.hpp>
30
31 #include <boost/asio/detail/push_options.hpp>
32
33 namespace boost {
34 namespace asio {
35
36 /// Adds buffering to the write-related operations of a stream.
37 /**
38 * The buffered_write_stream class template can be used to add buffering to the
39 * synchronous and asynchronous write operations of a stream.
40 *
41 * @par Thread Safety
42 * @e Distinct @e objects: Safe.@n
43 * @e Shared @e objects: Unsafe.
44 *
45 * @par Concepts:
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
47 */
48 template <typename Stream>
49 class buffered_write_stream
50 : private noncopyable
51 {
52 public:
53 /// The type of the next layer.
54 typedef typename remove_reference<Stream>::type next_layer_type;
55
56 /// The type of the lowest layer.
57 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
58
59 /// The type of the executor associated with the object.
60 typedef typename lowest_layer_type::executor_type executor_type;
61
62 #if defined(GENERATING_DOCUMENTATION)
63 /// The default buffer size.
64 static const std::size_t default_buffer_size = implementation_defined;
65 #else
66 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
67 #endif
68
69 /// Construct, passing the specified argument to initialise the next layer.
70 template <typename Arg>
71 explicit buffered_write_stream(Arg& a)
72 : next_layer_(a),
73 storage_(default_buffer_size)
74 {
75 }
76
77 /// Construct, passing the specified argument to initialise the next layer.
78 template <typename Arg>
79 buffered_write_stream(Arg& a, std::size_t buffer_size)
80 : next_layer_(a),
81 storage_(buffer_size)
82 {
83 }
84
85 /// Get a reference to the next layer.
86 next_layer_type& next_layer()
87 {
88 return next_layer_;
89 }
90
91 /// Get a reference to the lowest layer.
92 lowest_layer_type& lowest_layer()
93 {
94 return next_layer_.lowest_layer();
95 }
96
97 /// Get a const reference to the lowest layer.
98 const lowest_layer_type& lowest_layer() const
99 {
100 return next_layer_.lowest_layer();
101 }
102
103 /// Get the executor associated with the object.
104 executor_type get_executor() BOOST_ASIO_NOEXCEPT
105 {
106 return next_layer_.lowest_layer().get_executor();
107 }
108
109 #if !defined(BOOST_ASIO_NO_DEPRECATED)
110 /// (Deprecated: Use get_executor().) Get the io_context associated with the
111 /// object.
112 boost::asio::io_context& get_io_context()
113 {
114 return next_layer_.get_io_context();
115 }
116
117 /// (Deprecated: Use get_executor().) Get the io_context associated with the
118 /// object.
119 boost::asio::io_context& get_io_service()
120 {
121 return next_layer_.get_io_service();
122 }
123 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
124
125 /// Close the stream.
126 void close()
127 {
128 next_layer_.close();
129 }
130
131 /// Close the stream.
132 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
133 {
134 next_layer_.close(ec);
135 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
136 }
137
138 /// Flush all data from the buffer to the next layer. Returns the number of
139 /// bytes written to the next layer on the last write operation. Throws an
140 /// exception on failure.
141 std::size_t flush();
142
143 /// Flush all data from the buffer to the next layer. Returns the number of
144 /// bytes written to the next layer on the last write operation, or 0 if an
145 /// error occurred.
146 std::size_t flush(boost::system::error_code& ec);
147
148 /// Start an asynchronous flush.
149 template <typename WriteHandler>
150 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
151 void (boost::system::error_code, std::size_t))
152 async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
153
154 /// Write the given data to the stream. Returns the number of bytes written.
155 /// Throws an exception on failure.
156 template <typename ConstBufferSequence>
157 std::size_t write_some(const ConstBufferSequence& buffers);
158
159 /// Write the given data to the stream. Returns the number of bytes written,
160 /// or 0 if an error occurred and the error handler did not throw.
161 template <typename ConstBufferSequence>
162 std::size_t write_some(const ConstBufferSequence& buffers,
163 boost::system::error_code& ec);
164
165 /// Start an asynchronous write. The data being written must be valid for the
166 /// lifetime of the asynchronous operation.
167 template <typename ConstBufferSequence, typename WriteHandler>
168 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
169 void (boost::system::error_code, std::size_t))
170 async_write_some(const ConstBufferSequence& buffers,
171 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
172
173 /// Read some data from the stream. Returns the number of bytes read. Throws
174 /// an exception on failure.
175 template <typename MutableBufferSequence>
176 std::size_t read_some(const MutableBufferSequence& buffers)
177 {
178 return next_layer_.read_some(buffers);
179 }
180
181 /// Read some data from the stream. Returns the number of bytes read or 0 if
182 /// an error occurred.
183 template <typename MutableBufferSequence>
184 std::size_t read_some(const MutableBufferSequence& buffers,
185 boost::system::error_code& ec)
186 {
187 return next_layer_.read_some(buffers, ec);
188 }
189
190 /// Start an asynchronous read. The buffer into which the data will be read
191 /// must be valid for the lifetime of the asynchronous operation.
192 template <typename MutableBufferSequence, typename ReadHandler>
193 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
194 void (boost::system::error_code, std::size_t))
195 async_read_some(const MutableBufferSequence& buffers,
196 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
197 {
198 return next_layer_.async_read_some(buffers,
199 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
200 }
201
202 /// Peek at the incoming data on the stream. Returns the number of bytes read.
203 /// Throws an exception on failure.
204 template <typename MutableBufferSequence>
205 std::size_t peek(const MutableBufferSequence& buffers)
206 {
207 return next_layer_.peek(buffers);
208 }
209
210 /// Peek at the incoming data on the stream. Returns the number of bytes read,
211 /// or 0 if an error occurred.
212 template <typename MutableBufferSequence>
213 std::size_t peek(const MutableBufferSequence& buffers,
214 boost::system::error_code& ec)
215 {
216 return next_layer_.peek(buffers, ec);
217 }
218
219 /// Determine the amount of data that may be read without blocking.
220 std::size_t in_avail()
221 {
222 return next_layer_.in_avail();
223 }
224
225 /// Determine the amount of data that may be read without blocking.
226 std::size_t in_avail(boost::system::error_code& ec)
227 {
228 return next_layer_.in_avail(ec);
229 }
230
231 private:
232 /// Copy data into the internal buffer from the specified source buffer.
233 /// Returns the number of bytes copied.
234 template <typename ConstBufferSequence>
235 std::size_t copy(const ConstBufferSequence& buffers);
236
237 /// The next layer.
238 Stream next_layer_;
239
240 // The data in the buffer.
241 detail::buffered_stream_storage storage_;
242 };
243
244 } // namespace asio
245 } // namespace boost
246
247 #include <boost/asio/detail/pop_options.hpp>
248
249 #include <boost/asio/impl/buffered_write_stream.hpp>
250
251 #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP