| 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 | |
|---|
| 10 | <!-- Begin Banner --> |
|---|
| 11 | |
|---|
| 12 | <H1 CLASS="title">Closable</H1> |
|---|
| 13 | <HR CLASS="banner"> |
|---|
| 14 | |
|---|
| 15 | <!-- End Banner --> |
|---|
| 16 | |
|---|
| 17 | <H2>Description</H2> |
|---|
| 18 | |
|---|
| 19 | <P> |
|---|
| 20 | A Closable Filter or Device receives notifications before a stream or stream buffer which contains it is closed. |
|---|
| 21 | </P> |
|---|
| 22 | |
|---|
| 23 | <H4>Motivation</H4> |
|---|
| 24 | |
|---|
| 25 | <P> |
|---|
| 26 | 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 — <I>e.g.</I>, an |
|---|
| 27 | <A HREF="output_filter.html">OutputFilter</A> might conclude each sequence of data with a newline character. |
|---|
| 28 | Other Filters or Devices maintain state during a sequence of i/o operations which must be |
|---|
| 29 | 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. |
|---|
| 30 | </P> |
|---|
| 31 | |
|---|
| 32 | <H4>Closure Notifications</H4> |
|---|
| 33 | |
|---|
| 34 | <P> |
|---|
| 35 | 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: |
|---|
| 36 | </P> |
|---|
| 37 | |
|---|
| 38 | <H2>Examples</H2> |
|---|
| 39 | |
|---|
| 40 | <P> |
|---|
| 41 | 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: |
|---|
| 42 | </P> |
|---|
| 43 | <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">void</SPAN> close() { <SPAN CLASS="comment"> /* process closure notification */ </SPAN> }</PRE> |
|---|
| 44 | <P> |
|---|
| 45 | 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. |
|---|
| 46 | </P> |
|---|
| 47 | <P>For Filters controlling a single sequence, the signature of <CODE>close</CODE> is:</P> |
|---|
| 48 | <PRE CLASS="broken_ie"> <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Device> |
|---|
| 49 | <SPAN CLASS="keyword">void</SPAN> close(Device&) { <SPAN CLASS="omitted"> ... </SPAN> }</PRE> |
|---|
| 50 | <P> |
|---|
| 51 | 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 end of the parameter list. 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. |
|---|
| 52 | </P> |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | <H2>Refinement of</H2> |
|---|
| 56 | |
|---|
| 57 | <A NAME="types"></A> |
|---|
| 58 | <H2>Associated Types</H2> |
|---|
| 59 | |
|---|
| 60 | <TABLE CELLPADDING="5" BORDER="1"> |
|---|
| 61 | <TR><TD>Character type</TD><TD>The type of the characters in the associated sequence</TD></TR> |
|---|
| 62 | <TR><TD>Category</TD><TD>A category tag convertible to <A HREF="../guide/traits.html#category_tags"><CODE>closable_tag</CODE></A></TD></TR> |
|---|
| 63 | </TABLE> |
|---|
| 64 | |
|---|
| 65 | <H2>Notation</H2> |
|---|
| 66 | |
|---|
| 67 | <TABLE CELLPADDING="2"> |
|---|
| 68 | <TR><TD><CODE>C</CODE></TD><TD>-</TD><TD>A type which is a model of Closable</TD></TR> |
|---|
| 69 | <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> |
|---|
| 70 | <TR><TD><CODE>c</CODE></TD><TD>-</TD><TD>Object of type <CODE>C</CODE></TD></TR> |
|---|
| 71 | <TR><TD><CODE>d</CODE></TD><TD>-</TD><TD>Object of type <CODE>D</CODE></TD></TR> |
|---|
| 72 | <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> |
|---|
| 73 | </TABLE> |
|---|
| 74 | |
|---|
| 75 | <H2>Valid Expressions / Semantics</H2> |
|---|
| 76 | |
|---|
| 77 | <P>Same as <A HREF="filter.html">Filter</A> or <A HREF="device.html">Device</A>, with the following additional requirements:</P> |
|---|
| 78 | |
|---|
| 79 | <TABLE CELLPADDING="5" BORDER="1"> |
|---|
| 80 | <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Precondition</TH><TH>Semantics</TH><TH>Postcondition</TH></TR> |
|---|
| 81 | <TR> |
|---|
| 82 | <TD> |
|---|
| 83 | <PRE CLASS="plain_code"><CODE><A HREF="../functions/close.html#close_device">boost::iostreams::close</A>(c, w)</CODE></PRE> |
|---|
| 84 | </TD> |
|---|
| 85 | <TD><CODE>void</CODE></TD> |
|---|
| 86 | <TD>Category convertible to <A HREF="../guide/traits.html#category_tags"><CODE>device_tag</CODE></A></TD> |
|---|
| 87 | <TD> |
|---|
| 88 | If <CODE>w</CODE> is <CODE>std::ios_base::out</CODE>, writes zero or more characters to the output sequence |
|---|
| 89 | </TD> |
|---|
| 90 | <TD><CODE>c</CODE> is ready to begin processing a new character sequence</TD> |
|---|
| 91 | </TR> |
|---|
| 92 | <TR> |
|---|
| 93 | <TD> |
|---|
| 94 | <PRE CLASS="plain_code"><CODE><A HREF="../functions/close.html#close_filter">boost::iostreams::close</A>(c, d, w)</CODE></PRE> |
|---|
| 95 | </TD> |
|---|
| 96 | <TD><CODE>void</CODE></TD> |
|---|
| 97 | <TD>Category convertible to <A HREF="../guide/traits.html#category_tags"><CODE>filter_tag</CODE></A></TD> |
|---|
| 98 | <TD> |
|---|
| 99 | <P> |
|---|
| 100 | 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> |
|---|
| 101 | </P> |
|---|
| 102 | </TD> |
|---|
| 103 | <TD><CODE>c</CODE> is ready to begin processing a new character sequence</TD> |
|---|
| 104 | </TR> |
|---|
| 105 | </TABLE> |
|---|
| 106 | |
|---|
| 107 | <H2>Exceptions</H2> |
|---|
| 108 | |
|---|
| 109 | <P> |
|---|
| 110 | 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. |
|---|
| 111 | </P> |
|---|
| 112 | |
|---|
| 113 | <H2>Models</H2> |
|---|
| 114 | |
|---|
| 115 | <P>Many of the Filters and Devices provided by the Iostreams library are Closable, but this is an implementation detail.</P> |
|---|
| 116 | |
|---|
| 117 | <!-- Begin Footer --> |
|---|
| 118 | |
|---|
| 119 | <HR> |
|---|
| 120 | <P CLASS="copyright">Revised |
|---|
| 121 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
|---|
| 122 | 20 May, 2004 |
|---|
| 123 | <!--webbot bot="Timestamp" endspan i-checksum="38504" --> |
|---|
| 124 | </P> |
|---|
| 125 | |
|---|
| 126 | <P CLASS="copyright">© Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P> |
|---|
| 127 | <P CLASS="copyright"> |
|---|
| 128 | 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>) |
|---|
| 129 | </P> |
|---|
| 130 | |
|---|
| 131 | <!-- End Footer --> |
|---|
| 132 | |
|---|
| 133 | </BODY> |
|---|