PageRenderTime 30ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 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/buffered_read_stream.hpp>
 21#include <boost/asio/buffered_write_stream.hpp>
 22#include <boost/asio/buffered_stream_fwd.hpp>
 23#include <boost/asio/detail/noncopyable.hpp>
 24#include <boost/asio/error.hpp>
 25#include <boost/asio/io_service.hpp>
 26
 27#include <boost/asio/detail/push_options.hpp>
 28
 29namespace boost {
 30namespace 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 */
 44template <typename Stream>
 45class buffered_stream
 46  : private noncopyable
 47{
 48public:
 49  /// The type of the next layer.
 50  typedef typename boost::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  /// Construct, passing the specified argument to initialise the next layer.
 56  template <typename Arg>
 57  explicit buffered_stream(Arg& a)
 58    : inner_stream_impl_(a),
 59      stream_impl_(inner_stream_impl_)
 60  {
 61  }
 62
 63  /// Construct, passing the specified argument to initialise the next layer.
 64  template <typename Arg>
 65  explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
 66      std::size_t write_buffer_size)
 67    : inner_stream_impl_(a, write_buffer_size),
 68      stream_impl_(inner_stream_impl_, read_buffer_size)
 69  {
 70  }
 71
 72  /// Get a reference to the next layer.
 73  next_layer_type& next_layer()
 74  {
 75    return stream_impl_.next_layer().next_layer();
 76  }
 77
 78  /// Get a reference to the lowest layer.
 79  lowest_layer_type& lowest_layer()
 80  {
 81    return stream_impl_.lowest_layer();
 82  }
 83
 84  /// Get a const reference to the lowest layer.
 85  const lowest_layer_type& lowest_layer() const
 86  {
 87    return stream_impl_.lowest_layer();
 88  }
 89
 90  /// Get the io_service associated with the object.
 91  boost::asio::io_service& get_io_service()
 92  {
 93    return stream_impl_.get_io_service();
 94  }
 95
 96  /// Close the stream.
 97  void close()
 98  {
 99    stream_impl_.close();
100  }
101
102  /// Close the stream.
103  boost::system::error_code close(boost::system::error_code& ec)
104  {
105    return stream_impl_.close(ec);
106  }
107
108  /// Flush all data from the buffer to the next layer. Returns the number of
109  /// bytes written to the next layer on the last write operation. Throws an
110  /// exception on failure.
111  std::size_t flush()
112  {
113    return stream_impl_.next_layer().flush();
114  }
115
116  /// Flush all data from the buffer to the next layer. Returns the number of
117  /// bytes written to the next layer on the last write operation, or 0 if an
118  /// error occurred.
119  std::size_t flush(boost::system::error_code& ec)
120  {
121    return stream_impl_.next_layer().flush(ec);
122  }
123
124  /// Start an asynchronous flush.
125  template <typename WriteHandler>
126  void async_flush(WriteHandler handler)
127  {
128    return stream_impl_.next_layer().async_flush(handler);
129  }
130
131  /// Write the given data to the stream. Returns the number of bytes written.
132  /// Throws an exception on failure.
133  template <typename ConstBufferSequence>
134  std::size_t write_some(const ConstBufferSequence& buffers)
135  {
136    return stream_impl_.write_some(buffers);
137  }
138
139  /// Write the given data to the stream. Returns the number of bytes written,
140  /// or 0 if an error occurred.
141  template <typename ConstBufferSequence>
142  std::size_t write_some(const ConstBufferSequence& buffers,
143      boost::system::error_code& ec)
144  {
145    return stream_impl_.write_some(buffers, ec);
146  }
147
148  /// Start an asynchronous write. The data being written must be valid for the
149  /// lifetime of the asynchronous operation.
150  template <typename ConstBufferSequence, typename WriteHandler>
151  void async_write_some(const ConstBufferSequence& buffers,
152      WriteHandler handler)
153  {
154    stream_impl_.async_write_some(buffers, handler);
155  }
156
157  /// Fill the buffer with some data. Returns the number of bytes placed in the
158  /// buffer as a result of the operation. Throws an exception on failure.
159  std::size_t fill()
160  {
161    return stream_impl_.fill();
162  }
163
164  /// Fill the buffer with some data. Returns the number of bytes placed in the
165  /// buffer as a result of the operation, or 0 if an error occurred.
166  std::size_t fill(boost::system::error_code& ec)
167  {
168    return stream_impl_.fill(ec);
169  }
170
171  /// Start an asynchronous fill.
172  template <typename ReadHandler>
173  void async_fill(ReadHandler handler)
174  {
175    stream_impl_.async_fill(handler);
176  }
177
178  /// Read some data from the stream. Returns the number of bytes read. Throws
179  /// an exception on failure.
180  template <typename MutableBufferSequence>
181  std::size_t read_some(const MutableBufferSequence& buffers)
182  {
183    return stream_impl_.read_some(buffers);
184  }
185
186  /// Read some data from the stream. Returns the number of bytes read or 0 if
187  /// an error occurred.
188  template <typename MutableBufferSequence>
189  std::size_t read_some(const MutableBufferSequence& buffers,
190      boost::system::error_code& ec)
191  {
192    return stream_impl_.read_some(buffers, ec);
193  }
194
195  /// Start an asynchronous read. The buffer into which the data will be read
196  /// must be valid for the lifetime of the asynchronous operation.
197  template <typename MutableBufferSequence, typename ReadHandler>
198  void async_read_some(const MutableBufferSequence& buffers,
199      ReadHandler handler)
200  {
201    stream_impl_.async_read_some(buffers, handler);
202  }
203
204  /// Peek at the incoming data on the stream. Returns the number of bytes read.
205  /// Throws an exception on failure.
206  template <typename MutableBufferSequence>
207  std::size_t peek(const MutableBufferSequence& buffers)
208  {
209    return stream_impl_.peek(buffers);
210  }
211
212  /// Peek at the incoming data on the stream. Returns the number of bytes read,
213  /// or 0 if an error occurred.
214  template <typename MutableBufferSequence>
215  std::size_t peek(const MutableBufferSequence& buffers,
216      boost::system::error_code& ec)
217  {
218    return stream_impl_.peek(buffers, ec);
219  }
220
221  /// Determine the amount of data that may be read without blocking.
222  std::size_t in_avail()
223  {
224    return stream_impl_.in_avail();
225  }
226
227  /// Determine the amount of data that may be read without blocking.
228  std::size_t in_avail(boost::system::error_code& ec)
229  {
230    return stream_impl_.in_avail(ec);
231  }
232
233private:
234  // The buffered write stream.
235  typedef buffered_write_stream<Stream> write_stream_type;
236  write_stream_type inner_stream_impl_;
237
238  // The buffered read stream.
239  typedef buffered_read_stream<write_stream_type&> read_stream_type;
240  read_stream_type stream_impl_;
241};
242
243} // namespace asio
244} // namespace boost
245
246#include <boost/asio/detail/pop_options.hpp>
247
248#endif // BOOST_ASIO_BUFFERED_STREAM_HPP