dual_use_filters.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. <!-- Begin Banner -->
  10. <H1 CLASS="title">Tutorial</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <!-- Begin Nav -->
  14. <DIV CLASS='nav'>
  15. <A HREF='multichar_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
  16. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  17. <A HREF='finite_state_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  18. </DIV>
  19. <!-- End Nav -->
  20. <A NAME="dual_use"></A>
  21. <H2>2.2.9. Dual-Use Filters</H2>
  22. <P>
  23. It can be confusing to maintain two separate filters &#8212; one for input and the other for output &#8212; 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>.
  24. </P>
  25. <P>
  26. 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:
  27. </P>
  28. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/char_traits.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/char_traits.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// EOF, WOULD_BLOCK</SPAN>
  29. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// input_filter</SPAN>
  30. <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">// get</SPAN>
  31. <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
  32. <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 {
  33. <SPAN CLASS="keyword">public</SPAN>:
  34. <SPAN CLASS="keyword">explicit</SPAN> shell_comments_input_filter(<SPAN CLASS="keyword">char</SPAN> comment_char = <SPAN CLASS="literal">'#'</SPAN>)
  35. : comment_char_(comment_char), skip_(<SPAN CLASS="keyword">false</SPAN>)
  36. { }
  37. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  38. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get(Source&amp; src)
  39. {
  40. <SPAN CLASS='comment'>/* same as shell_comments_input_filter::get() */</SPAN>
  41. }
  42. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  43. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put(Sink&amp; dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c)
  44. {
  45. <SPAN CLASS='comment'>/* same as shell_comments_output_filter::put() */</SPAN>
  46. }
  47. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Device&gt;
  48. <SPAN CLASS="keyword">void</SPAN> close(Device&amp;, std::ios_base::openmode)
  49. {
  50. skip_ = <SPAN CLASS="keyword">false</SPAN>;
  51. }
  52. <SPAN CLASS="keyword">private</SPAN>:
  53. <SPAN CLASS="keyword">char</SPAN> comment_char_;
  54. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> skip_;
  55. };
  56. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  57. <P>
  58. 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.
  59. </P>
  60. <!-- Begin Nav -->
  61. <DIV CLASS='nav'>
  62. <A HREF='multichar_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
  63. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  64. <A HREF='finite_state_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  65. </DIV>
  66. <!-- End Nav -->
  67. <!-- Begin Footer -->
  68. <HR>
  69. <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>
  70. <P CLASS="copyright">
  71. 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>)
  72. </P>
  73. <!-- End Footer -->
  74. </BODY>