| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | 
|---|
| 2 | <HTML> | 
|---|
| 3 | <HEAD> | 
|---|
| 4 |     <TITLE>Source</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">Source</H1> | 
|---|
| 13 |     <HR CLASS="banner"> | 
|---|
| 14 |  | 
|---|
| 15 | <!-- End Banner --> | 
|---|
| 16 |  | 
|---|
| 17 | <H2>Definition</H2> | 
|---|
| 18 |  | 
|---|
| 19 | <P> | 
|---|
| 20 |     A Source is a <A HREF="device.html">Device</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#input">input</A>.  | 
|---|
| 21 | </P> | 
|---|
| 22 |  | 
|---|
| 23 | <H2>Description</H2> | 
|---|
| 24 |  | 
|---|
| 25 | <P>A Source provides read-access to a sequence of characters of a given type. In general, a Source may expose this sequence in two ways:</P>  | 
|---|
| 26 |  | 
|---|
| 27 | <OL> | 
|---|
| 28 |     <LI STYLE="list-style-type:lower-roman"> | 
|---|
| 29 |         by defining a member function <CODE>read</CODE>; or | 
|---|
| 30 |     </LI> | 
|---|
| 31 |     <LI STYLE="list-style-type:lower-roman"> | 
|---|
| 32 |         by defining a member function <CODE>input_sequence</CODE> returning a pair of pointers delimiting the sequence in its entirety. | 
|---|
| 33 |     </LI> | 
|---|
| 34 | </OL> | 
|---|
| 35 |  | 
|---|
| 36 | <P>As a special case, Boost.Iostreams treats standard input streams as Sources. (For details, see <A HREF="../functions/read.html"><CODE>read</CODE></A>.)</P> | 
|---|
| 37 |  | 
|---|
| 38 | <P>The mode of a Source is <A HREF="../guide/modes.html#input"><CODE>input</CODE></A> or one of its refinements.</P> | 
|---|
| 39 |  | 
|---|
| 40 | <H2>Note</H2> | 
|---|
| 41 |  | 
|---|
| 42 | <P>To be usable with the streams and stream buffers provided by the Boost Iostreams library, Sources must model <A HREF="blocking.html">Blocking</A>. | 
|---|
| 43 |  | 
|---|
| 44 | <H2>Example</H2> | 
|---|
| 45 |  | 
|---|
| 46 | <P> | 
|---|
| 47 |     A model of Source can be defined as follows: | 
|---|
| 48 | </P> | 
|---|
| 49 |  | 
|---|
| 50 | <PRE CLASS="broken_ie"><SPAN CLASS="keyword">struct</SPAN> Source { | 
|---|
| 51 |     <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN>        char_type; | 
|---|
| 52 |     <SPAN CLASS="keyword">typedef</SPAN> source_tag  category; | 
|---|
| 53 |     std::streamsize read(<SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n)  | 
|---|
| 54 |     { | 
|---|
| 55 |         <SPAN CLASS="comment">// Read up to n characters from the input  | 
|---|
| 56 |         // sequence into the buffer s, returning    | 
|---|
| 57 |         // the number of characters read, or -1  | 
|---|
| 58 |         // to indicate end-of-sequence.</SPAN> | 
|---|
| 59 |     } | 
|---|
| 60 | };</PRE> | 
|---|
| 61 |  | 
|---|
| 62 | <P> | 
|---|
| 63 |     Here <CODE>source_tag</CODE> is a <A HREF="../guide/traits.html#category_tags">category tag</A> identifying the type as a model of Source. Typically a Source can be defined by deriving from the helper classes <A HREF="../classes/device.html#synopsis"><CODE>source</CODE></A> or <A HREF="../classes/device.html#synopsis"><CODE>wsource</CODE></A> and defining a member function <CODE>read</CODE>. | 
|---|
| 64 |  | 
|---|
| 65 | <H2>Refinement of</H2> | 
|---|
| 66 |  | 
|---|
| 67 | <P><A HREF="device.html">Device</A>.</P> | 
|---|
| 68 |  | 
|---|
| 69 | <A NAME="types"></A> | 
|---|
| 70 | <H2>Associated Types</H2> | 
|---|
| 71 |  | 
|---|
| 72 | <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P> | 
|---|
| 73 |  | 
|---|
| 74 | <TABLE CELLPADDING="5" BORDER="1"> | 
|---|
| 75 |     <TR><TD>Category</TD><TD>A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>device_tag</CODE></A> and to <A HREF="../guide/modes.html#mode_tags"><CODE>input</CODE></A></TD></TR> | 
|---|
| 76 | </TABLE> | 
|---|
| 77 |  | 
|---|
| 78 | <A NAME="notation"></A> | 
|---|
| 79 | <H2>Notation</H2> | 
|---|
| 80 |  | 
|---|
| 81 | <TABLE CELLPADDING="2"> | 
|---|
| 82 |     <TR><TD><CODE>S</CODE></TD><TD>- A type which is a model of Source</TD></TR> | 
|---|
| 83 |     <TR><TD><CODE>Ch</CODE></TD><TD>- The character type</TD></TR> | 
|---|
| 84 |     <TR><TD><CODE>src</CODE></TD><TD>- Object of type <CODE>S</CODE></TD></TR> | 
|---|
| 85 |     <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>Ch*</CODE></SPAN></TD></TR> | 
|---|
| 86 |     <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR> | 
|---|
| 87 |     <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR> | 
|---|
| 88 | </TABLE> | 
|---|
| 89 |  | 
|---|
| 90 | <A NAME="expressions"></A> | 
|---|
| 91 | <H2>Valid Expressions / Semantics</H2> | 
|---|
| 92 |  | 
|---|
| 93 | <TABLE CELLPADDING="5" BORDER="1"> | 
|---|
| 94 |     <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR> | 
|---|
| 95 |     <TR> | 
|---|
| 96 |         <TD> | 
|---|
| 97 |             <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A><S>::type</CODE></PRE> | 
|---|
| 98 |         </TD> | 
|---|
| 99 |         <TD><CODE>typename</CODE> of the character type</TD> | 
|---|
| 100 |         <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD> | 
|---|
| 101 |     </TR> | 
|---|
| 102 |     <TR> | 
|---|
| 103 |         <TD> | 
|---|
| 104 |             <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A><S>::type</CODE></PRE> | 
|---|
| 105 |         </TD> | 
|---|
| 106 |         <TD><CODE>typename</CODE> of the category</TD> | 
|---|
| 107 |         <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD> | 
|---|
| 108 |     </TR> | 
|---|
| 109 |     <TR> | 
|---|
| 110 |         <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/read.html">io::read</A>(src, s, n)</CODE></PRE></TD> | 
|---|
| 111 |         <TD><CODE>std::streamsize</CODE></TD> | 
|---|
| 112 |         <TD>Not convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD> | 
|---|
| 113 |         <TD> | 
|---|
| 114 |             Reads up to <CODE>n</CODE> characters from the input sequence controlled by <CODE>dev</CODE> into <CODE>s</CODE>, returning the number of characters read, or <CODE>-1</CODE> to indicate end-of-sequence | 
|---|
| 115 |         </TD> | 
|---|
| 116 |     </TR> | 
|---|
| 117 |     <TR> | 
|---|
| 118 |         <TD><PRE CLASS="plain_code"><CODE>src.input_sequence()</CODE></PRE></TD> | 
|---|
| 119 |         <TD><PRE CLASS="plain_code"><CODE>std::pair<Ch*,Ch*></CODE></PRE></TD> | 
|---|
| 120 |         <TD>Convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD> | 
|---|
| 121 |         <TD>Returns a pair of pointers delimiting the sequence controlled by <CODE>src</CODE></TD> | 
|---|
| 122 |     </TR> | 
|---|
| 123 | </TABLE> | 
|---|
| 124 |  | 
|---|
| 125 | <H2>Exceptions</H2> | 
|---|
| 126 |  | 
|---|
| 127 | <P> | 
|---|
| 128 |     Errors which occur during the execution of member functions <CODE>read</CODE> or <CODE>input_sequence</CODE> are indicated by throwing exceptions. Reaching the end of the sequence is not an error. | 
|---|
| 129 | </P> | 
|---|
| 130 |  | 
|---|
| 131 | <P> | 
|---|
| 132 |     After an exception is thrown, a Source must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour. | 
|---|
| 133 | </P> | 
|---|
| 134 |  | 
|---|
| 135 | <H2>Models</H2> | 
|---|
| 136 |  | 
|---|
| 137 | <UL> | 
|---|
| 138 |     <LI>Standard input streams and stream buffers. | 
|---|
| 139 |     <LI><A HREF="../classes/array.html#array_source"><CODE>array_source</CODE></A>, <A HREF="../classes/file.html#file_source"><CODE>file_source</CODE></A>, <A HREF="../classes/file_descriptor.html#file_descriptor_source"><CODE>file_descriptor_source</CODE></A>, <A HREF="../classes/mapped_file.html#mapped_file_source"><CODE>mapped_file_source</CODE></A>. | 
|---|
| 140 | </UL> | 
|---|
| 141 |  | 
|---|
| 142 | <!-- Begin Footer --> | 
|---|
| 143 |  | 
|---|
| 144 | <HR> | 
|---|
| 145 | <P CLASS="copyright">Revised | 
|---|
| 146 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> | 
|---|
| 147 | 20 May, 2004 | 
|---|
| 148 | <!--webbot bot="Timestamp" endspan i-checksum="38504" --> | 
|---|
| 149 | </P> | 
|---|
| 150 |  | 
|---|
| 151 | <P CLASS="copyright">© Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P> | 
|---|
| 152 | <P CLASS="copyright">  | 
|---|
| 153 |     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>) | 
|---|
| 154 | </P> | 
|---|
| 155 |  | 
|---|
| 156 | <!-- End Footer --> | 
|---|
| 157 |  | 
|---|
| 158 | </BODY> | 
|---|