]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/iostreams/doc/tutorial/container_source.html
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / iostreams / doc / tutorial / container_source.html
CommitLineData
7c673cae
FG
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<HTML>
3<HEAD>
4 <TITLE>Tutorial</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">Tutorial</H1>
13 <HR CLASS="banner">
14
15<!-- End Banner -->
16
17<!-- Begin Nav -->
18
19<DIV CLASS='nav'>
20 <A HREF='writing_devices.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
21 <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
22 <A HREF='container_sink.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
23</DIV>
24
25<!-- End Nav -->
26
27 <!-- Container Source -->
28
29<A NAME="container_source"></A>
30<H2>2.1.2. Writing a <CODE>container_source</CODE></H2>
31
32<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:
33
34<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
35<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// source_tag
36</SPAN>
37<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
38
39<SPAN CLASS='keyword'>class</SPAN> my_source {
40<SPAN CLASS='keyword'>public</SPAN>:
41 <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>char</SPAN> char_type;
42 <SPAN CLASS='keyword'>typedef</SPAN> source_tag category;
43
44 std::streamsize read(<SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n)
45 {
46 <SPAN CLASS='comment'>// Read up to n characters from the underlying data source</SPAN>
47 <SPAN CLASS='comment'>// into the buffer s, returning the number of characters</SPAN>
48 <SPAN CLASS='comment'>// read; return -1 to indicate EOF</SPAN>
49 }
50
51 <SPAN CLASS='comment'>/* Other members */</SPAN>
52};</PRE>
53
54<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>
55
56<P>The member function <CODE>read</CODE> reads up to <CODE>n</CODE> characters into the buffer <CODE>s</CODE> and returns the number of characters 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>
57
58<P>You could also write the above example as follows:</P>
59
60<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// source</SPAN>
61
62<SPAN CLASS='keyword'>class</SPAN> my_source : <SPAN CLASS='keyword'>public</SPAN> source {
63<SPAN CLASS='keyword'>public</SPAN>:
64 std::streamsize read(<SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n);
65
66 <SPAN CLASS='comment'>/* Other members */</SPAN>
67};</PRE>
68
69<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.
70
71<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>
72
73<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;algorithm&gt;</SPAN> <SPAN CLASS='comment'>// copy, min</SPAN>
74<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
75<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// source_tag</SPAN>
76
77<SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
78
79<SPAN CLASS='keyword'>template</SPAN>&lt;<SPAN CLASS='keyword'>typename</SPAN> Container&gt;
80<SPAN CLASS='keyword'>class</SPAN> container_source {
81<SPAN CLASS='keyword'>public</SPAN>:
82 <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>typename</SPAN> Container::value_type char_type;
83 <SPAN CLASS='keyword'>typedef</SPAN> source_tag category;
84 container_source(Container&amp; container)
85 : container_(container), pos_(<SPAN CLASS='numeric_literal'>0</SPAN>)
86 { }
87 std::streamsize read(char_type* s, std::streamsize n)
88 {
89 <SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
90 streamsize amt = <SPAN CLASS='keyword'>static_cast</SPAN>&lt;streamsize&gt;(container_.size() - pos_);
91 streamsize result = (min)(n, amt);
92 <SPAN CLASS='keyword'>if</SPAN> (result != <SPAN CLASS='numeric_literal'>0</SPAN>) {
93 std::copy( container_.begin() + pos_,
94 container_.begin() + pos_ + result,
95 s );
96 pos_ += result;
97 <SPAN CLASS='keyword'>return</SPAN> result;
98 } <SPAN CLASS='keyword'>else</SPAN> {
99 <SPAN CLASS='keyword'>return</SPAN> <SPAN CLASS='numeric_literal'>-1</SPAN>; <SPAN CLASS='comment'>// EOF</SPAN>
100 }
101 }
102 Container&amp; container() { <SPAN CLASS='keyword'>return</SPAN> container_; }
103<SPAN CLASS='keyword'>private</SPAN>:
104 <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>typename</SPAN> Container::size_type size_type;
105 Container&amp; container_;
106 size_type pos_;
107};
108
109} } } <SPAN CLASS='comment'>// End namespace boost::iostreams:example</SPAN></PRE>
110
111<P>Here, note that</P>
112<UL>
113<LI>The member type <CODE>char_type</CODE> is defined to be equal to the containers's <CODE>value_type</CODE>;
114<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
115<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>.
116</UL>
117
118<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>
119
120<P>You can read from a container_source as follows</P>
121
122<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;cassert&gt;</SPAN>
123<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;string&gt;</SPAN>
124<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/stream.hpp&gt;</SPAN></A>
125<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../example/container_device.hpp"><SPAN CLASS='literal'>&lt;libs/iostreams/example/container_device.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// container_source</SPAN>
126
127<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
128<SPAN CLASS='keyword'>namespace</SPAN> ex = boost::iostreams::example;
129
130<SPAN CLASS='keyword'>int</SPAN> main()
131{
132 <SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
133 <SPAN CLASS='keyword'>typedef</SPAN> ex::container_source&lt;string&gt; string_source;
134
135 string input = <SPAN CLASS='literal'>"Hello World!"</SPAN>;
136 string output;
137 io::stream&lt;string_source&gt; in(input);
138 getline(in, output);
139 assert(input == output);
140}</PRE>
141
142<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/html/range/reference/utilities/iterator_range.html" 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>
143
144<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;cassert&gt;</SPAN>
145<SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;string&gt;</SPAN>
146<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/filtering_stream.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/filtering_stream.hpp&gt;</SPAN></A>
147<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/range/iterator_range.hpp"><SPAN CLASS='literal'>&lt;boost/range/iterator_range.hpp&gt;</SPAN></A>
148
149<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
150
151<SPAN CLASS='keyword'>int</SPAN> main()
152{
153 <SPAN CLASS='keyword'>using</SPAN> <SPAN CLASS='keyword'>namespace</SPAN> std;
154
155 string input = <SPAN CLASS='literal'>"Hello World!"</SPAN>;
156 string output;
157 io::filtering_istream in(boost::make_iterator_range(input));
158 getline(in, output);
159 assert(input == output);
160}</PRE>
161
162<!-- Begin Nav -->
163
164<DIV CLASS='nav'>
165 <A HREF='writing_devices.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
166 <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
167 <A HREF='container_sink.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
168</DIV>
169
170<!-- End Nav -->
171
172<!-- Begin Footer -->
173
174<HR>
175
176
177<P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="http://www.coderage.com/turkanis/" target="_top">Jonathan Turkanis</a></P>
178<P CLASS="copyright">
179 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>)
180</P>
181<!-- End Footer -->
182
183</BODY>