| 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 | |
|---|
| 10 | <!-- Begin Banner --> |
|---|
| 11 | |
|---|
| 12 | <H1 CLASS="title">Sink</H1> |
|---|
| 13 | <HR CLASS="banner"> |
|---|
| 14 | |
|---|
| 15 | <!-- End Banner --> |
|---|
| 16 | |
|---|
| 17 | <H2>Definition</H2> |
|---|
| 18 | |
|---|
| 19 | <P> |
|---|
| 20 | 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>. |
|---|
| 21 | </P> |
|---|
| 22 | |
|---|
| 23 | <H2>Description</H2> |
|---|
| 24 | |
|---|
| 25 | <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> |
|---|
| 26 | <OL> |
|---|
| 27 | <LI STYLE="list-style-type:lower-roman"> |
|---|
| 28 | 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>; |
|---|
| 29 | </LI> |
|---|
| 30 | <LI STYLE="list-style-type:lower-roman"> |
|---|
| 31 | by defining a member function <CODE>output_sequence</CODE> returning a pair of pointers delimiting the sequence in its entirety. |
|---|
| 32 | </LI> |
|---|
| 33 | </OL> |
|---|
| 34 | |
|---|
| 35 | <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> |
|---|
| 36 | |
|---|
| 37 | <P>The mode of a Sink is <A HREF="../guide/modes.html#output"><CODE>output</CODE></A> or one of its refinements.</P> |
|---|
| 38 | |
|---|
| 39 | <H2>Note</H2> |
|---|
| 40 | |
|---|
| 41 | <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>. |
|---|
| 42 | |
|---|
| 43 | <H2>Example</H2> |
|---|
| 44 | |
|---|
| 45 | <P>A model of Sink can be defined as follows:</P> |
|---|
| 46 | |
|---|
| 47 | <PRE CLASS="broken_ie"><SPAN CLASS="keyword">struct</SPAN> Sink { |
|---|
| 48 | <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type; |
|---|
| 49 | <SPAN CLASS="keyword">typedef</SPAN> sink_tag category; |
|---|
| 50 | <SPAN CLASS="keyword">void</SPAN> write(<SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n) |
|---|
| 51 | { |
|---|
| 52 | <SPAN CLASS="comment">// Write up to n characters from the buffer |
|---|
| 53 | // s to the output sequence, returning the |
|---|
| 54 | // number of characters written</SPAN> |
|---|
| 55 | } |
|---|
| 56 | };</PRE> |
|---|
| 57 | |
|---|
| 58 | <P> |
|---|
| 59 | 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>. |
|---|
| 60 | |
|---|
| 61 | <H2>Refinement of</H2> |
|---|
| 62 | |
|---|
| 63 | <P><A HREF="device.html">Device</A>.</P> |
|---|
| 64 | |
|---|
| 65 | <A NAME="types"></A> |
|---|
| 66 | <H2>Associated Types</H2> |
|---|
| 67 | |
|---|
| 68 | <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P> |
|---|
| 69 | |
|---|
| 70 | <TABLE CELLPADDING="5" BORDER="1"> |
|---|
| 71 | <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> |
|---|
| 72 | </TABLE> |
|---|
| 73 | |
|---|
| 74 | <A NAME="notation"></A> |
|---|
| 75 | <H2>Notation</H2> |
|---|
| 76 | |
|---|
| 77 | <TABLE CELLPADDING="2"> |
|---|
| 78 | <TR><TD><CODE>S</CODE></TD><TD>- A type which is a model of Sink</TD></TR> |
|---|
| 79 | <TR><TD><CODE>Ch</CODE></TD><TD>- The character type</TD></TR> |
|---|
| 80 | <TR><TD><CODE>snk</CODE></TD><TD>- Object of type <CODE>S</CODE></TD></TR> |
|---|
| 81 | <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>const Ch*</CODE></SPAN></TD></TR> |
|---|
| 82 | <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR> |
|---|
| 83 | <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR> |
|---|
| 84 | </TABLE> |
|---|
| 85 | |
|---|
| 86 | <A NAME="expressions"></A> |
|---|
| 87 | <H2>Valid Expressions / Semantics</H2> |
|---|
| 88 | |
|---|
| 89 | <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P> |
|---|
| 90 | |
|---|
| 91 | <TABLE CELLPADDING="5" BORDER="1"> |
|---|
| 92 | <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR> |
|---|
| 93 | <TR> |
|---|
| 94 | <TD> |
|---|
| 95 | <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A><S>::type</CODE></PRE> |
|---|
| 96 | </TD> |
|---|
| 97 | <TD><CODE>typename</CODE> of the character type</TD> |
|---|
| 98 | <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD> |
|---|
| 99 | </TR> |
|---|
| 100 | <TR> |
|---|
| 101 | <TD> |
|---|
| 102 | <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A><S>::type</CODE></PRE> |
|---|
| 103 | </TD> |
|---|
| 104 | <TD><CODE>typename</CODE> of the category</TD> |
|---|
| 105 | <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD> |
|---|
| 106 | </TR> |
|---|
| 107 | <TR> |
|---|
| 108 | <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/read.html">io::write</A>(snk, s, n)</CODE></PRE></TD> |
|---|
| 109 | <TD><CODE>std::streamsize</CODE></TD> |
|---|
| 110 | <TD>Not convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD> |
|---|
| 111 | <TD> |
|---|
| 112 | 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 |
|---|
| 113 | </TD> |
|---|
| 114 | </TR> |
|---|
| 115 | <TR> |
|---|
| 116 | <TD><PRE CLASS="plain_code"><CODE>snk.output_sequence()</CODE></PRE></TD> |
|---|
| 117 | <TD><PRE CLASS="plain_code"><CODE>std::pair<Ch*,Ch*></CODE></PRE></TD> |
|---|
| 118 | <TD>Convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD> |
|---|
| 119 | <TD>Returns a pair of pointers delimiting the sequence controlled by <CODE>snk</CODE></TD> |
|---|
| 120 | </TR> |
|---|
| 121 | </TABLE> |
|---|
| 122 | |
|---|
| 123 | <H2>Exceptions</H2> |
|---|
| 124 | |
|---|
| 125 | <P> |
|---|
| 126 | Errors which occur during the execution of member functions <CODE>write</CODE> or <CODE>output_sequence</CODE> |
|---|
| 127 | are indicated by throwing exceptions. Attempting to write past the end of the sequence is always an error. |
|---|
| 128 | </P> |
|---|
| 129 | |
|---|
| 130 | <P> |
|---|
| 131 | 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. |
|---|
| 132 | </P> |
|---|
| 133 | |
|---|
| 134 | <H2>Models</H2> |
|---|
| 135 | |
|---|
| 136 | <UL> |
|---|
| 137 | <LI>Standard output streams and stream buffers. |
|---|
| 138 | <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>. |
|---|
| 139 | </UL> |
|---|
| 140 | |
|---|
| 141 | <!-- Begin Footer --> |
|---|
| 142 | |
|---|
| 143 | <HR> |
|---|
| 144 | <P CLASS="copyright">Revised |
|---|
| 145 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
|---|
| 146 | 20 May, 2004 |
|---|
| 147 | <!--webbot bot="Timestamp" endspan i-checksum="38504" --> |
|---|
| 148 | </P> |
|---|
| 149 | |
|---|
| 150 | <P CLASS="copyright">© Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P> |
|---|
| 151 | <P CLASS="copyright"> |
|---|
| 152 | 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>) |
|---|
| 153 | </P> |
|---|
| 154 | |
|---|
| 155 | <!-- End Footer --> |
|---|
| 156 | |
|---|
| 157 | </BODY> |
|---|