[29] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
| 2 | <HTML> |
---|
| 3 | <HEAD> |
---|
| 4 | <TITLE>Tutorial</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">Tutorial</H1> |
---|
| 13 | <HR CLASS="banner"> |
---|
| 14 | |
---|
| 15 | <!-- End Banner --> |
---|
| 16 | |
---|
| 17 | <!-- Begin Nav --> |
---|
| 18 | |
---|
| 19 | <DIV CLASS='nav'> |
---|
| 20 | <A HREF='multichar_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/prev.png'></A> |
---|
| 21 | <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/up.png'></A> |
---|
| 22 | <A HREF='finite_state_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/next.png'></A> |
---|
| 23 | </DIV> |
---|
| 24 | |
---|
| 25 | <!-- End Nav --> |
---|
| 26 | |
---|
| 27 | <A NAME="dual_use"></A> |
---|
| 28 | <H2>2.2.9. Dual-Use Filters</H2> |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | <P> |
---|
| 32 | It can be confusing to maintain two separate filters — one for input and the other for output — to implement the same filtering algorithm. One way to avoid this is to use the <A HREF="writing_filters.html#filter_helpers">filter helpers</A> mentioned above. The filter helpers produce filters which can be used for either input or output, but not both simultaneously. Such filters are called <A HREF="../concepts/dual_use_filter.html">Dual-Use Filters</A>. |
---|
| 33 | </P> |
---|
| 34 | |
---|
| 35 | <P> |
---|
| 36 | A <A HREF="../concepts/dual_use_filter.html">Dual-Use Filter</A> can be produced by merging the implementations of an <A HREF="../concepts/input_filter.html">InputFilter</A> and an <A HREF="../concepts/output_filter.html">OutputFilter</A>, and giving the resulting class a <A HREF="../guide/traits.html#category">category</A> convertible to <A HREF="../guide/traits.html#category_tags"><CODE>dual_use_filter_tag</CODE></A>. For example, you can implement a <A HREF="shell_comments_filters.html">shell comments Filter</A> as a Dual-Use Filter as follows: |
---|
| 37 | </P> |
---|
| 38 | |
---|
| 39 | <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/char_traits.hpp"><SPAN CLASS="literal"><boost/iostreams/char_traits.hpp></SPAN></A> <SPAN CLASS="comment">// EOF, WOULD_BLOCK</SPAN> |
---|
| 40 | <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal"><boost/iostreams/concepts.hpp></SPAN></A> <SPAN CLASS="comment">// input_filter</SPAN> |
---|
| 41 | <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal"><boost/iostreams/operations.hpp></SPAN></A> <SPAN CLASS="comment">// get</SPAN> |
---|
| 42 | |
---|
| 43 | <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example { |
---|
| 44 | |
---|
| 45 | <SPAN CLASS="keyword">class</SPAN> shell_comments_input_filter : <SPAN CLASS="keyword"><SPAN CLASS="keyword"><SPAN CLASS="keyword">public</SPAN></SPAN></SPAN> dual_use_filter { |
---|
| 46 | <SPAN CLASS="keyword">public</SPAN>: |
---|
| 47 | <SPAN CLASS="keyword">explicit</SPAN> shell_comments_input_filter(<SPAN CLASS="keyword">char</SPAN> comment_char = <SPAN CLASS="literal">'#'</SPAN>) |
---|
| 48 | : comment_char_(comment_char), skip_(<SPAN CLASS="keyword">false</SPAN>) |
---|
| 49 | { } |
---|
| 50 | |
---|
| 51 | <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Source> |
---|
| 52 | <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get(Source& src) |
---|
| 53 | { |
---|
| 54 | <SPAN CLASS='comment'>/* same as shell_comments_input_filter::get() */</SPAN> |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Sink> |
---|
| 58 | <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put(Sink& dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c) |
---|
| 59 | { |
---|
| 60 | <SPAN CLASS='comment'>/* same as shell_comments_output_filter::put() */</SPAN> |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Device> |
---|
| 64 | <SPAN CLASS="keyword">void</SPAN> close(Device&, std::ios_base::openmode) |
---|
| 65 | { |
---|
| 66 | skip_ = <SPAN CLASS="keyword">false</SPAN>; |
---|
| 67 | } |
---|
| 68 | <SPAN CLASS="keyword">private</SPAN>: |
---|
| 69 | <SPAN CLASS="keyword">char</SPAN> comment_char_; |
---|
| 70 | <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> skip_; |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE> |
---|
| 74 | |
---|
| 75 | <P> |
---|
| 76 | When a <A HREF="../guide/filtering_streams.html">filtering stream or stream buffer</A> containing a <A HREF="../concepts/closable.html">Closable</A> <A HREF="../concepts/dual_use_filter.html">Dual-Use Filter</A> is closed, the Filter's member function <CODE>close</CODE> is called with one of the values <CODE>std::ios_base::in</CODE> or <CODE>std::ios_base::out</CODE> as the second function argument, indicating whether the Filter was used for input or output. |
---|
| 77 | </P> |
---|
| 78 | |
---|
| 79 | <!-- Begin Nav --> |
---|
| 80 | |
---|
| 81 | <DIV CLASS='nav'> |
---|
| 82 | <A HREF='multichar_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/prev.png'></A> |
---|
| 83 | <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/up.png'></A> |
---|
| 84 | <A HREF='finite_state_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/next.png'></A> |
---|
| 85 | </DIV> |
---|
| 86 | |
---|
| 87 | <!-- End Nav --> |
---|
| 88 | |
---|
| 89 | <!-- Begin Footer --> |
---|
| 90 | |
---|
| 91 | <HR> |
---|
| 92 | |
---|
| 93 | <P CLASS="copyright">Revised |
---|
| 94 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
| 95 | 20 May, 2004 |
---|
| 96 | <!--webbot bot="Timestamp" endspan i-checksum="38504" --> |
---|
| 97 | </P> |
---|
| 98 | |
---|
| 99 | <P CLASS="copyright">© Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P> |
---|
| 100 | <P CLASS="copyright"> |
---|
| 101 | Use, modification, and distribution are subject to the Boost Software License, Version 2.0. (See accompanying file <A HREF="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>) |
---|
| 102 | </P> |
---|
| 103 | <!-- End Footer --> |
---|
| 104 | |
---|
| 105 | </BODY> |
---|