/Src/Dependencies/Boost/boost/asio/buffered_stream.hpp

http://hadesmem.googlecode.com/ · C++ Header · 248 lines · 149 code · 36 blank · 63 comment · 1 complexity · 2f975286d15dd6aaec4c7a5adb3d4e89 MD5 · raw file

  1. //
  2. // buffered_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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. #ifndef BOOST_ASIO_BUFFERED_STREAM_HPP
  11. #define BOOST_ASIO_BUFFERED_STREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <boost/asio/buffered_read_stream.hpp>
  18. #include <boost/asio/buffered_write_stream.hpp>
  19. #include <boost/asio/buffered_stream_fwd.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/io_service.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. /// Adds buffering to the read- and write-related operations of a stream.
  27. /**
  28. * The buffered_stream class template can be used to add buffering to the
  29. * synchronous and asynchronous read and write operations of a stream.
  30. *
  31. * @par Thread Safety
  32. * @e Distinct @e objects: Safe.@n
  33. * @e Shared @e objects: Unsafe.
  34. *
  35. * @par Concepts:
  36. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  37. */
  38. template <typename Stream>
  39. class buffered_stream
  40. : private noncopyable
  41. {
  42. public:
  43. /// The type of the next layer.
  44. typedef typename boost::remove_reference<Stream>::type next_layer_type;
  45. /// The type of the lowest layer.
  46. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  47. /// Construct, passing the specified argument to initialise the next layer.
  48. template <typename Arg>
  49. explicit buffered_stream(Arg& a)
  50. : inner_stream_impl_(a),
  51. stream_impl_(inner_stream_impl_)
  52. {
  53. }
  54. /// Construct, passing the specified argument to initialise the next layer.
  55. template <typename Arg>
  56. explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
  57. std::size_t write_buffer_size)
  58. : inner_stream_impl_(a, write_buffer_size),
  59. stream_impl_(inner_stream_impl_, read_buffer_size)
  60. {
  61. }
  62. /// Get a reference to the next layer.
  63. next_layer_type& next_layer()
  64. {
  65. return stream_impl_.next_layer().next_layer();
  66. }
  67. /// Get a reference to the lowest layer.
  68. lowest_layer_type& lowest_layer()
  69. {
  70. return stream_impl_.lowest_layer();
  71. }
  72. /// Get a const reference to the lowest layer.
  73. const lowest_layer_type& lowest_layer() const
  74. {
  75. return stream_impl_.lowest_layer();
  76. }
  77. /// Get the io_service associated with the object.
  78. boost::asio::io_service& get_io_service()
  79. {
  80. return stream_impl_.get_io_service();
  81. }
  82. /// Close the stream.
  83. void close()
  84. {
  85. stream_impl_.close();
  86. }
  87. /// Close the stream.
  88. boost::system::error_code close(boost::system::error_code& ec)
  89. {
  90. return stream_impl_.close(ec);
  91. }
  92. /// Flush all data from the buffer to the next layer. Returns the number of
  93. /// bytes written to the next layer on the last write operation. Throws an
  94. /// exception on failure.
  95. std::size_t flush()
  96. {
  97. return stream_impl_.next_layer().flush();
  98. }
  99. /// Flush all data from the buffer to the next layer. Returns the number of
  100. /// bytes written to the next layer on the last write operation, or 0 if an
  101. /// error occurred.
  102. std::size_t flush(boost::system::error_code& ec)
  103. {
  104. return stream_impl_.next_layer().flush(ec);
  105. }
  106. /// Start an asynchronous flush.
  107. template <typename WriteHandler>
  108. void async_flush(WriteHandler handler)
  109. {
  110. return stream_impl_.next_layer().async_flush(handler);
  111. }
  112. /// Write the given data to the stream. Returns the number of bytes written.
  113. /// Throws an exception on failure.
  114. template <typename ConstBufferSequence>
  115. std::size_t write_some(const ConstBufferSequence& buffers)
  116. {
  117. return stream_impl_.write_some(buffers);
  118. }
  119. /// Write the given data to the stream. Returns the number of bytes written,
  120. /// or 0 if an error occurred.
  121. template <typename ConstBufferSequence>
  122. std::size_t write_some(const ConstBufferSequence& buffers,
  123. boost::system::error_code& ec)
  124. {
  125. return stream_impl_.write_some(buffers, ec);
  126. }
  127. /// Start an asynchronous write. The data being written must be valid for the
  128. /// lifetime of the asynchronous operation.
  129. template <typename ConstBufferSequence, typename WriteHandler>
  130. void async_write_some(const ConstBufferSequence& buffers,
  131. WriteHandler handler)
  132. {
  133. stream_impl_.async_write_some(buffers, handler);
  134. }
  135. /// Fill the buffer with some data. Returns the number of bytes placed in the
  136. /// buffer as a result of the operation. Throws an exception on failure.
  137. std::size_t fill()
  138. {
  139. return stream_impl_.fill();
  140. }
  141. /// Fill the buffer with some data. Returns the number of bytes placed in the
  142. /// buffer as a result of the operation, or 0 if an error occurred.
  143. std::size_t fill(boost::system::error_code& ec)
  144. {
  145. return stream_impl_.fill(ec);
  146. }
  147. /// Start an asynchronous fill.
  148. template <typename ReadHandler>
  149. void async_fill(ReadHandler handler)
  150. {
  151. stream_impl_.async_fill(handler);
  152. }
  153. /// Read some data from the stream. Returns the number of bytes read. Throws
  154. /// an exception on failure.
  155. template <typename MutableBufferSequence>
  156. std::size_t read_some(const MutableBufferSequence& buffers)
  157. {
  158. return stream_impl_.read_some(buffers);
  159. }
  160. /// Read some data from the stream. Returns the number of bytes read or 0 if
  161. /// an error occurred.
  162. template <typename MutableBufferSequence>
  163. std::size_t read_some(const MutableBufferSequence& buffers,
  164. boost::system::error_code& ec)
  165. {
  166. return stream_impl_.read_some(buffers, ec);
  167. }
  168. /// Start an asynchronous read. The buffer into which the data will be read
  169. /// must be valid for the lifetime of the asynchronous operation.
  170. template <typename MutableBufferSequence, typename ReadHandler>
  171. void async_read_some(const MutableBufferSequence& buffers,
  172. ReadHandler handler)
  173. {
  174. stream_impl_.async_read_some(buffers, handler);
  175. }
  176. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  177. /// Throws an exception on failure.
  178. template <typename MutableBufferSequence>
  179. std::size_t peek(const MutableBufferSequence& buffers)
  180. {
  181. return stream_impl_.peek(buffers);
  182. }
  183. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  184. /// or 0 if an error occurred.
  185. template <typename MutableBufferSequence>
  186. std::size_t peek(const MutableBufferSequence& buffers,
  187. boost::system::error_code& ec)
  188. {
  189. return stream_impl_.peek(buffers, ec);
  190. }
  191. /// Determine the amount of data that may be read without blocking.
  192. std::size_t in_avail()
  193. {
  194. return stream_impl_.in_avail();
  195. }
  196. /// Determine the amount of data that may be read without blocking.
  197. std::size_t in_avail(boost::system::error_code& ec)
  198. {
  199. return stream_impl_.in_avail(ec);
  200. }
  201. private:
  202. // The buffered write stream.
  203. typedef buffered_write_stream<Stream> write_stream_type;
  204. write_stream_type inner_stream_impl_;
  205. // The buffered read stream.
  206. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  207. read_stream_type stream_impl_;
  208. };
  209. } // namespace asio
  210. } // namespace boost
  211. #include <boost/asio/detail/pop_options.hpp>
  212. #endif // BOOST_ASIO_BUFFERED_STREAM_HPP