streambuf.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>streambuf</title>
  5. <link rel="stylesheet" href="../../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
  7. <link rel="home" href="../../index.html" title="Asio">
  8. <link rel="up" href="../reference.html" title="Reference">
  9. <link rel="prev" href="stream_socket_service/stream_socket_service.html" title="stream_socket_service::stream_socket_service">
  10. <link rel="next" href="system_category.html" title="system_category">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td></tr></table>
  14. <hr>
  15. <div class="spirit-nav">
  16. <a accesskey="p" href="stream_socket_service/stream_socket_service.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="system_category.html"><img src="../../next.png" alt="Next"></a>
  17. </div>
  18. <div class="section">
  19. <div class="titlepage"><div><div><h3 class="title">
  20. <a name="asio.reference.streambuf"></a><a class="link" href="streambuf.html" title="streambuf">streambuf</a>
  21. </h3></div></div></div>
  22. <p>
  23. <a class="indexterm" name="idp161353296"></a>
  24. Typedef for the typical usage of <a class="link" href="basic_streambuf.html" title="basic_streambuf"><code class="computeroutput"><span class="identifier">basic_streambuf</span></code></a>.
  25. </p>
  26. <pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">basic_streambuf</span> <span class="identifier">streambuf</span><span class="special">;</span>
  27. </pre>
  28. <h5>
  29. <a name="asio.reference.streambuf.h0"></a>
  30. <span><a name="asio.reference.streambuf.types"></a></span><a class="link" href="streambuf.html#asio.reference.streambuf.types">Types</a>
  31. </h5>
  32. <div class="informaltable"><table class="table">
  33. <colgroup>
  34. <col>
  35. <col>
  36. </colgroup>
  37. <thead><tr>
  38. <th>
  39. <p>
  40. Name
  41. </p>
  42. </th>
  43. <th>
  44. <p>
  45. Description
  46. </p>
  47. </th>
  48. </tr></thead>
  49. <tbody>
  50. <tr>
  51. <td>
  52. <p>
  53. <a class="link" href="basic_streambuf/const_buffers_type.html" title="basic_streambuf::const_buffers_type"><span class="bold"><strong>const_buffers_type</strong></span></a>
  54. </p>
  55. </td>
  56. <td>
  57. <p>
  58. The type used to represent the input sequence as a list of buffers.
  59. </p>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td>
  64. <p>
  65. <a class="link" href="basic_streambuf/mutable_buffers_type.html" title="basic_streambuf::mutable_buffers_type"><span class="bold"><strong>mutable_buffers_type</strong></span></a>
  66. </p>
  67. </td>
  68. <td>
  69. <p>
  70. The type used to represent the output sequence as a list of buffers.
  71. </p>
  72. </td>
  73. </tr>
  74. </tbody>
  75. </table></div>
  76. <h5>
  77. <a name="asio.reference.streambuf.h1"></a>
  78. <span><a name="asio.reference.streambuf.member_functions"></a></span><a class="link" href="streambuf.html#asio.reference.streambuf.member_functions">Member
  79. Functions</a>
  80. </h5>
  81. <div class="informaltable"><table class="table">
  82. <colgroup>
  83. <col>
  84. <col>
  85. </colgroup>
  86. <thead><tr>
  87. <th>
  88. <p>
  89. Name
  90. </p>
  91. </th>
  92. <th>
  93. <p>
  94. Description
  95. </p>
  96. </th>
  97. </tr></thead>
  98. <tbody>
  99. <tr>
  100. <td>
  101. <p>
  102. <a class="link" href="basic_streambuf/basic_streambuf.html" title="basic_streambuf::basic_streambuf"><span class="bold"><strong>basic_streambuf</strong></span></a>
  103. </p>
  104. </td>
  105. <td>
  106. <p>
  107. Construct a basic_streambuf object.
  108. </p>
  109. </td>
  110. </tr>
  111. <tr>
  112. <td>
  113. <p>
  114. <a class="link" href="basic_streambuf/commit.html" title="basic_streambuf::commit"><span class="bold"><strong>commit</strong></span></a>
  115. </p>
  116. </td>
  117. <td>
  118. <p>
  119. Move characters from the output sequence to the input sequence.
  120. </p>
  121. </td>
  122. </tr>
  123. <tr>
  124. <td>
  125. <p>
  126. <a class="link" href="basic_streambuf/consume.html" title="basic_streambuf::consume"><span class="bold"><strong>consume</strong></span></a>
  127. </p>
  128. </td>
  129. <td>
  130. <p>
  131. Remove characters from the input sequence.
  132. </p>
  133. </td>
  134. </tr>
  135. <tr>
  136. <td>
  137. <p>
  138. <a class="link" href="basic_streambuf/data.html" title="basic_streambuf::data"><span class="bold"><strong>data</strong></span></a>
  139. </p>
  140. </td>
  141. <td>
  142. <p>
  143. Get a list of buffers that represents the input sequence.
  144. </p>
  145. </td>
  146. </tr>
  147. <tr>
  148. <td>
  149. <p>
  150. <a class="link" href="basic_streambuf/max_size.html" title="basic_streambuf::max_size"><span class="bold"><strong>max_size</strong></span></a>
  151. </p>
  152. </td>
  153. <td>
  154. <p>
  155. Get the maximum size of the basic_streambuf.
  156. </p>
  157. </td>
  158. </tr>
  159. <tr>
  160. <td>
  161. <p>
  162. <a class="link" href="basic_streambuf/prepare.html" title="basic_streambuf::prepare"><span class="bold"><strong>prepare</strong></span></a>
  163. </p>
  164. </td>
  165. <td>
  166. <p>
  167. Get a list of buffers that represents the output sequence, with
  168. the given size.
  169. </p>
  170. </td>
  171. </tr>
  172. <tr>
  173. <td>
  174. <p>
  175. <a class="link" href="basic_streambuf/size.html" title="basic_streambuf::size"><span class="bold"><strong>size</strong></span></a>
  176. </p>
  177. </td>
  178. <td>
  179. <p>
  180. Get the size of the input sequence.
  181. </p>
  182. </td>
  183. </tr>
  184. </tbody>
  185. </table></div>
  186. <h5>
  187. <a name="asio.reference.streambuf.h2"></a>
  188. <span><a name="asio.reference.streambuf.protected_member_functions"></a></span><a class="link" href="streambuf.html#asio.reference.streambuf.protected_member_functions">Protected
  189. Member Functions</a>
  190. </h5>
  191. <div class="informaltable"><table class="table">
  192. <colgroup>
  193. <col>
  194. <col>
  195. </colgroup>
  196. <thead><tr>
  197. <th>
  198. <p>
  199. Name
  200. </p>
  201. </th>
  202. <th>
  203. <p>
  204. Description
  205. </p>
  206. </th>
  207. </tr></thead>
  208. <tbody>
  209. <tr>
  210. <td>
  211. <p>
  212. <a class="link" href="basic_streambuf/overflow.html" title="basic_streambuf::overflow"><span class="bold"><strong>overflow</strong></span></a>
  213. </p>
  214. </td>
  215. <td>
  216. <p>
  217. Override std::streambuf behaviour.
  218. </p>
  219. </td>
  220. </tr>
  221. <tr>
  222. <td>
  223. <p>
  224. <a class="link" href="basic_streambuf/reserve.html" title="basic_streambuf::reserve"><span class="bold"><strong>reserve</strong></span></a>
  225. </p>
  226. </td>
  227. <td>
  228. </td>
  229. </tr>
  230. <tr>
  231. <td>
  232. <p>
  233. <a class="link" href="basic_streambuf/underflow.html" title="basic_streambuf::underflow"><span class="bold"><strong>underflow</strong></span></a>
  234. </p>
  235. </td>
  236. <td>
  237. <p>
  238. Override std::streambuf behaviour.
  239. </p>
  240. </td>
  241. </tr>
  242. </tbody>
  243. </table></div>
  244. <p>
  245. The <code class="computeroutput"><span class="identifier">basic_streambuf</span></code> class
  246. is derived from <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">streambuf</span></code> to associate the streambuf's
  247. input and output sequences with one or more character arrays. These character
  248. arrays are internal to the <code class="computeroutput"><span class="identifier">basic_streambuf</span></code>
  249. object, but direct access to the array elements is provided to permit them
  250. to be used efficiently with I/O operations. Characters written to the output
  251. sequence of a <code class="computeroutput"><span class="identifier">basic_streambuf</span></code>
  252. object are appended to the input sequence of the same object.
  253. </p>
  254. <p>
  255. The <code class="computeroutput"><span class="identifier">basic_streambuf</span></code> class's
  256. public interface is intended to permit the following implementation strategies:
  257. </p>
  258. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  259. <li class="listitem">
  260. A single contiguous character array, which is reallocated as necessary
  261. to accommodate changes in the size of the character sequence. This is
  262. the implementation approach currently used in Asio.
  263. </li>
  264. <li class="listitem">
  265. A sequence of one or more character arrays, where each array is of the
  266. same size. Additional character array objects are appended to the sequence
  267. to accommodate changes in the size of the character sequence.
  268. </li>
  269. <li class="listitem">
  270. A sequence of one or more character arrays of varying sizes. Additional
  271. character array objects are appended to the sequence to accommodate changes
  272. in the size of the character sequence.
  273. </li>
  274. </ul></div>
  275. <p>
  276. The constructor for <a class="link" href="basic_streambuf.html" title="basic_streambuf"><code class="computeroutput"><span class="identifier">basic_streambuf</span></code></a> accepts a <code class="computeroutput"><span class="identifier">size_t</span></code> argument specifying the maximum
  277. of the sum of the sizes of the input sequence and output sequence. During
  278. the lifetime of the <code class="computeroutput"><span class="identifier">basic_streambuf</span></code>
  279. object, the following invariant holds:
  280. </p>
  281. <pre class="programlisting"><span class="identifier">size</span><span class="special">()</span> <span class="special">&lt;=</span> <span class="identifier">max_size</span><span class="special">()</span>
  282. </pre>
  283. <p>
  284. Any member function that would, if successful, cause the invariant to be
  285. violated shall throw an exception of class <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">length_error</span></code>.
  286. </p>
  287. <p>
  288. The constructor for <code class="computeroutput"><span class="identifier">basic_streambuf</span></code>
  289. takes an Allocator argument. A copy of this argument is used for any memory
  290. allocation performed, by the constructor and by all member functions, during
  291. the lifetime of each <code class="computeroutput"><span class="identifier">basic_streambuf</span></code>
  292. object.
  293. </p>
  294. <h5>
  295. <a name="asio.reference.streambuf.h3"></a>
  296. <span><a name="asio.reference.streambuf.examples"></a></span><a class="link" href="streambuf.html#asio.reference.streambuf.examples">Examples</a>
  297. </h5>
  298. <p>
  299. Writing directly from an streambuf to a socket:
  300. </p>
  301. <pre class="programlisting"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">streambuf</span> <span class="identifier">b</span><span class="special">;</span>
  302. <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="identifier">os</span><span class="special">(&amp;</span><span class="identifier">b</span><span class="special">);</span>
  303. <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello, World!\n"</span><span class="special">;</span>
  304. <span class="comment">// try sending some data in input sequence</span>
  305. <span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">sock</span><span class="special">.</span><span class="identifier">send</span><span class="special">(</span><span class="identifier">b</span><span class="special">.</span><span class="identifier">data</span><span class="special">());</span>
  306. <span class="identifier">b</span><span class="special">.</span><span class="identifier">consume</span><span class="special">(</span><span class="identifier">n</span><span class="special">);</span> <span class="comment">// sent data is removed from input sequence</span>
  307. </pre>
  308. <p>
  309. Reading from a socket directly into a streambuf:
  310. </p>
  311. <pre class="programlisting"><span class="identifier">asio</span><span class="special">::</span><span class="identifier">streambuf</span> <span class="identifier">b</span><span class="special">;</span>
  312. <span class="comment">// reserve 512 bytes in output sequence</span>
  313. <span class="identifier">asio</span><span class="special">::</span><span class="identifier">streambuf</span><span class="special">::</span><span class="identifier">mutable_buffers_type</span> <span class="identifier">bufs</span> <span class="special">=</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">prepare</span><span class="special">(</span><span class="number">512</span><span class="special">);</span>
  314. <span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">sock</span><span class="special">.</span><span class="identifier">receive</span><span class="special">(</span><span class="identifier">bufs</span><span class="special">);</span>
  315. <span class="comment">// received data is "committed" from output sequence to input sequence</span>
  316. <span class="identifier">b</span><span class="special">.</span><span class="identifier">commit</span><span class="special">(</span><span class="identifier">n</span><span class="special">);</span>
  317. <span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span> <span class="identifier">is</span><span class="special">(&amp;</span><span class="identifier">b</span><span class="special">);</span>
  318. <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">;</span>
  319. <span class="identifier">is</span> <span class="special">&gt;&gt;</span> <span class="identifier">s</span><span class="special">;</span>
  320. </pre>
  321. <h5>
  322. <a name="asio.reference.streambuf.h4"></a>
  323. <span><a name="asio.reference.streambuf.requirements"></a></span><a class="link" href="streambuf.html#asio.reference.streambuf.requirements">Requirements</a>
  324. </h5>
  325. <p>
  326. <span class="emphasis"><em>Header: </em></span><code class="literal">asio/streambuf.hpp</code>
  327. </p>
  328. <p>
  329. <span class="emphasis"><em>Convenience header: </em></span><code class="literal">asio.hpp</code>
  330. </p>
  331. </div>
  332. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  333. <td align="left"></td>
  334. <td align="right"><div class="copyright-footer">Copyright &#169; 2003-2014 Christopher M. Kohlhoff<p>
  335. Distributed under the Boost Software License, Version 1.0. (See accompanying
  336. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  337. </p>
  338. </div></td>
  339. </tr></table>
  340. <hr>
  341. <div class="spirit-nav">
  342. <a accesskey="p" href="stream_socket_service/stream_socket_service.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png" alt="Home"></a><a accesskey="n" href="system_category.html"><img src="../../next.png" alt="Next"></a>
  343. </div>
  344. </body>
  345. </html>