Files
iostreams/doc/concepts/closable.html

137 lines
7.1 KiB
HTML
Executable File

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Closable</TITLE>
<LINK REL='stylesheet' HREF='../../../../boost.css'>
<LINK REL='stylesheet' HREF='../theme/iostreams.css'>
</HEAD>
<BODY>
<!-- Begin Banner -->
<H1 CLASS='title'>Closable</H1>
<HR CLASS='banner'>
<!-- End Banner -->
<H2>Description</H2>
<P>
A Closable Filter or Device receives notifications before a containing stream or stream buffer is closed.
</P>
<H4>Motivation</H4>
<P>
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
<A HREF='output_filter.html'>OutputFilter</A> might conclude each sequence of data with a newline character.
Other Filters or Devices maintain state during a sequence of i/o operations which must be
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.
</P>
<H4>Closure Notifications</H4>
<P>
The Iostreams Library sends closure notifications by invoking the function <A HREF="../functions/close.html"><CODE>close</CODE></A>. For a Closable Filter or Device, <CODE>close</CODE> delegates to a member function <CODE>close</CODE>; for other Devices, it does nothing. 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:
</P>
<H2>Examples</H2>
<P>
If a Device controls a single sequence of characters, it can be made Closable simply by specifying a <A HREF="../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:
</P>
<PRE CLASS='broken_ie'> <SPAN CLASS="keyword">void</SPAN> close() { <SPAN CLASS='comment'> /* process closure notification */ </SPAN> }</PRE>
<P>
A category tag covertible to <CODE>closable_tag</CODE> is provided automatically for Filter and Device types which derive from the <A HREF="../concepts.html#convenience">convenience templates and <CODE>typedef</CODE>s</A>.
</P>
<P>For Filter types controlling a single sequence, the signature should be:</P>
<PRE CLASS='broken_ie'> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Device&gt;
<SPAN CLASS="keyword">void</SPAN> close(Device&amp;) { <SPAN CLASS='omitted'> ... </SPAN> }</PRE>
<P>
For Filters and Devices controlling separate input and output sequences, the above signatures should be modified by adding a <CODE>std::ios_base::openmode</CODE> parameter at the beginning of the parameter list. The member will be called first with argument <CODE>std::ios_base::in</CODE> &#8212; to signal the closing of the input sequence &#8212; and later with argument <CODE>std::ios_base::out</CODE> &#8212; to signal the closing of the output sequence.
</P>
<H2>Refinement of</H2>
<A NAME='types'></A>
<H2>Associated Types</H2>
<TABLE CELLPADDING='5' BORDER='1'>
<TR><TD>Character type</TD><TD>The type of the characters in the associated sequence</TD></TR>
<TR><TD>Traits type</TD><TD>A character traits type associated with the character type</TD></TR>
<TR><TD>Category</TD><TD>A category tag convertible to <A HREF="../traits.html#category_tags"><CODE>closable_tag</CODE></A></TD></TR>
</TABLE>
<H2>Notation</H2>
<TABLE CELLPADDING='2'>
<TR><TD><CODE>C</CODE></TD><TD>-</TD><TD>A type which is a model of Closable</TD></TR>
<TR><TD><CODE>D</CODE></TD><TD>-</TD><TD>A type which is a model of <A HREF='device.html'>Device</A>, with the same character type as <CODE>C</CODE> and with i/o mode refining the i/o mode of <CODE>C</CODE></TD></TR>
<TR><TD><CODE>c</CODE></TD><TD>-</TD><TD>Object of type <CODE>C</CODE></TD></TR>
<TR><TD><CODE>d</CODE></TD><TD>-</TD><TD>Object of type <CODE>D</CODE></TD></TR>
<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>
</TABLE>
<H2>Valid Expressions / Semantics</H2>
<P>Same as <A HREF="filter.html">Filter</A> or <A HREF="device.html">Device</A>, with the following additional requirements:</P>
<TABLE CELLPADDING='5' BORDER='1'>
<TR><TH>Expression</TH><TH>Expression Type</TH><TH>Precondition</TH><TH>Semantics</TH><TH>Postcondition</TH></TR>
<TR>
<TD>
<PRE CLASS='plain_code'><CODE><A HREF='../functions/close.html#close_device'>boost::iostreams::close</A>(c, w)</CODE></PRE>
</TD>
<TD><CODE>void</CODE></TD>
<TD>Category convertible to <A HREF='../traits.html#category_tags'><CODE>device_tag</CODE></A></TD>
<TD>
If <CODE>w</CODE> is <CODE>std::ios_base::out</CODE>, writes zero or more characters to the output sequence
</TD>
<TD><CODE>c</CODE> is ready to begin processing new sequences of data</TD>
</TR>
<TR>
<TD>
<PRE CLASS='plain_code'><CODE><A HREF='../functions/close.html#close_filter'>boost::iostreams::close</A>(c, w, d)</CODE></PRE>
</TD>
<TD><CODE>void</CODE></TD>
<TD>Category convertible to <A HREF='../traits.html#category_tags'><CODE>filter_tag</CODE></A></TD>
<TD>
<P>
If <CODE>w</CODE> is <CODE>std::ios_base::in</CODE>, reads zero or more characters from <CODE>d</CODE> using <A HREF='../functions/get.html'><CODE>boost::iostreams::get</CODE></A> and <A HREF='../functions/read.html'><CODE>boost::iostreams::read</CODE></A>
</P>
<P>
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>
</P>
</TD>
<TD><CODE>c</CODE> is ready to begin processing new sequences of data</TD>
</TR>
</TABLE>
<H2>Exceptions</H2>
<P>
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.
</P>
<H2>Models</H2>
<P>Many of the Filters and Devices provided by the Iostreams Library are Closable, but this is an implementation detail.</P>
<!-- Begin Footer -->
<HR>
<P CLASS='copyright'>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
20 May, 2004
<!--webbot bot="Timestamp" endspan i-checksum="38504" -->
</P>
<P CLASS='copyright'>&copy; Copyright Jonathan Turkanis, 2004</P>
<P CLASS='copyright'>
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>)
</P>
<!-- End Footer -->
</BODY>