Back to Arangodb

Class Template basic_counter

3rdParty/boost/1.78.0/libs/iostreams/doc/classes/counter.html

3.12.9.14.2 KB
Original Source

Class Template basic_counter

DescriptionHeadersReferenceExamples


Description

The class template basic_counter is a DualUseFilter which forwards data unmodified to the next filter in a chain, keeping track of the number of characters and lines it has processed.

basic_counter is OptimallyBuffered with an optimal buffer size of 0 to keep the character and line counts accurate. When a basic_counter is used for output, the line and character counts should exactly reflect the data which has been passed to downstream fillters. When a basic_counter 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.

Headers

<boost/iostreams/filter/counter.hpp>

Reference

Synopsis

namespaceboost {namespaceiostreams {template<typename[Ch](#template_params)>class[basic\_counter](#template_params){public:typedefCh char_type;typedeftypename[implmentation defined] category;explicit[basic\_counter](#basic_counter_ctor)(intfirst_line =0,intfirst_char =0);int[lines](#lines)()const;int[characters](#characters)()const;
    std::streamsize[optimal\_buffer\_size](#optimal_buffer_size)()const;
};typedefbasic_counter<char>counter;typedefbasic_counter<wchar\_t>wcounter;

} }// End namespace boost::io

Template parameters

| | | | Ch | - | The character type |

counter::counter

explicitbasic_counter(intfirst_line =0,intfirst_char =0);

Constructs a basic_counter with the given initial counts.

counter::lines

intlines()const;

Returns the current line count.

counter::characters

intcharacters()const;

Returns the current character count.

counter::optimal_buffer_size

std::streamsize optimal_buffer_size()const;

Returns 0.

Examples

The following example illustrates one way of obtaining the line and character counts after a basic_counter has been added to a filter chain: the filtering_stream member function component to obtain a pointer to basic_counter.

#include[\<boost/iostreams/device/file.hpp\>](../../../../boost/iostreams/device/file.hpp)#include[\<boost/iostreams/filter/counter.hpp\>](../../../../boost/iostreams/filter/counter.hpp)#include[\<boost/iostreams/filtering\_stream.hpp\>](../../../../boost/iostreams/filtering_stream.hpp)namespaceio = boost::iostreams;intmain()
{
    io::filtering_istream in;
    in.push(io::counter());
    in.push(io::file_source("poem.txt"));// read from inintlines = in.component<0, counter>()->lines();intcharacters = in.component<0, counter>()->characters();
}

The following example illustrates a second way of obtaining the line and character counts: add the basic_counter to the filter chain by referece, using boost::ref, and access the basic_counter directly.

#include[\<boost/iostreams/device/file.hpp\>](../../../../boost/iostreams/device/file.hpp)#include[\<boost/iostreams/filter/counter.hpp\>](../../../../boost/iostreams/filter/counter.hpp)#include[\<boost/iostreams/filtering\_stream.hpp\>](../../../../boost/iostreams/filtering_stream.hpp)#include[\<boost/ref.hpp\>](../../../../boost/ref.hpp)namespaceio = boost::iostreams;intmain()
{
    io::counter cnt;
    io::filtering_ostreams out;
    out.push(boost::ref(cnt));
    out.push(io::file_sink("log.txt"));// write to outintlines = cnt.lines();intcharacters = cnt.characters();
}

© Copyright 2008 CodeRage, LLC
© Copyright 2004-2007 Jonathan Turkanis

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)