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><boost/iostreams/filter/counter.hpp></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><<SPAN CLASS='keyword'>typename</SPAN> <A CLASS='documented' HREF='#template_params'>Ch</A>> 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<<SPAN CLASS='keyword'>char</SPAN>> <SPAN CLASS='defined'>counter</SPAN>; 62<SPAN CLASS='keyword'>typedef</SPAN> basic_counter<<SPAN CLASS='keyword'>wchar_t</SPAN>> <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'><boost/iostreams/device/file.hpp></SPAN></A> 111<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filter/counter.hpp'><SPAN CLASS='literal'><boost/iostreams/filter/counter.hpp></SPAN></A> 112<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filtering_stream.hpp'><SPAN CLASS='literal'><boost/iostreams/filtering_stream.hpp></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<<SPAN CLASS='literal'>0</SPAN>, counter>()->lines(); 123 <SPAN CLASS='keyword'>int</SPAN> characters = in.component<<SPAN CLASS='literal'>0</SPAN>, counter>()->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'><boost/iostreams/device/file.hpp></SPAN></A> 129<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filter/counter.hpp'><SPAN CLASS='literal'><boost/iostreams/filter/counter.hpp></SPAN></A> 130<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/iostreams/filtering_stream.hpp'><SPAN CLASS='literal'><boost/iostreams/filtering_stream.hpp></SPAN></A> 131<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS='header' HREF='../../../../boost/ref.hpp'><SPAN CLASS='literal'><boost/ref.hpp></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 150<P CLASS="copyright">© Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>© Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P> 151<P CLASS="copyright"> 152 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>) 153</P> 154 155<!-- End Footer --> 156 157</BODY> 158