output_filter.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>OutputFilter</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">OutputFilter</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <DL CLASS="page-index" STYLE="margin-top:0">
  14. <DT><A HREF="#definition">Definition</A>
  15. <DT><A HREF="#description">Description</A>
  16. <DT><A HREF="#examples">Examples</A>
  17. <DT><A HREF="#detailed_specification">Detailed Specification</A>
  18. </DL>
  19. <A NAME="definition"></A>
  20. <H2>Definition</H2>
  21. <P>
  22. An OutputFilter is a <A HREF="filter.html">Filter</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#output">output</A>.
  23. </P>
  24. <A NAME="description"></A>
  25. <H2>Description</H2>
  26. <P>
  27. An OutputFilter operates on the character sequence controlled by a <A HREF="sink.html">Sink</A>, providing access to a filtered sequence having the same character type. It may expose the filtered sequence in two ways:
  28. <OL>
  29. <LI STYLE="list-style-type:lower-roman">
  30. by defining a member function <CODE>put</CODE>
  31. </LI>
  32. <LI STYLE="list-style-type:lower-roman">
  33. by defining a member function <CODE>write</CODE>
  34. </OL>
  35. The second alternative is provided for enhanced performance. OutputFilters implementing this alternative are referred to as as <I>Multi-Character</I>. (<I>See</I> <A HREF="multi_character.html">Multi-Character Filter</A>.)
  36. </P>
  37. <A NAME="examples"></A>
  38. <H2>Examples</H2>
  39. <H4>I. Ordinary OutputFilter</H4>
  40. <P>
  41. The following example shows an OutputFilter which converts each character in a sequence to uppercase.
  42. </P>
  43. <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN> <SPAN CLASS="comment">// toupper</SPAN>
  44. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// output_filter_tag</SPAN>
  45. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// put</SPAN>
  46. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  47. <SPAN CLASS="keyword">namespace</SPAN> io = boost::iostreams;
  48. <SPAN CLASS="keyword">struct</SPAN> toupper_output_filter {
  49. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  50. <SPAN CLASS="keyword">typedef</SPAN> io::output_filter_tag category;
  51. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  52. <SPAN CLASS="keyword">bool</SPAN> put(Sink&amp; snk, <SPAN CLASS="keyword">char</SPAN> c)
  53. {
  54. <SPAN CLASS="keyword">return</SPAN> io::put(snk, toupper((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) c));
  55. }
  56. };</PRE>
  57. <P>
  58. Here <CODE>char_type</CODE> is the <A HREF="../guide/traits.html#char_type">character type</A> of the Filter, <CODE>output_filter_tag</CODE> is a <A HREF="../guide/traits.html#category">category tag</A> identifying the Filter as a model of OutputFilter, and the function <A HREF="../functions/put.html"><CODE>io::put</CODE></A> writes a character to an arbitrary Sink.<A CLASS="footnote_ref" NAME="note_1_ref" HREF="#note_1"><SUP>[1]</SUP></A>
  59. </P>
  60. <P>
  61. The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>output_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>output_wfilter</CODE></A>, which provide member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new model of OutputFilter, it is often sufficient to derive from <CODE>output_filter</CODE> or <CODE>output_wfilter</CODE> and to define a member function <CODE>put</CODE>.
  62. </P>
  63. <H4>II. Multi-Character OutputFilter</H4>
  64. <P>
  65. The following example shows a Multi-Character OutputFilter which performs the same filtering operation as the Filter in Example I.
  66. </P>
  67. <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN> <SPAN CLASS="comment">// toupper</SPAN>
  68. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// output_filter_tag</SPAN>
  69. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// put</SPAN>
  70. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  71. <SPAN CLASS="keyword">namespace</SPAN> io = boost::iostreams;
  72. <SPAN CLASS="keyword">struct</SPAN> toupper_filter {
  73. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  74. <SPAN CLASS="keyword">typedef</SPAN> io::multichar_output_filter_tag category;
  75. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  76. <SPAN CLASS="keyword">std::streamsize</SPAN> write(Sink&amp; snk, <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, streamsize n)
  77. {
  78. std::streamsize rest = n;
  79. <SPAN CLASS="keyword">while</SPAN> (rest != 0 &amp;&amp; io::put(snk, toupper((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) *s++))
  80. --rest;
  81. <SPAN CLASS="keyword">return</SPAN> n - rest;
  82. }
  83. };</PRE>
  84. <P>
  85. Here <CODE>multichar_output_filter_tag</CODE> is a <A HREF="../guide/traits.html#category">category tag</A> identifying the Filter as a multichar OutputFilter.
  86. </P>
  87. <P>
  88. The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>multichar_output_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>multichar_output_wfilter</CODE></A>, which provide the member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new multichar OutputFilter, it is often sufficient to derive from <CODE>multichar_output_filter</CODE> or <CODE>multichar_output_wfilter</CODE> and to define a member function <CODE>write</CODE>.
  89. </P>
  90. <A NAME="detailed_specification"></A>
  91. <H2>Refinement of</H2>
  92. <P><A HREF="filter.html">Filter</A>.</P>
  93. <H2>Associated Types</H2>
  94. <TABLE CELLPADDING="5" BORDER="1">
  95. <TR><TD>Character type</TD><TD>The type of the characters in the filtered sequences</TD></TR>
  96. <TR>
  97. <TD>Category</TD>
  98. <TD>
  99. A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>filter_tag</CODE></A> and to <A HREF="../guide/modes.html#output"><CODE>output</CODE></A>
  100. </TD>
  101. </TR>
  102. <TR>
  103. <TD>Mode</TD>
  104. <TD>
  105. The unique <I>most-derived</I> <A HREF="../guide/modes.html#mode_tags">mode tag</A> to which Category is convertible
  106. </TD>
  107. </TR>
  108. </TABLE>
  109. <H2>Notation</H2>
  110. <TABLE CELLPADDING="2">
  111. <TR><TD><CODE>F</CODE></TD><TD>- A type which is a model of OutputFilter</TD></TR>
  112. <TR><TD><CODE>D</CODE></TD><TD>- A type which is a model of <A HREF="device.html">Device</A>, with the same character type as <CODE>F</CODE> and with mode refining the mode of <CODE>F</CODE></TD></TR>
  113. <TR><TD><CODE>Ch</CODE></TD><TD>- The character type of <CODE>F</CODE></TD></TR>
  114. <TR><TD><CODE>Tr</CODE></A></TD><TD>- <A HREF="../classes/char_traits.html"><CODE>boost::iostreams::char_traits&lt;Ch&gt;</CODE></A></TD></TR>
  115. <TR><TD><CODE>f</CODE></TD><TD>- Object of type <CODE>F</CODE></TD></TR>
  116. <TR><TD><CODE>d</CODE></TD><TD>- Object of type <CODE>D</CODE></TD></TR>
  117. <TR><TD><CODE>c</CODE></TD><TD>- Object of type <CODE>Ch</CODE></TD></TR>
  118. <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>const Ch*</CODE></TD></TR>
  119. <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR>
  120. <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR>
  121. </TABLE>
  122. <A NAME="semantics"></A>
  123. <H2>Valid Expressions / Semantics</H2>
  124. <TABLE CELLPADDING="5" BORDER="1">
  125. <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR>
  126. <TR>
  127. <TD>
  128. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;F&gt;::type</CODE></PRE>
  129. </TD>
  130. <TD><CODE>typename</CODE> of the character type</TD>
  131. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  132. </TR>
  133. <TR>
  134. <TD>
  135. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A>&lt;F&gt;::type</CODE></PRE>
  136. </TD>
  137. <TD><CODE>typename</CODE> of the category</TD>
  138. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  139. </TR>
  140. <TR>
  141. <TD><PRE CLASS="plain_code"><CODE>f.put(d, c)</CODE></PRE></TD>
  142. <TD><CODE>bool</CODE></TD>
  143. <TD>
  144. Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>output</CODE></A> but not to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
  145. </TD>
  146. <TD>
  147. Attempts to writes the character <CODE>c</CODE> to the output sequence controlled by <CODE>f</CODE>, returning <CODE>false</CODE> if <CODE>c</CODE> cannot be consumed because a call to <CODE>d</CODE> has consumed fewer characters than requested. The output sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/put.html"><CODE>io::put</CODE></A> and <A HREF="../functions/write.html"><CODE>io::write</CODE></A>.
  148. </TD>
  149. </TR>
  150. <TR>
  151. <TD><PRE CLASS="plain_code"><CODE>f.write(d, s, n)</CODE></PRE></TD>
  152. <TD><PRE CLASS="plain_code"><CODE>std::streamsize</CODE></PRE></TD>
  153. <TD>
  154. Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>output</CODE></A> and to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
  155. </TD>
  156. <TD>
  157. Writes up to <CODE>n</CODE> characters from the buffer <CODE>s</CODE> to the output sequence controlled by <CODE>d</CODE>, returning the number of characters written. A value less than <CODE>n</CODE> may be returned only if a call to <CODE>d</CODE> has consumed fewer characters than requested. The output sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/put.html"><CODE>io::put</CODE></A> and <A HREF="../functions/write.html"><CODE>io::write</CODE></A>.
  158. </TD>
  159. </TR>
  160. </TABLE>
  161. <H2>Exceptions</H2>
  162. <P>
  163. Errors which occur during the execution of <CODE>put</CODE> or <CODE>write</CODE> are indicated by throwing exceptions. Attempting to write past the end of the sequence is always an error.
  164. </P>
  165. <P>
  166. After an exception is thrown, an OutputFilter must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour.Furthermore, unless it is <A HREF="closable.html">Closable</A>, it must be ready to begin processing a new character sequence.
  167. </P>
  168. <H2>Models</H2>
  169. <H2>Acknowledgments</H2>
  170. <P>
  171. The concept OutputFilter was inspired by the <I>inserters</I> of <A CLASS="footnote_ref" HREF="../bibliography.html#kanze">[Kanze]</A>.
  172. </P>
  173. <!-- Begin Footnotes -->
  174. <HR>
  175. <P>
  176. <A CLASS="footnote_ref" NAME="note_1" HREF="#note_1_ref"><SUP>[1]</SUP></A>Technically, <CODE>boost::iostreams::put</CODE> requires that a Sink be <A HREF="../concepts/direct.html"><I>indirect</I></A>.
  177. </P>
  178. <!-- End Footnotes -->
  179. <!-- Begin Footer -->
  180. <HR>
  181. <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>
  182. <P CLASS="copyright">
  183. 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>)
  184. </P>
  185. <!-- End Footer -->
  186. </BODY>