1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Tutorial</TITLE>
<LINK REL="stylesheet" HREF="../../../../boost.css">
<LINK REL="stylesheet" HREF="../theme/iostreams.css">
</HEAD>
<BODY>
<!-- Begin Banner -->
<H1 CLASS="title">Tutorial</H1>
<HR CLASS="banner">
<!-- End Banner -->
<!-- Begin Nav -->
<DIV CLASS='nav'>
<A HREF='writing_devices.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/prev.png'></A>
<A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/up.png'></A>
<A HREF='container_sink.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/next.png'></A>
</DIV>
<!-- End Nav -->
<!-- Container Source -->
<A NAME="container_source"></A>
<H2>2.1.2. Writing a <CODE>container_source</CODE></H2>
<P>Suppose you want to write a Device for reading characters from an STL container. A Device which only supports reading is called a <A HREF="../concepts/source.html">Source</A>. A typical narrow-character Source looks like this:
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal"><iosfwd></SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'><boost/iostreams/categories.hpp></SPAN></A> <SPAN CLASS='comment'>// source_tag
</SPAN>
<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
<SPAN CLASS='keyword'>class</SPAN> my_source {
<SPAN CLASS='keyword'>public</SPAN>:
<SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>char</SPAN> char_type;
<SPAN CLASS='keyword'>typedef</SPAN> source_tag category;
std::streamsize read(<SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n)
{
<SPAN CLASS='comment'>// Read up to n characters from the underlying data source</SPAN>
<SPAN CLASS='comment'>// into the buffer s, returning the number of characters</SPAN>
<SPAN CLASS='comment'>// read; return -1 to indicate EOF</SPAN>
}
<SPAN CLASS='comment'>/* Other members */</SPAN>
};</PRE>
<P>Here the member type <A HREF="../guide/traits.html#char_type"><CODE>char_type</CODE></A> indicates the type of characters handled by my_source, which will almost always be <CODE>char</CODE> or <CODE>wchar_t</CODE>. The member type <A HREF="../guide/traits.html#char_type">category</A> indicates which of the fundamental i/o operations are supported by the device. The category tag <A HREF="../guide/traits.html#category_tags"><CODE>source_tag</CODE></A> indicates that only <A HREF="../functions/read.html"><CODE>read</CODE></A> is supported.</P>
<P>The member function <CODE>read</CODE> reads up to <CODE>n</CODE> character into the buffer <CODE>s</CODE> and returns the number of character read, unless that number is <CODE>0</CODE> and end-of-stream has been reached, in which case the special value <CODE>-1</CODE> is returned. In general, a Source's member function <CODE>read</CODE> may return fewer characters than requested even though end-of-stream has not been reached; such Sources are called <I>non-blocking</I>. Non-blocking Devices do not interact well with standard streams and stream buffers, however, so most devices should be <A HREF="../concepts/blocking.html">Blocking</A>. <I>See</I> <A HREF="../guide/asynchronous.html">Asynchronous and Non-Blocking I/O</A>.</P>
<P>You could also write the above example as follows:</P>
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS='literal'><boost/iostreams/concepts.hpp></SPAN></A> <SPAN CLASS='comment'>// source</SPAN>
<SPAN CLASS='keyword'>class</SPAN> my_source : <SPAN CLASS='keyword'>public</SPAN> source {
<SPAN CLASS='keyword'>public</SPAN>:
std::streamsize read(<SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n);
<SPAN CLASS='comment'>/* Other members */</SPAN>
};</PRE>
<P>Here <A HREF="../classes/device.html#synopsis"><CODE>source</CODE></A> is a convenience base class which provides the member types <CODE>char_type</CODE> and <CODE>category</CODE>, as well as no-op implementations of member functions <CODE>close</CODE> and <CODE>imbue</CODE>, not needed here.
<P>You're now ready to write your <CODE>container_source</CODE>. For simplicity, let's assume that your container's iterators are RandomAccessIterators.</P>
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><algorithm></SPAN> <SPAN CLASS='comment'>// copy, min</SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><iosfwd></SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'><boost/iostreams/categories.hpp></SPAN></A> <SPAN CLASS='comment'>// source_tag</SPAN>
<SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
<SPAN CLASS='keyword'>template</SPAN><<SPAN CLASS='keyword'>typename</SPAN> Container>
<SPAN CLASS='keyword'>class</SPAN> container_source {
<SPAN CLASS='keyword'>public</SPAN>:
<SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>typename</SPAN> Container::value_type char_type;
<SPAN CLASS='keyword'>typedef</SPAN> source_tag category;
container_source(Container& container)
: container_(container), pos_(<SPAN CLASS='numeric_literal'>0</SPAN>)
{ }
std::streamsize read(char_type* s, std::streamsize n)
{
<SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
streamsize amt = <SPAN CLASS='keyword'>static_cast</SPAN><streamsize>(container_.size() - pos_);
streamsize result = (min)(n, amt);
<SPAN CLASS='keyword'>if</SPAN> (result != <SPAN CLASS='numeric_literal'>0</SPAN>) {
std::copy( container_.begin() + pos_,
container_.begin() + pos_ + result,
s );
pos_ += result;
<SPAN CLASS='keyword'>return</SPAN> result;
} <SPAN CLASS='keyword'>else</SPAN> {
<SPAN CLASS='keyword'>return</SPAN> <SPAN CLASS='numeric_literal'>-1</SPAN>; <SPAN CLASS='comment'>// EOF</SPAN>
}
}
Container& container() { <SPAN CLASS='keyword'>return</SPAN> container_; }
<SPAN CLASS='keyword'>private</SPAN>:
<SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>typename</SPAN> Container::size_type size_type;
Container& container_;
size_type pos_;
};
} } } <SPAN CLASS='comment'>// End namespace boost::iostreams:example</SPAN></PRE>
<P>Here, note that</P>
<UL>
<LI>The member type <CODE>char_type</CODE> is defined to be equal to the containers's <CODE>value_type</CODE>;
<LI>The member type <CODE>category</CODE> tells the Iostreams library that <CODE>container_source</CODE> is a model of <A HREF="../concepts/source.html">Source</A>; and
<LI>A <CODE>container_source</CODE> can be constructed from a instance of <CODE>Container</CODE>, which is passed and stored by reference, and accessible <I>via</I> the member function <CODE>container()</CODE>.
</UL>
<P>The main idea behind the implementation of <CODE>read()</CODE> is simple: First, you calculate the number of characters to be read, which is the minimum of the number of unread characters remaining in the container and the number of characters requested. Second, if the number of characters to be read is non-zero, you copy that number of characters from the container into the provided buffer and update the current read position. If the number of characters is zero, i.e., if all the characters in the container have already been consumed by previous calls to read (or if the container was empty to begin with), you return <CODE>-1</CODE> to indicate end-of-stream.</P>
<P>You can read from a container_source as follows</P>
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><cassert></SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><string></SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream.hpp"><SPAN CLASS='literal'><boost/iostreams/stream.hpp></SPAN></A>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../example/container_device.hpp"><SPAN CLASS='literal'><libs/iostreams/example/container_device.hpp></SPAN></A> <SPAN CLASS='comment'>// container_source</SPAN>
<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
<SPAN CLASS='keyword'>namespace</SPAN> ex = boost::iostreams::example;
<SPAN CLASS='keyword'>int</SPAN> main()
{
<SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
<SPAN CLASS='keyword'>typedef</SPAN> ex::container_source<string> string_source;
string input = <SPAN CLASS='literal'>"Hello World!"</SPAN>;
string output;
io::stream<string_source> in(input);
getline(in, output);
assert(input == output);
}</PRE>
<P>Finally, I should mention that the Iostreams library provides an easier way to read from an STL container: instances of <A HREF="../../../range/doc/utility_class.html#iter_range" TARGET="_top"><CODE>boost::iterator_range</CODE></A> can be added directly to <A HREF="../guide/filtering_streams.html">filtering streams and stream buffers</A>. So you could write:</P>
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><cassert></SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><string></SPAN>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/filtering_stream.hpp"><SPAN CLASS='literal'><boost/iostreams/filtering_stream.hpp></SPAN></A>
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/range/iterator_range.hpp"><SPAN CLASS='literal'><boost/range/iterator_range.hpp></SPAN></A>
<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
<SPAN CLASS='keyword'>int</SPAN> main()
{
<SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
string input = <SPAN CLASS='literal'>"Hello World!"</SPAN>;
string output;
io::filtering_istream in(boost::make_iterator_range(input));
getline(in, output);
assert(input == output);
}</PRE>
<!-- Begin Nav -->
<DIV CLASS='nav'>
<A HREF='writing_devices.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/prev.png'></A>
<A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/up.png'></A>
<A HREF='container_sink.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/html/images/next.png'></A>
</DIV>
<!-- End Nav -->
<!-- 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">© Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P>
<P CLASS="copyright">
Use, modification, and distribution are subject to the Boost Software License, Version 2.0. (See accompanying file <A HREF="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> 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>
|