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

http://hadesmem.googlecode.com/ · C++ Header · 370 lines · 171 code · 33 blank · 166 comment · 15 complexity · bd420ec720843ed45ba88e6509032f5c MD5 · raw file

  1. //
  2. // basic_streambuf.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_BASIC_STREAMBUF_HPP
  11. #define BOOST_ASIO_BASIC_STREAMBUF_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. #if !defined(BOOST_NO_IOSTREAM)
  17. #include <algorithm>
  18. #include <cstring>
  19. #include <stdexcept>
  20. #include <streambuf>
  21. #include <vector>
  22. #include <boost/limits.hpp>
  23. #include <boost/throw_exception.hpp>
  24. #include <boost/asio/basic_streambuf_fwd.hpp>
  25. #include <boost/asio/buffer.hpp>
  26. #include <boost/asio/detail/noncopyable.hpp>
  27. #include <boost/asio/detail/push_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. /// Automatically resizable buffer class based on std::streambuf.
  31. /**
  32. * The @c basic_streambuf class is derived from @c std::streambuf to associate
  33. * the streambuf's input and output sequences with one or more character
  34. * arrays. These character arrays are internal to the @c basic_streambuf
  35. * object, but direct access to the array elements is provided to permit them
  36. * to be used efficiently with I/O operations. Characters written to the output
  37. * sequence of a @c basic_streambuf object are appended to the input sequence
  38. * of the same object.
  39. *
  40. * The @c basic_streambuf class's public interface is intended to permit the
  41. * following implementation strategies:
  42. *
  43. * @li A single contiguous character array, which is reallocated as necessary
  44. * to accommodate changes in the size of the character sequence. This is the
  45. * implementation approach currently used in Asio.
  46. *
  47. * @li A sequence of one or more character arrays, where each array is of the
  48. * same size. Additional character array objects are appended to the sequence
  49. * to accommodate changes in the size of the character sequence.
  50. *
  51. * @li A sequence of one or more character arrays of varying sizes. Additional
  52. * character array objects are appended to the sequence to accommodate changes
  53. * in the size of the character sequence.
  54. *
  55. * The constructor for basic_streambuf accepts a @c size_t argument specifying
  56. * the maximum of the sum of the sizes of the input sequence and output
  57. * sequence. During the lifetime of the @c basic_streambuf object, the following
  58. * invariant holds:
  59. * @code size() <= max_size()@endcode
  60. * Any member function that would, if successful, cause the invariant to be
  61. * violated shall throw an exception of class @c std::length_error.
  62. *
  63. * The constructor for @c basic_streambuf takes an Allocator argument. A copy
  64. * of this argument is used for any memory allocation performed, by the
  65. * constructor and by all member functions, during the lifetime of each @c
  66. * basic_streambuf object.
  67. *
  68. * @par Examples
  69. * Writing directly from an streambuf to a socket:
  70. * @code
  71. * boost::asio::streambuf b;
  72. * std::ostream os(&b);
  73. * os << "Hello, World!\n";
  74. *
  75. * // try sending some data in input sequence
  76. * size_t n = sock.send(b.data());
  77. *
  78. * b.consume(n); // sent data is removed from input sequence
  79. * @endcode
  80. *
  81. * Reading from a socket directly into a streambuf:
  82. * @code
  83. * boost::asio::streambuf b;
  84. *
  85. * // reserve 512 bytes in output sequence
  86. * boost::asio::streambuf::mutable_buffers_type bufs = b.prepare(512);
  87. *
  88. * size_t n = sock.receive(bufs);
  89. *
  90. * // received data is "committed" from output sequence to input sequence
  91. * b.commit(n);
  92. *
  93. * std::istream is(&b);
  94. * std::string s;
  95. * is >> s;
  96. * @endcode
  97. */
  98. #if defined(GENERATING_DOCUMENTATION)
  99. template <typename Allocator = std::allocator<char> >
  100. #else
  101. template <typename Allocator>
  102. #endif
  103. class basic_streambuf
  104. : public std::streambuf,
  105. private noncopyable
  106. {
  107. public:
  108. #if defined(GENERATING_DOCUMENTATION)
  109. /// The type used to represent the input sequence as a list of buffers.
  110. typedef implementation_defined const_buffers_type;
  111. /// The type used to represent the output sequence as a list of buffers.
  112. typedef implementation_defined mutable_buffers_type;
  113. #else
  114. typedef boost::asio::const_buffers_1 const_buffers_type;
  115. typedef boost::asio::mutable_buffers_1 mutable_buffers_type;
  116. #endif
  117. /// Construct a basic_streambuf object.
  118. /**
  119. * Constructs a streambuf with the specified maximum size. The initial size
  120. * of the streambuf's input sequence is 0.
  121. */
  122. explicit basic_streambuf(
  123. std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
  124. const Allocator& allocator = Allocator())
  125. : max_size_(maximum_size),
  126. buffer_(allocator)
  127. {
  128. std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);
  129. buffer_.resize((std::max<std::size_t>)(pend, 1));
  130. setg(&buffer_[0], &buffer_[0], &buffer_[0]);
  131. setp(&buffer_[0], &buffer_[0] + pend);
  132. }
  133. /// Get the size of the input sequence.
  134. /**
  135. * @returns The size of the input sequence. The value is equal to that
  136. * calculated for @c s in the following code:
  137. * @code
  138. * size_t s = 0;
  139. * const_buffers_type bufs = data();
  140. * const_buffers_type::const_iterator i = bufs.begin();
  141. * while (i != bufs.end())
  142. * {
  143. * const_buffer buf(*i++);
  144. * s += buffer_size(buf);
  145. * }
  146. * @endcode
  147. */
  148. std::size_t size() const
  149. {
  150. return pptr() - gptr();
  151. }
  152. /// Get the maximum size of the basic_streambuf.
  153. /**
  154. * @returns The allowed maximum of the sum of the sizes of the input sequence
  155. * and output sequence.
  156. */
  157. std::size_t max_size() const
  158. {
  159. return max_size_;
  160. }
  161. /// Get a list of buffers that represents the input sequence.
  162. /**
  163. * @returns An object of type @c const_buffers_type that satisfies
  164. * ConstBufferSequence requirements, representing all character arrays in the
  165. * input sequence.
  166. *
  167. * @note The returned object is invalidated by any @c basic_streambuf member
  168. * function that modifies the input sequence or output sequence.
  169. */
  170. const_buffers_type data() const
  171. {
  172. return boost::asio::buffer(boost::asio::const_buffer(gptr(),
  173. (pptr() - gptr()) * sizeof(char_type)));
  174. }
  175. /// Get a list of buffers that represents the output sequence, with the given
  176. /// size.
  177. /**
  178. * Ensures that the output sequence can accommodate @c n characters,
  179. * reallocating character array objects as necessary.
  180. *
  181. * @returns An object of type @c mutable_buffers_type that satisfies
  182. * MutableBufferSequence requirements, representing character array objects
  183. * at the start of the output sequence such that the sum of the buffer sizes
  184. * is @c n.
  185. *
  186. * @throws std::length_error If <tt>size() + n > max_size()</tt>.
  187. *
  188. * @note The returned object is invalidated by any @c basic_streambuf member
  189. * function that modifies the input sequence or output sequence.
  190. */
  191. mutable_buffers_type prepare(std::size_t n)
  192. {
  193. reserve(n);
  194. return boost::asio::buffer(boost::asio::mutable_buffer(
  195. pptr(), n * sizeof(char_type)));
  196. }
  197. /// Move characters from the output sequence to the input sequence.
  198. /**
  199. * Appends @c n characters from the start of the output sequence to the input
  200. * sequence. The beginning of the output sequence is advanced by @c n
  201. * characters.
  202. *
  203. * Requires a preceding call <tt>prepare(x)</tt> where <tt>x >= n</tt>, and
  204. * no intervening operations that modify the input or output sequence.
  205. *
  206. * @throws std::length_error If @c n is greater than the size of the output
  207. * sequence.
  208. */
  209. void commit(std::size_t n)
  210. {
  211. if (pptr() + n > epptr())
  212. n = epptr() - pptr();
  213. pbump(static_cast<int>(n));
  214. setg(eback(), gptr(), pptr());
  215. }
  216. /// Remove characters from the input sequence.
  217. /**
  218. * Removes @c n characters from the beginning of the input sequence.
  219. *
  220. * @throws std::length_error If <tt>n > size()</tt>.
  221. */
  222. void consume(std::size_t n)
  223. {
  224. if (egptr() < pptr())
  225. setg(&buffer_[0], gptr(), pptr());
  226. if (gptr() + n > pptr())
  227. n = pptr() - gptr();
  228. gbump(static_cast<int>(n));
  229. }
  230. protected:
  231. enum { buffer_delta = 128 };
  232. /// Override std::streambuf behaviour.
  233. /**
  234. * Behaves according to the specification of @c std::streambuf::underflow().
  235. */
  236. int_type underflow()
  237. {
  238. if (gptr() < pptr())
  239. {
  240. setg(&buffer_[0], gptr(), pptr());
  241. return traits_type::to_int_type(*gptr());
  242. }
  243. else
  244. {
  245. return traits_type::eof();
  246. }
  247. }
  248. /// Override std::streambuf behaviour.
  249. /**
  250. * Behaves according to the specification of @c std::streambuf::overflow(),
  251. * with the specialisation that @c std::length_error is thrown if appending
  252. * the character to the input sequence would require the condition
  253. * <tt>size() > max_size()</tt> to be true.
  254. */
  255. int_type overflow(int_type c)
  256. {
  257. if (!traits_type::eq_int_type(c, traits_type::eof()))
  258. {
  259. if (pptr() == epptr())
  260. {
  261. std::size_t buffer_size = pptr() - gptr();
  262. if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
  263. {
  264. reserve(max_size_ - buffer_size);
  265. }
  266. else
  267. {
  268. reserve(buffer_delta);
  269. }
  270. }
  271. *pptr() = traits_type::to_char_type(c);
  272. pbump(1);
  273. return c;
  274. }
  275. return traits_type::not_eof(c);
  276. }
  277. void reserve(std::size_t n)
  278. {
  279. // Get current stream positions as offsets.
  280. std::size_t gnext = gptr() - &buffer_[0];
  281. std::size_t pnext = pptr() - &buffer_[0];
  282. std::size_t pend = epptr() - &buffer_[0];
  283. // Check if there is already enough space in the put area.
  284. if (n <= pend - pnext)
  285. {
  286. return;
  287. }
  288. // Shift existing contents of get area to start of buffer.
  289. if (gnext > 0)
  290. {
  291. pnext -= gnext;
  292. std::memmove(&buffer_[0], &buffer_[0] + gnext, pnext);
  293. }
  294. // Ensure buffer is large enough to hold at least the specified size.
  295. if (n > pend - pnext)
  296. {
  297. if (n <= max_size_ && pnext <= max_size_ - n)
  298. {
  299. pend = pnext + n;
  300. buffer_.resize((std::max<std::size_t>)(pend, 1));
  301. }
  302. else
  303. {
  304. std::length_error ex("boost::asio::streambuf too long");
  305. boost::throw_exception(ex);
  306. }
  307. }
  308. // Update stream positions.
  309. setg(&buffer_[0], &buffer_[0], &buffer_[0] + pnext);
  310. setp(&buffer_[0] + pnext, &buffer_[0] + pend);
  311. }
  312. private:
  313. std::size_t max_size_;
  314. std::vector<char_type, Allocator> buffer_;
  315. // Helper function to get the preferred size for reading data.
  316. friend std::size_t read_size_helper(
  317. basic_streambuf& sb, std::size_t max_size)
  318. {
  319. return std::min<std::size_t>(
  320. std::max<std::size_t>(512, sb.buffer_.capacity() - sb.size()),
  321. std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
  322. }
  323. };
  324. // Helper function to get the preferred size for reading data. Used for any
  325. // user-provided specialisations of basic_streambuf.
  326. template <typename Allocator>
  327. inline std::size_t read_size_helper(
  328. basic_streambuf<Allocator>& sb, std::size_t max_size)
  329. {
  330. return std::min<std::size_t>(512,
  331. std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
  332. }
  333. } // namespace asio
  334. } // namespace boost
  335. #include <boost/asio/detail/pop_options.hpp>
  336. #endif // !defined(BOOST_NO_IOSTREAM)
  337. #endif // BOOST_ASIO_BASIC_STREAMBUF_HPP