Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/iostreams/doc/classes/counter.html @ 35

Last change on this file since 35 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 8.3 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<HTML>
3<HEAD>
4    <TITLE>Class Template counter</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">Class Template <CODE>basic_counter</CODE></H1>
13    <HR CLASS="banner">
14
15<!-- End Banner -->
16
17<DL class="page-index">
18  <DT><A href="#description">Description</A></DT>
19  <DT><A href="#headers">Headers</A></DT>
20  <DT><A href="#reference">Reference</A></DT>
21  <DT><A href="#examples">Examples</A></DT>
22</DL>
23
24<HR>
25
26<A NAME="description"></A>
27<H2>Description</H2>
28
29<P>
30    The class template <CODE>basic_counter</CODE> is a <A HREF='../concepts/dual_use_filter.html'>DualUseFilter</A> which forwards data unmodified to the next filter in a chain, keeping track of the number of characters and lines it has processed.
31</P>
32<P>
33    <CODE>basic_counter</CODE> is <A HREF='../concepts/optimally_buffered.html'>OptimallyBuffered</A> with an optimal buffer size of <CODE>0</CODE> to keep the character and line counts accurate. When a <CODE>basic_counter</CODE> is used for output, the line and character counts should exactly reflect the data which has been passed to downstream fillters. When a <CODE>basic_counter</CODE> is used for input, the character counts may deviate slightly from the number of characters that have been read from downstream the filters because of the putback buffer. The line count should exactly reflect the number of lines that have been read from downstream, except when the first character of a line is being read, in which case the line count may be off by one.
34</P>
35
36<A NAME="headers"></A>
37<H2>Headers</H2>
38
39<DL class="page-index">
40  <DT><A CLASS="header" HREF="../../../../boost/iostreams/filter/counter.hpp"><CODE>&lt;boost/iostreams/filter/counter.hpp&gt;</CODE></A></DT>
41</DL>
42
43<A NAME="reference"></A>
44<H2>Reference</H2>
45
46<H4>Synopsis</H4>
47
48<PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams {
49
50<SPAN CLASS='keyword'>template</SPAN>&lt;<SPAN CLASS='keyword'>typename</SPAN> <A CLASS='documented' HREF='#template_params'>Ch</A>&gt;
51<SPAN CLASS='keyword'>class</SPAN> <A CLASS='documented' HREF='#template_params'>basic_counter</A> {
52<SPAN CLASS='keyword'>public:</SPAN>
53    <SPAN CLASS='keyword'>typedef</SPAN> Ch                                char_type;
54    <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>typename</SPAN> [implmentation defined]  category;
55    <SPAN CLASS='keyword'>explicit</SPAN> <A CLASS='documented' HREF='#basic_counter_ctor'>basic_counter</A>(<SPAN CLASS='keyword'>int</SPAN> first_line = <SPAN CLASS='literal'>0</SPAN>, <SPAN CLASS='keyword'>int</SPAN> first_char = <SPAN CLASS='literal'>0</SPAN>);
56    <SPAN CLASS='keyword'>int</SPAN> <A CLASS='documented' HREF='#lines'>lines</A>() <SPAN CLASS='keyword'>const</SPAN>;
57    <SPAN CLASS='keyword'>int</SPAN> <A CLASS='documented' HREF='#characters'>characters</A>() <SPAN CLASS='keyword'>const</SPAN>;
58    std::streamsize <A CLASS='documented' HREF='#optimal_buffer_size'>optimal_buffer_size</A>() <SPAN CLASS='keyword'>const</SPAN>;
59};
60
61<SPAN CLASS='keyword'>typedef</SPAN> basic_counter&lt;<SPAN CLASS='keyword'>char</SPAN>&gt;     <SPAN CLASS='defined'>counter</SPAN>;
62<SPAN CLASS='keyword'>typedef</SPAN> basic_counter&lt;<SPAN CLASS='keyword'>wchar_t</SPAN>&gt;  <SPAN CLASS='defined'>wcounter</SPAN>;
63
64} } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE>
65
66<A NAME="template_params"></A>
67<H4>Template parameters</H4>
68
69<TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
70<TR>
71    <TR>
72        <TD VALIGN="top"><I>Ch</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
73        <TD>The <A HREF='../guide/traits.html#char_type'>character type</A></TD>
74    </TR>
75</TABLE>
76
77<A NAME="basic_counter_ctor"></A>
78<H4><CODE>counter::counter</CODE></H4>
79
80<PRE CLASS="broken_ie">    <SPAN CLASS='keyword'>explicit</SPAN> basic_counter(<SPAN CLASS='keyword'>int</SPAN> first_line = <SPAN CLASS='literal'>0</SPAN>, <SPAN CLASS='keyword'>int</SPAN> first_char = <SPAN CLASS='literal'>0</SPAN>);</PRE>
81
82<P>Constructs a <CODE>basic_counter</CODE> with the given initial counts.</P>
83
84<A NAME="lines"></A>
85<H4><CODE>counter::lines</CODE></H4>
86
87<PRE CLASS="broken_ie">    <SPAN CLASS='keyword'>int</SPAN> lines() <SPAN CLASS='keyword'>const</SPAN>;</PRE>
88
89<P>Returns the current line count.</P>
90
91<A NAME="characters"></A>
92<H4><CODE>counter::characters</CODE></H4>
93
94<PRE CLASS="broken_ie">    <SPAN CLASS='keyword'>int</SPAN> characters() <SPAN CLASS='keyword'>const</SPAN>;</PRE>
95
96<P>Returns the current character count.</P>
97
98<A NAME="optimal_buffer_size"></A>
99<H4><CODE>counter::optimal_buffer_size</CODE></H4>
100
101<PRE CLASS="broken_ie">    std::streamsize optimal_buffer_size() <SPAN CLASS='keyword'>const</SPAN>;</PRE>
102
103<P>Returns <CODE>0</CODE>.</P>
104
105<A NAME="examples"></A>
106<H2>Examples</H2>
107
108<P>The following example illustrates one way of obtaining the line and character counts after a <CODE>basic_counter</CODE> has been added to a filter chain: the <A HREF='filtering_stream.html'><CODE>filtering_stream</CODE></A> member function <A HREF='filtering_stream.html#component'><CODE>component</CODE></A> to obtain a pointer to <CODE>basic_counter</CODE>.</P>
109
110<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/device/file.hpp'><SPAN CLASS='literal'>&lt;boost/iostreams/device/file.hpp&gt;</SPAN></A>
111<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filter/counter.hpp'><SPAN CLASS='literal'>&lt;boost/iostreams/filter/counter.hpp&gt;</SPAN></A>
112<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>
113
114<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
115
116<SPAN CLASS='keyword'>int</SPAN> main()
117{
118    io::filtering_istream in;
119    in.push(io::counter());
120    in.push(io::file_source(<SPAN CLASS='literal'>"poem.txt"</SPAN>));
121    <SPAN CLASS='comment'>// read from in</SPAN>
122    <SPAN CLASS='keyword'>int</SPAN> lines = in.component&lt;<SPAN CLASS='literal'>0</SPAN>, counter&gt;()-&gt;lines();
123    <SPAN CLASS='keyword'>int</SPAN> characters = in.component&lt;<SPAN CLASS='literal'>0</SPAN>, counter&gt;()-&gt;characters();
124}</PRE>
125
126<P>The following example illustrates a second way of obtaining the line and character counts: add the <CODE>basic_counter</CODE> to the filter chain by referece, using <A HREF='../../../../doc/html/ref.html'><CODE>boost::ref</CODE></A>, and access the <CODE>basic_counter</CODE> directly.
127
128<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/device/file.hpp'><SPAN CLASS='literal'>&lt;boost/iostreams/device/file.hpp&gt;</SPAN></A>
129<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filter/counter.hpp'><SPAN CLASS='literal'>&lt;boost/iostreams/filter/counter.hpp&gt;</SPAN></A>
130<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>
131<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/ref.hpp'><SPAN CLASS='literal'>&lt;boost/ref.hpp&gt;</SPAN></A>
132
133<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
134
135<SPAN CLASS='keyword'>int</SPAN> main()
136{
137    io::counter cnt;
138    io::filtering_ostreams out;
139    out.push(boost::ref(cnt));
140    out.push(io::file_sink(<SPAN CLASS='literal'>"log.txt"</SPAN>));
141    <SPAN CLASS='comment'>// write to out</SPAN>
142    <SPAN CLASS='keyword'>int</SPAN> lines = cnt.lines();
143    <SPAN CLASS='keyword'>int</SPAN> characters = cnt.characters();
144}</PRE>
145
146<!-- Begin Footer -->
147
148<HR>
149<P CLASS="copyright">Revised
150<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
15120 May, 2004
152<!--webbot bot="Timestamp" endspan i-checksum="38504" -->
153</P>
154
155<P CLASS="copyright">&copy; Copyright <A HREF="http://www.kangaroologic.com" TARGET="_top">Jonathan Turkanis</A>, 2004</P>
156<P CLASS="copyright"> 
157    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>)
158</P>
159
160<!-- End Footer -->
161
162</BODY>
Note: See TracBrowser for help on using the repository browser.