sink.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Sink</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../../boost.css">
  6. <LINK REL="stylesheet" HREF="../theme/iostreams.css">
  7. </HEAD>
  8. <BODY>
  9. <!-- Begin Banner -->
  10. <H1 CLASS="title">Sink</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <H2>Definition</H2>
  14. <P>
  15. A Sink is a <A HREF="device.html">Device</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#output">output</A>.
  16. </P>
  17. <H2>Description</H2>
  18. <P>A Sink provides write-access to a sequence of characters of a given type. In general, a Sink may expose this sequence in two ways:</P>
  19. <OL>
  20. <LI STYLE="list-style-type:lower-roman">
  21. by defining a member function <CODE>write</CODE>, invoked indirectly by the Iostreams library through the function <A HREF="../functions/write.html"><CODE>boost::iostreams::write</CODE></A>;
  22. </LI>
  23. <LI STYLE="list-style-type:lower-roman">
  24. by defining a member function <CODE>output_sequence</CODE> returning a pair of pointers delimiting the sequence in its entirety.
  25. </LI>
  26. </OL>
  27. <P>As a special case, Boost.Iostreams treats standard output streams as Sinks. (For details, see <A HREF="../functions/write.html"><CODE>write</CODE></A>.)</P>
  28. <P>The mode of a Sink is <A HREF="../guide/modes.html#output"><CODE>output</CODE></A> or one of its refinements.</P>
  29. <H2>Note</H2>
  30. <P>To be usable with the streams and stream buffers provided by the Boost Iostreams library, Sinks must model <A HREF="blocking.html">Blocking</A>.
  31. <H2>Example</H2>
  32. <P>A model of Sink can be defined as follows:</P>
  33. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">struct</SPAN> Sink {
  34. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  35. <SPAN CLASS="keyword">typedef</SPAN> sink_tag category;
  36. std::streamsize write(<SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n)
  37. {
  38. <SPAN CLASS="comment">// Write up to n characters from the buffer
  39. // s to the output sequence, returning the
  40. // number of characters written</SPAN>
  41. }
  42. };</PRE>
  43. <P>
  44. Here <CODE>sink_tag</CODE> is a <A HREF="../guide/traits.html#category_tags">category tag</A> identifying the type as a model of Sink. Typically a Sink can be defined by deriving from the helper classes <A HREF="../classes/device.html#synopsis"><CODE>sink</CODE></A> or <A HREF="../classes/device.html#synopsis"><CODE>wsink</CODE></A> and defining a member function <CODE>write</CODE>.
  45. <H2>Refinement of</H2>
  46. <P><A HREF="device.html">Device</A>.</P>
  47. <A NAME="types"></A>
  48. <H2>Associated Types</H2>
  49. <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P>
  50. <TABLE CELLPADDING="5" BORDER="1">
  51. <TR><TD>Category</TD><TD>A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>device_tag</CODE></A> and to <A HREF="../guide/modes.html#mode_tags"><CODE>output</CODE></A></TD></TR>
  52. </TABLE>
  53. <A NAME="notation"></A>
  54. <H2>Notation</H2>
  55. <TABLE CELLPADDING="2">
  56. <TR><TD><CODE>S</CODE></TD><TD>- A type which is a model of Sink</TD></TR>
  57. <TR><TD><CODE>Ch</CODE></TD><TD>- The character type</TD></TR>
  58. <TR><TD><CODE>snk</CODE></TD><TD>- Object of type <CODE>S</CODE></TD></TR>
  59. <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>const Ch*</CODE></SPAN></TD></TR>
  60. <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR>
  61. <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR>
  62. </TABLE>
  63. <A NAME="expressions"></A>
  64. <H2>Valid Expressions / Semantics</H2>
  65. <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P>
  66. <TABLE CELLPADDING="5" BORDER="1">
  67. <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR>
  68. <TR>
  69. <TD>
  70. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;S&gt;::type</CODE></PRE>
  71. </TD>
  72. <TD><CODE>typename</CODE> of the character type</TD>
  73. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  74. </TR>
  75. <TR>
  76. <TD>
  77. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A>&lt;S&gt;::type</CODE></PRE>
  78. </TD>
  79. <TD><CODE>typename</CODE> of the category</TD>
  80. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  81. </TR>
  82. <TR>
  83. <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/write.html">io::write</A>(snk, s, n)</CODE></PRE></TD>
  84. <TD><CODE>std::streamsize</CODE></TD>
  85. <TD>Not convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD>
  86. <TD>
  87. Writes up to <CODE>n</CODE> characters from the sequence beginning at <CODE>s</CODE> to the output sequence controlled by <CODE>dev</CODE>, returning the number of characters written
  88. </TD>
  89. </TR>
  90. <TR>
  91. <TD><PRE CLASS="plain_code"><CODE>snk.output_sequence()</CODE></PRE></TD>
  92. <TD><PRE CLASS="plain_code"><CODE>std::pair&lt;Ch*,Ch*&gt;</CODE></PRE></TD>
  93. <TD>Convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD>
  94. <TD>Returns a pair of pointers delimiting the sequence controlled by <CODE>snk</CODE></TD>
  95. </TR>
  96. </TABLE>
  97. <H2>Exceptions</H2>
  98. <P>
  99. Errors which occur during the execution of member functions <CODE>write</CODE> or <CODE>output_sequence</CODE>
  100. are indicated by throwing exceptions. Attempting to write past the end of the sequence is always an error.
  101. </P>
  102. <P>
  103. After an exception is thrown, a Sink must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour.
  104. </P>
  105. <H2>Models</H2>
  106. <UL>
  107. <LI>Standard output streams and stream buffers.
  108. <LI><A HREF="../classes/array.html#array_sink"><CODE>array_sink</CODE></A>, <A HREF="../classes/file.html#file_sink"><CODE>file_sink</CODE></A>, <A HREF="../classes/file_descriptor.html#file_descriptor_sink"><CODE>file_descriptor_sink</CODE></A>, <A HREF="../classes/mapped_file.html#mapped_file_sink"><CODE>mapped_file_sink</CODE></A>.
  109. </UL>
  110. <!-- Begin Footer -->
  111. <HR>
  112. <P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P>
  113. <P CLASS="copyright">
  114. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
  115. </P>
  116. <!-- End Footer -->
  117. </BODY>