close.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Function Template close</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../../boost.css">
  6. <LINK REL="stylesheet" HREF="../theme/iostreams.css">
  7. <STYLE> H3 CODE { font-size: 120% } </STYLE>
  8. </HEAD>
  9. <BODY>
  10. <!-- Begin Banner -->
  11. <H1 CLASS="title">Function Template <CODE>close</CODE></H1>
  12. <HR CLASS="banner">
  13. <!-- End Banner -->
  14. <DL class="page-index">
  15. <DT><A href="#description">Description</A></DT>
  16. <DT><A href="#when">When is <CODE>close</CODE> invoked?</DT>
  17. <DT><A href="#headers">Headers</A></DT>
  18. <DT><A href="#reference">Reference</A></DT>
  19. </DL>
  20. <A NAME="description"></A>
  21. <H2>Description</H2>
  22. <P>The Iostreams library provides three overloads of the function template <CODE>close</CODE>.</P>
  23. <P>The first overload of <CODE>close</CODE> takes a single Device argument. It allows a user to close a Device without worrying whether the Device controls a single sequence or two sequences:</P>
  24. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams {
  25. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>&gt;
  26. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T&amp; t);
  27. } } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE>
  28. <P>The other two overloads of <CODE>close</CODE> should rarely be called by library users; they are invoked automatically by the library to indicate to <A HREF="../guide/concepts.html#filter_concepts">Filters</A> and <A HREF="../guide/concepts.html#device_concepts">Devices</A> that a sequence of data is about to end. This gives Filters and Devices an opportunity to free resources or to reset their states in preparation for a new character sequence. Filters and Devices which perform output can use the opportunity to write additional data to the end of a stream.
  29. </P>
  30. <P>The details regarding when and how <CODE>close</CODE> is invoked are a bit messy:</P>
  31. <A NAME="when"></A>
  32. <H2>When is <CODE>close</CODE> invoked?</H2>
  33. <H4><CODE>stream_buffer</CODE> and <CODE>stream</CODE></H4>
  34. <P>When an instance of <CODE>stream_buffer</CODE> or <CODE>stream</CODE> based on a Device <CODE>d</CODE> is closed using member function <CODE>close</CODE>, the following sequence of functions calls is made:<P>
  35. <PRE CLASS="broken_ie"> boost::iostreams::close(d, std::ios_base::in);
  36. boost::iostreams::close(d, std::ios_base::out);</PRE>
  37. <P>The effect, if <CODE>D</CODE> is <A HREF="../concepts/closable.html">Closable</A> and controls a single sequence, is as follows:</P>
  38. <PRE CLASS="broken_ie"> d.close();</PRE>
  39. <P>If <CODE>D</CODE> is <A HREF="../concepts/closable.html">Closable</A> and controls separate input and output sequences, the effect is as follows:</P>
  40. <PRE CLASS="broken_ie"> d.close(std::ios_base::in);
  41. d.close(std::ios_base::out);</PRE>
  42. <P>(<I>See</I> the semantics of <CODE>close</CODE> for <A HREF="#close_device">Device types</A>, below.)
  43. <H4><CODE>filtering_streambuf</CODE> and <CODE>filtering_stream</CODE></H4>
  44. <P>
  45. A <A HREF="../classes/filtering_streambuf.html"><CODE>filtering_streambuf</CODE></A> or <A HREF="../classes/filtering_stream.html"><CODE>filtering_stream</CODE></A> is considered to be <I>closed</I> if its lifetime ends while its chain is complete or if its terminal Device is removed using <CODE>pop</CODE> or <CODE>reset</CODE>. When this occurs, the following sequence of calls is made, assuming that the underlying sequence of Filters and Devices is <CODE>f<SUB>1</SUB></CODE>, <CODE>f<SUB>1</SUB></CODE>, ..., <CODE>f<SUB>n-1</SUB></CODE>, <CODE>d</CODE> and the corresponding sequence of stream buffers is <CODE>buf<SUB>1</SUB></CODE>, <CODE>buf<SUB>2</SUB></CODE>, ..., <CODE>buf<SUB>n-1</SUB></CODE>, <CODE>buf<SUB>n</SUB></CODE>:<A CLASS='footnote_ref' NAME='note_1_ref' HREF="#note_1"><SUP>[1]</SUP></A>
  46. <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  47. <SPAN CLASS="comment">// Close each input sequence, in reverse order:</SPAN>
  48. boost::iostreams::close(d,<SPAN STYLE="visibility:hidden"><SUB>n-1</SUB>, buf<SUB>n-1</SUB></SPAN> ios_base::in);
  49. boost::iostreams::close(f<SUB>n-1</SUB>, buf<SUB>n</SUB>,<SPAN STYLE="visibility:hidden"><SUB>-1</SUB></SPAN> ios_base::in);
  50. boost::iostreams::close(f<SUB>n-2</SUB>, buf<SUB>n-1</SUB>, ios_base::in);
  51. <SPAN CLASS="omitted">...</SPAN>
  52. boost::iostreams::close(f<SUB>1</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> buf<SUB>2</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> ios_base::in);
  53. <SPAN CLASS="comment">// Close each output sequence, in order:</SPAN>
  54. boost::iostreams::close(f<SUB>1</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> buf<SUB>2</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> ios_base::out);
  55. boost::iostreams::close(f<SUB>2</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> buf<SUB>3</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> ios_base::out);
  56. <SPAN CLASS="omitted">...</SPAN>
  57. boost::iostreams::close(f<SUB>n-1</SUB>, buf<SUB>n</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> ios_base::out);
  58. boost::iostreams::close(d,<SPAN STYLE="visibility:hidden"><SUB>n-1</SUB>, buf<SUB>n-1</SUB></SPAN> ios_base::out);</PRE>
  59. </P>
  60. <P>This implies</P>
  61. <UL>
  62. <LI>For filter chains consisting of read-only components, the elements of the chain are closed in reverse order</LI>
  63. <LI>For filter chains consisting of write-only components, the elements of the chain are closed in forward order</LI>
  64. <LI>Filters and Devices controlling separate input and output sequences receive two closure notifications, the first with argument <CODE>ios_base::in</CODE> and the second with argument <CODE>ios_base::out</CODE>.
  65. </UL>
  66. <P>(<I>See</I> the semantics of <CODE>close</CODE> for <A HREF="#close_filter">Filter</A> and <A HREF="#close_device">Device</A> types, below.)</P>
  67. <A NAME="headers"></A>
  68. <H2>Headers</H2>
  69. <DL>
  70. <DT><A CLASS="header" HREF="../../../../boost/iostreams/close.hpp"><CODE>&lt;boost/iostreams/close.hpp&gt;</CODE></A></DT>
  71. <DT><A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><CODE>&lt;boost/iostreams/operations.hpp&gt;</CODE></A></DT>
  72. </DL>
  73. <A NAME="reference"></A>
  74. <H2>Reference</H2>
  75. <A NAME="synopsis"></A>
  76. <H3>Synopsis</H3>
  77. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams {
  78. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>&gt;
  79. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_convenience">close</A>(T&amp; t);
  80. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params_devices">T</A>&gt;
  81. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T&amp; t, std::ios_base::openmode which);
  82. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params_filters">T</A>, <SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params_filters">Device</A>&gt;
  83. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_filter">close</A>(T&amp; t, Device&amp; next, std::ios_base::openmode which);
  84. } } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE>
  85. <A NAME="close_convenience"></A>
  86. <H3>Function Template <CODE>close</CODE> &#8212; Convenience Function</H3>
  87. <A NAME="template_params"></A>
  88. <H4>Template Parameters</H4>
  89. <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
  90. <TR>
  91. <TR>
  92. <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  93. <TD>A model of one of the <A HREF="../guide/concepts.html#device_concepts">Device</A> concepts.
  94. </TR>
  95. </TABLE>
  96. <H4>Semantics</H4>
  97. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> T&gt;
  98. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T&amp; t);</PRE>
  99. <P>This overload of <CODE>close</CODE> calls <CODE>close(t, std::ios_base::in)</CODE> followed by <CODE>close(t, std::ios_base::out)</CODE>. It ensures that <CODE>t</CODE> is closed properly, regardless of the <A HREF="../guide/modes.html">mode</A> or <CODE>t</CODE>.
  100. <A NAME="close_device"></A>
  101. <H3>Function Template <CODE>close</CODE> &#8212; Closure Notification for Devices</H3>
  102. <A NAME="template_params_devices"></A>
  103. <H4>Template Parameters</H4>
  104. <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
  105. <TR>
  106. <TR>
  107. <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  108. <TD>A model of one of the <A HREF="../guide/concepts.html#device_concepts">Device</A> concepts.
  109. </TR>
  110. </TABLE>
  111. <H4>Semantics</H4>
  112. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> T&gt;
  113. <SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T&amp; t, std::ios_base::openmode which);</PRE>
  114. <P>If <CODE>t</CODE> is a <A HREF="../guide/filtering_streams.html">filtering stream or stream buffer</A>, <CODE>close</CODE> calls <A HREF="../classes/filtering_stream.html#pop"><CODE>pop</CODE></A> if <CODE>t</CODE> is <A HREF="../classes/filtering_streambuf.html#is_complete">complete</A>. The semantics depends on its <A HREF="../guide/traits.html#category">category</A> as follows:</P>
  115. <TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4>
  116. <TR><TH><CODE>category&lt;T&gt;::type</CODE></TH><TH>semantics</TH></TR>
  117. <TR>
  118. <TD VALIGN="top">convertible to <A HREF="../guide/modes.html#input"><CODE>input</CODE></A> but not to <A HREF="../guide/modes.html#output"><CODE>output</CODE></A></TD>
  119. <TD>calls <CODE>t.pop()</CODE> if <code>t</CODE> is complete and which == ios_base::in</CODE></TD>
  120. </TR>
  121. <TR>
  122. <TD VALIGN="top">otherwise</TD>
  123. <TD>calls <CODE>t.pop()</CODE> if <code>t</CODE> is complete and <CODE>which == ios_base::out</CODE></TD>
  124. </TR>
  125. </TABLE>
  126. <P>The semantics of <CODE>close</CODE> for a device <CODE>T</CODE> other than a filtering stream or stream buffer depends on its <A HREF="../guide/traits.html#category">category</A> as follows:</P>
  127. <TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4>
  128. <TR><TH><CODE>category&lt;T&gt;::type</CODE></TH><TH>semantics</TH></TR>
  129. <TR>
  130. <TD VALIGN="top">not convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD>
  131. <TD>calls <A HREF="flush.html"><CODE>flush</CODE></A></TD>
  132. </TR>
  133. <TR>
  134. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#bidirectional"><CODE>bidirectional</CODE></A></A></TD>
  135. <TD>calls <CODE>t.close(which)</CODE></TD>
  136. </TR>
  137. <TR>
  138. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>input</CODE></A> but not to <A HREF="../guide/modes.html#output"><CODE>output</CODE></A></TD>
  139. <TD>calls <CODE>t.close()</CODE> if <CODE>which == ios_base::in</CODE></TD>
  140. </TR>
  141. <TR>
  142. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>output</CODE></A> but not to <A HREF="../guide/modes.html#bidirectional"><CODE>bidirectional</CODE></A></TD>
  143. <TD>calls <CODE>t.close()</CODE> if <CODE>which == ios_base::out</CODE></TD>
  144. </TR>
  145. </TABLE>
  146. <P>In short:
  147. <UL>
  148. <LI CLASS="square">If <CODE>T</CODE> is not <A HREF="../concepts/closable.html">Closable</A>, <CODE>close</CODE> calls <A HREF="flush.html"><CODE>flush</CODE></A>.
  149. <LI CLASS="square">If <CODE>T</CODE> is <A HREF="../concepts/closable.html">Closable</A> and controls two separate sequences, <CODE>close</CODE> delegates to a member function <CODE>close</CODE> taking a single <CODE>openmode</CODE> parameter.
  150. <LI CLASS="square">Otherwise, <CODE>close</CODE> delegates to a member function <CODE>close</CODE> taking no parameters, but only if its <CODE>openmode</CODE> parameter is consistent with the mode of <CODE>T</CODE>.
  151. </UL>
  152. <P>The last condition prevents a Device controlling a single sequence from being closed twice in succession.</P>
  153. <P><B>NOTE:</B> Starting with Boost 1.35, the invocation of this function with an <CODE>openmode</CODE> other than <CODE>std::ios_base::in</CODE> or
  154. <CODE>std::ios_base::out</CODE> is <B>deprecated</B>. To close both sequences at once, use
  155. <PRE>close(t)</PRE>
  156. instead of
  157. <PRE>close(t, std::ios_base::in | std::ios_base::out)</PRE></P>
  158. <A NAME="close_filter"></A>
  159. <H3>Function Template <CODE>close</CODE> &#8212; Closure Notification for Filters</H3>
  160. <A NAME="template_params_filters"></A>
  161. <H4>Template Parameters</H4>
  162. <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
  163. <TR>
  164. <TR>
  165. <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  166. <TD>A model of one of the <A HREF="../guide/concepts.html#filter_concepts">Filter</A> concepts</TD>
  167. </TR>
  168. <TR>
  169. <TD VALIGN="top"><I>Device</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  170. <TD>A <A HREF="../concepts/blocking.html">Blocking</A> <A HREF="../concepts/device.html">Device</A> whose <A HREF="../guide/modes.html">mode</A> refines that of <CODE>T</CODE>.</TD>
  171. </TR>
  172. </TABLE>
  173. <H4>Semantics</H4>
  174. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> T, <SPAN CLASS="keyword">typename</SPAN> Device&gt;
  175. <SPAN CLASS="keyword">void</SPAN> close(T&amp; t, Device&amp; next, std::ios_base::openmode which);</PRE>
  176. <P>The semantics of <CODE>close</CODE> for a Filter type <CODE>T</CODE> depends on its <A HREF="../guide/traits.html#category">category</A> as follows:</P>
  177. <TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4>
  178. <TR><TH><CODE>category&lt;T&gt;::type</CODE></TH><TH>semantics</TH></TR>
  179. <TR>
  180. <TD VALIGN="top">not convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD>
  181. <TD>calls <A HREF="flush.html"><CODE>flush</CODE></A></TD>
  182. </TR>
  183. <TR>
  184. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#bidirectional"><CODE>bidirectional</CODE></A></A></TD>
  185. <TD>calls <CODE>t.close(next, which)</CODE></TD>
  186. </TR>
  187. <TR>
  188. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>input</CODE></A> but not to <A HREF="../guide/modes.html#output"><CODE>output</CODE></A></TD>
  189. <TD>calls <CODE>t.close(next)</CODE> if <CODE>which == ios_base::in</CODE></TD>
  190. </TR>
  191. <TR>
  192. <TD VALIGN="top">convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>output</CODE></A> but not to <A HREF="../guide/modes.html#bidirectional"><CODE>bidirectional</CODE></A></TD>
  193. <TD>calls <CODE>t.close(next)</CODE> if <CODE>which == ios_base::out</CODE></TD>
  194. </TR>
  195. </TABLE>
  196. <P>In short:
  197. <UL>
  198. <LI CLASS="square">If <CODE>T</CODE> is not <A HREF="../concepts/closable.html">Closable</A>, <CODE>close</CODE> calls <A HREF="flush.html"><CODE>flush</CODE></A>.
  199. <LI CLASS="square">If <CODE>T</CODE> is <A HREF="../concepts/closable.html">Closable</A> and controls two separate sequences, <CODE>close</CODE> delegates to a member function <CODE>close</CODE> taking <CODE>openmode</CODE> and stream buffer parameters.
  200. <LI CLASS="square">Otherwise, <CODE>close</CODE> delegates to a member function <CODE>close</CODE> taking a single stream buffer parameter, but only if its <CODE>openmode</CODE> parameter is consistent with the mode of <CODE>T</CODE>.
  201. </UL>
  202. <P>The last condition prevents a Filter controlling a single sequence from being closed twice in succession.</P>
  203. <P><B>NOTE:</B> Starting with Boost 1.35, the invocation of this function with an <CODE>openmode</CODE> other than <CODE>std::ios_base::in</CODE> or
  204. <CODE>std::ios_base::out</CODE> is <B>deprecated</B>.</P>
  205. <!-- End Footnotes -->
  206. <HR>
  207. <P>
  208. <A CLASS='footnote_ref' NAME='note_1' HREF="#note_1_ref"><SUP>[1]</SUP></A>This behavior can be disabled in the case of <CODE>pop</CODE> by calling member function <CODE>set_auto_close</CODE> with the argument <CODE>false</CODE>. See, e.g., <A HREF="../classes/filtering_stream.html#set_auto_close"><CODE>filtering_stream::set_auto_close</CODE></A>.
  209. </P>
  210. <!-- Begin Footnotes -->
  211. <!-- Begin Footer -->
  212. <HR>
  213. <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>
  214. <P CLASS="copyright">
  215. 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>)
  216. </P>
  217. <!-- End Footer -->
  218. </BODY>