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 11<!-- Begin Banner --> 12 13 <H1 CLASS="title">Function Template <CODE>close</CODE></H1> 14 <HR CLASS="banner"> 15 16<!-- End Banner --> 17 18<DL class="page-index"> 19 <DT><A href="#description">Description</A></DT> 20 <DT><A href="#when">When is <CODE>close</CODE> invoked?</DT> 21 <DT><A href="#headers">Headers</A></DT> 22 <DT><A href="#reference">Reference</A></DT> 23</DL> 24 25<A NAME="description"></A> 26<H2>Description</H2> 27 28<P>The Iostreams library provides three overloads of the function template <CODE>close</CODE>.</P> 29 30<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> 31 32<PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams { 33 34<SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>> 35<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T& t); 36 37} } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE> 38 39 40<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. 41</P> 42 43<P>The details regarding when and how <CODE>close</CODE> is invoked are a bit messy:</P> 44 45<A NAME="when"></A> 46<H2>When is <CODE>close</CODE> invoked?</H2> 47 48<H4><CODE>stream_buffer</CODE> and <CODE>stream</CODE></H4> 49 50<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> 51<PRE CLASS="broken_ie"> boost::iostreams::close(d, std::ios_base::in); 52 boost::iostreams::close(d, std::ios_base::out);</PRE> 53<P>The effect, if <CODE>D</CODE> is <A HREF="../concepts/closable.html">Closable</A> and controls a single sequence, is as follows:</P> 54<PRE CLASS="broken_ie"> d.close();</PRE> 55<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> 56<PRE CLASS="broken_ie"> d.close(std::ios_base::in); 57 d.close(std::ios_base::out);</PRE> 58 59<P>(<I>See</I> the semantics of <CODE>close</CODE> for <A HREF="#close_device">Device types</A>, below.) 60 61<H4><CODE>filtering_streambuf</CODE> and <CODE>filtering_stream</CODE></H4> 62 63<P> 64 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> 65 66 <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std; 67 68 <SPAN CLASS="comment">// Close each input sequence, in reverse order:</SPAN> 69 boost::iostreams::close(d,<SPAN STYLE="visibility:hidden"><SUB>n-1</SUB>, buf<SUB>n-1</SUB></SPAN> ios_base::in); 70 boost::iostreams::close(f<SUB>n-1</SUB>, buf<SUB>n</SUB>,<SPAN STYLE="visibility:hidden"><SUB>-1</SUB></SPAN> ios_base::in); 71 boost::iostreams::close(f<SUB>n-2</SUB>, buf<SUB>n-1</SUB>, ios_base::in); 72 <SPAN CLASS="omitted">...</SPAN> 73 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); 74 75 <SPAN CLASS="comment">// Close each output sequence, in order:</SPAN> 76 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); 77 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); 78 <SPAN CLASS="omitted">...</SPAN> 79 boost::iostreams::close(f<SUB>n-1</SUB>, buf<SUB>n</SUB>,<SPAN STYLE="visibility:hidden"><SUB>n-</SUB></SPAN> ios_base::out); 80 boost::iostreams::close(d,<SPAN STYLE="visibility:hidden"><SUB>n-1</SUB>, buf<SUB>n-1</SUB></SPAN> ios_base::out);</PRE> 81</P> 82<P>This implies</P> 83<UL> 84<LI>For filter chains consisting of read-only components, the elements of the chain are closed in reverse order</LI> 85<LI>For filter chains consisting of write-only components, the elements of the chain are closed in forward order</LI> 86<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>. 87</UL> 88 89<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> 90 91<A NAME="headers"></A> 92<H2>Headers</H2> 93 94<DL> 95 <DT><A CLASS="header" HREF="../../../../boost/iostreams/close.hpp"><CODE><boost/iostreams/close.hpp></CODE></A></DT> 96 <DT><A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><CODE><boost/iostreams/operations.hpp></CODE></A></DT> 97</DL> 98 99<A NAME="reference"></A> 100<H2>Reference</H2> 101 102<A NAME="synopsis"></A> 103<H3>Synopsis</H3> 104 105<PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams { 106 107<SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>> 108<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_convenience">close</A>(T& t); 109 110<SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params_devices">T</A>> 111<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T& t, std::ios_base::openmode which); 112 113<SPAN CLASS="keyword">template</SPAN><<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>> 114<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_filter">close</A>(T& t, Device& next, std::ios_base::openmode which); 115 116} } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE> 117 118 119<A NAME="close_convenience"></A> 120<H3>Function Template <CODE>close</CODE> — Convenience Function</H3> 121 122<A NAME="template_params"></A> 123<H4>Template Parameters</H4> 124 125<TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2> 126<TR> 127 <TR> 128 <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 129 <TD>A model of one of the <A HREF="../guide/concepts.html#device_concepts">Device</A> concepts. 130 </TR> 131</TABLE> 132 133<H4>Semantics</H4> 134 135<PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> T> 136<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T& t);</PRE> 137 138<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>. 139 140<A NAME="close_device"></A> 141<H3>Function Template <CODE>close</CODE> — Closure Notification for Devices</H3> 142 143<A NAME="template_params_devices"></A> 144<H4>Template Parameters</H4> 145 146<TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2> 147<TR> 148 <TR> 149 <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 150 <TD>A model of one of the <A HREF="../guide/concepts.html#device_concepts">Device</A> concepts. 151 </TR> 152</TABLE> 153 154<H4>Semantics</H4> 155 156<PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> T> 157<SPAN CLASS="keyword">void</SPAN> <A CLASS="documented" HREF="#close_device">close</A>(T& t, std::ios_base::openmode which);</PRE> 158 159<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> 160 161<TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4> 162 <TR><TH><CODE>category<T>::type</CODE></TH><TH>semantics</TH></TR> 163 <TR> 164 <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> 165 <TD>calls <CODE>t.pop()</CODE> if <code>t</CODE> is complete and which == ios_base::in</CODE></TD> 166 </TR> 167 <TR> 168 <TD VALIGN="top">otherwise</TD> 169 <TD>calls <CODE>t.pop()</CODE> if <code>t</CODE> is complete and <CODE>which == ios_base::out</CODE></TD> 170 </TR> 171</TABLE> 172 173<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> 174 175<TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4> 176 <TR><TH><CODE>category<T>::type</CODE></TH><TH>semantics</TH></TR> 177 <TR> 178 <TD VALIGN="top">not convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD> 179 <TD>calls <A HREF="flush.html"><CODE>flush</CODE></A></TD> 180 </TR> 181 <TR> 182 <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> 183 <TD>calls <CODE>t.close(which)</CODE></TD> 184 </TR> 185 <TR> 186 <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> 187 <TD>calls <CODE>t.close()</CODE> if <CODE>which == ios_base::in</CODE></TD> 188 </TR> 189 <TR> 190 <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> 191 <TD>calls <CODE>t.close()</CODE> if <CODE>which == ios_base::out</CODE></TD> 192 </TR> 193</TABLE> 194 195<P>In short: 196<UL> 197 <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>. 198 <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. 199 <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>. 200</UL> 201 202<P>The last condition prevents a Device controlling a single sequence from being closed twice in succession.</P> 203 204<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 205 <CODE>std::ios_base::out</CODE> is <B>deprecated</B>. To close both sequences at once, use 206<PRE>close(t)</PRE> 207instead of 208<PRE>close(t, std::ios_base::in | std::ios_base::out)</PRE></P> 209 210<A NAME="close_filter"></A> 211<H3>Function Template <CODE>close</CODE> — Closure Notification for Filters</H3> 212 213<A NAME="template_params_filters"></A> 214<H4>Template Parameters</H4> 215 216<TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2> 217<TR> 218 <TR> 219 <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 220 <TD>A model of one of the <A HREF="../guide/concepts.html#filter_concepts">Filter</A> concepts</TD> 221 </TR> 222 <TR> 223 <TD VALIGN="top"><I>Device</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 224 <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> 225 </TR> 226</TABLE> 227 228<H4>Semantics</H4> 229 230<PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> T, <SPAN CLASS="keyword">typename</SPAN> Device> 231<SPAN CLASS="keyword">void</SPAN> close(T& t, Device& next, std::ios_base::openmode which);</PRE> 232 233<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> 234 235<TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4> 236 <TR><TH><CODE>category<T>::type</CODE></TH><TH>semantics</TH></TR> 237 <TR> 238 <TD VALIGN="top">not convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD> 239 <TD>calls <A HREF="flush.html"><CODE>flush</CODE></A></TD> 240 </TR> 241 <TR> 242 <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> 243 <TD>calls <CODE>t.close(next, which)</CODE></TD> 244 </TR> 245 <TR> 246 <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> 247 <TD>calls <CODE>t.close(next)</CODE> if <CODE>which == ios_base::in</CODE></TD> 248 </TR> 249 <TR> 250 <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> 251 <TD>calls <CODE>t.close(next)</CODE> if <CODE>which == ios_base::out</CODE></TD> 252 </TR> 253</TABLE> 254 255<P>In short: 256<UL> 257 <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>. 258 <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. 259 <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>. 260</UL> 261 262<P>The last condition prevents a Filter controlling a single sequence from being closed twice in succession.</P> 263 264<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 265 <CODE>std::ios_base::out</CODE> is <B>deprecated</B>.</P> 266 267<!-- End Footnotes --> 268 269<HR> 270 271<P> 272<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>. 273</P> 274 275<!-- Begin Footnotes --> 276 277<!-- Begin Footer --> 278 279<HR> 280 281<P CLASS="copyright">© Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>© Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P> 282<P CLASS="copyright"> 283 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>) 284</P> 285 286<!-- End Footer --> 287 288</BODY>