closable.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Closable</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">Closable</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <H2>Description</H2>
  14. <P>
  15. A Closable Filter or Device receives notifications before a stream or stream buffer which contains it is closed.
  16. </P>
  17. <H4>Motivation</H4>
  18. <P>
  19. The C++ standard library does not recognize the notion of an arbitrary stream or stream buffer being <I>open</I> or <I>closed</I>. For the Iostreams library, however, the notion of opening and closing streams and stream buffers is important. Some Filters and Devices have special behavior reserved for the end of a stream &#8212; <I>e.g.</I>, an
  20. <A HREF="output_filter.html">OutputFilter</A> might conclude each sequence of data with a newline character.
  21. Other Filters or Devices maintain state during a sequence of i/o operations which must be
  22. reset before the Filter or Device can be reused. Such Filters and Devices need to be notified before a stream is closed, and should model Closable.
  23. </P>
  24. <H4>Closure Notifications</H4>
  25. <P>
  26. The Iostreams library sends closure notifications by invoking the function <A HREF="../functions/close.html"><CODE>close</CODE></A>. For most Closable Filter or Device, <CODE>close</CODE> delegates to a member function <CODE>close</CODE>; for non-Closable Devices, <CODE>close</CODE> calls <A HREF="../functions/flush.html"><CODE>flush</CODE></A>. The <A HREF="../functions/close.html#when">details</A> regarding when and how <CODE>close</CODE> is invoked are complicated. However, defining a Filter or Device which receives closure notifications is easy, as illustrated in the following sections.
  27. </P>
  28. <H4>Single-Sequence Devices</H4>
  29. <P>
  30. If a Device controls a single sequence of characters, it can be made Closable simply by specifying a <A HREF="../guide/traits.html#category_tags">category tag</A> which is convertible to <CODE>closable_tag</CODE> and by adding a member function <CODE>close</CODE>, like so:
  31. </P>
  32. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">void</SPAN> close() { <SPAN CLASS="comment"> /* process closure notification */ </SPAN> }</PRE>
  33. <P>
  34. Filters and Devices which derive from the <A HREF="../guide/concepts.html#convenience">convenience templates and <CODE>typedef</CODE>s</A> have category tags convertible to <CODE>closable_tag</CODE> provided automatically.
  35. </P>
  36. <H4>Single-Sequence Filters</H4>
  37. <P>For Filters controlling a single sequence, the signature of <CODE>close</CODE> is:</P>
  38. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Device&gt;
  39. <SPAN CLASS="keyword">void</SPAN> close(Device&amp;);</PRE>
  40. <H4>Two-Sequence Devices</H4>
  41. <P>
  42. If a Device controls separate input and output sequences, the signature of <CODE>close</CODE> is:</P>
  43. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">void</SPAN> close(std::ios_base::openmode);</PRE>
  44. <P>This function will be called twice: first with argument <CODE>std::ios_base::in</CODE>, to signal the closing of the input sequence, and later with argument <CODE>std::ios_base::out</CODE>, to signal the closing of the output sequence. </P>
  45. <H4>Two-Sequence Filters</H4>
  46. <P>
  47. If a Filter controls separate input and output sequences, the signature of <CODE>close</CODE> is:</P>
  48. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Device&gt;
  49. <SPAN CLASS="keyword">void</SPAN> close(Device&amp;, std::ios_base::openmode);</PRE>
  50. <P>This function will be called twice: first with argument <CODE>std::ios_base::in</CODE>, to signal the closing of the input sequence, and later with argument <CODE>std::ios_base::out</CODE>, to signal the closing of the output sequence.</P>
  51. <H4>Dual-Use Filters</H4>
  52. <P>The signature of <CODE>close</CODE> for <A HREF="dual_use_filter.html">DualUserFilters</A> is the same as that for two-sequence Filters:</P>
  53. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Device&gt;
  54. <SPAN CLASS="keyword">void</SPAN> close(Device&amp;, std::ios_base::openmode);</PRE>
  55. <P>This function will be called only once, depending on whether the filter is being used for input or output.</P>
  56. <H2>Refinement of</H2>
  57. <A NAME="types"></A>
  58. <H2>Associated Types</H2>
  59. <TABLE CELLPADDING="5" BORDER="1">
  60. <TR><TD>Character type</TD><TD>The type of the characters in the associated sequence</TD></TR>
  61. <TR><TD>Category</TD><TD>A category tag convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD></TR>
  62. </TABLE>
  63. <H2>Notation</H2>
  64. <TABLE CELLPADDING="2">
  65. <TR><TD><CODE>C</CODE></TD><TD>-</TD><TD>A type which is a model of Closable</TD></TR>
  66. <TR><TD><CODE>D</CODE></TD><TD>-</TD><TD>A type which is a model of <A HREF="blocking.html">Blocking</A>, with the same character type as <CODE>C</CODE> and with mode refining the mode of <CODE>C</CODE></TD></TR>
  67. <TR><TD><CODE>c</CODE></TD><TD>-</TD><TD>Object of type <CODE>C</CODE></TD></TR>
  68. <TR><TD><CODE>d</CODE></TD><TD>-</TD><TD>Object of type <CODE>D</CODE></TD></TR>
  69. <TR><TD><CODE>w</CODE></TD><TD>-</TD><TD>Object of type <CODE>std::ios_base::openmode</CODE>, equal to <CODE>std::ios_base::in</CODE> or <CODE>std::ios_base::out</CODE></TD></TR>
  70. </TABLE>
  71. <H2>Valid Expressions / Semantics</H2>
  72. <P>Same as <A HREF="filter.html">Filter</A> or <A HREF="device.html">Device</A>, with the following additional requirements:</P>
  73. <TABLE CELLPADDING="5" BORDER="1">
  74. <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Precondition</TH><TH>Semantics</TH><TH>Postcondition</TH></TR>
  75. <TR>
  76. <TD>
  77. <PRE CLASS="plain_code"><CODE><A HREF="../functions/close.html#close_device">boost::iostreams::close</A>(c, w)</CODE></PRE>
  78. </TD>
  79. <TD><CODE>void</CODE></TD>
  80. <TD>Category convertible to <A HREF="../guide/traits.html#category_tags"><CODE>device_tag</CODE></A></TD>
  81. <TD>
  82. If <CODE>w</CODE> is <CODE>std::ios_base::out</CODE>, writes zero or more characters to the output sequence
  83. </TD>
  84. <TD><CODE>c</CODE> is ready to begin processing a new character sequence</TD>
  85. </TR>
  86. <TR>
  87. <TD>
  88. <PRE CLASS="plain_code"><CODE><A HREF="../functions/close.html#close_filter">boost::iostreams::close</A>(c, d, w)</CODE></PRE>
  89. </TD>
  90. <TD><CODE>void</CODE></TD>
  91. <TD>Category convertible to <A HREF="../guide/traits.html#category_tags"><CODE>filter_tag</CODE></A></TD>
  92. <TD>
  93. <P>
  94. If <CODE>w</CODE> is <CODE>std::ios_base::out</CODE>, writes zero or more characters to <CODE>d</CODE> using <A HREF="../functions/put.html"><CODE>boost::iostreams::put</CODE></A> and <A HREF="../functions/write.html"><CODE>boost::iostreams::write</CODE></A>
  95. </P>
  96. </TD>
  97. <TD><CODE>c</CODE> is ready to begin processing a new character sequence</TD>
  98. </TR>
  99. </TABLE>
  100. <H2>Exceptions</H2>
  101. <P>
  102. Errors which occur during the execution of <A CLASS="code" HREF="../functions/close.html"><CODE>close</CODE></A> are be indicated by throwing exceptions. Such exceptions are caught and ignored if they occur during the execution of stream or stream buffer destructor.
  103. </P>
  104. <H2>Models</H2>
  105. <UL>
  106. <LI><A HREF="../guide/generic_streams.html">Generic Streams and Stream Buffers</A>.
  107. <LI><A HREF="../guide/filtering_streams.html">Filtering Streams and Stream Buffers</A>.
  108. <LI>Standard file streams and stream buffers.</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>