BOOST source code brief analysis series - Timer (2)

zhaozj2021-02-16  41

BOOST source code brief analysis series - Timer (2)

I have long wanted to write out, but I have been busy with the code today. I finally have time to write, and I have solved a problem in the past, which is Timer, so it is also Gave me the power.

The remaining two classes, Grogress_Timer and Grogress_Display are defined in the header file progress.hpp, written by Beman Dawes, the latest version is published on November 1, 2001.

Class Progress_Timer

#ifndef boost_progress_hpp

#define boost_progress_hpp

...

#ENDIF

Again, this is a protective character that is in almost every header file.

#include // Because Progress_Timer inherits to class Timer, it is included.

#include // Timer definitions are also encountered in which the definition of class NoncopyAble is included. When Progress_Timer inherits this class, there is no copy constructor.

#include // boost defines a lot of data types.

#include

#include // These two have nothing to say, but now writing code is best or standard library, such as use String, not using char *.

Class Progress_timer: Public Timer, Private Noncopyable

{

PUBLIC:

Explicit Progress_Timer (std :: ostream & os = std :: cout)

: m_os (os) {}

~ Progress_timer ()

{

Try

{

Std :: istream :: fmtflags old_flags = m_os.setf (std :: istream :: fixed,

Std :: istream :: floatfield;

Std :: streamsize old_prec = m_os.Precision (2);

M_OS << ELAPSED () << "S / N" << std :: endl;

M_OS.FLAGS (OLD_FLAGS);

m_os.precision (old_prec);

}

Catch (...) {}

}

Private:

Std :: Ostream & M_OS;

}

From the code, Progress_timer is really simple, she is inherited in Timer and Noncopyable. The main code is in the destructor, meaning that the time of the object survives is displayed when it is analyzed.

The constructor is expressed with explicit not to hide the type conversion.

The function of calculating the passed time is ELAPSED (), which is inherited from Timer. To read this, I think the most complex is the operation of the output stream, which is involved, setting the output stream output accuracy for the output stream output format.

FMTFLAGS: is a data type defined in a standard warehouse, and her object represents a set of flags and integer values ​​for stream I / O operations. These markers have a lot, such as:

Boolpha expresses true and false in the form of symbols. DEC is output by 10-based output HEX at 16-based output Fixed floating point number output format showbase output prefix, octal is 0, and hexadecimal is 0x floatfield and floating point output. Unitbuf is refreshed after each output. ... Here is just a very few part, and more detailed description can be referred to the C Programming Language Chapter 21.

Std :: istream :: fmtflags old_flags = m_os.setf (std :: istream :: fixed,

Std :: istream :: floatfield;

So this statement means that the output format is set to the standard output object M_OS is a floating point format, and saves the original format, which is easy to complete the output.

Std :: streamsize old_prec = m_os.Precision (2);

This set, the output accuracy is 2.

The rest of the job is output and recovered.

Abnormal processing technology is used in this code.

Try

{

// ...

}

Catch (...) {// ...}

Here, it is used to capture all anomalies with Catch ...

2. Grogress_Display

This class seems to have no relationship with the two classes, and we can implement the progress of our task with a schedule in the console program.

When we build a class of objects, a progress bar ruler is displayed. When calling the Operator function, decide whether to grow the progress bar until the last stop of the ruler.

Class Progress_Display: Private Noncopyable

{

PUBLIC:

Explicit Progress_Display (unsigned long expected_count,

Std :: OSTREAM & OS = std :: cout,

Const std :: string & S1 = "/ n", // Leading strings

Const std :: string & s2 = "",

Const std :: string & s3 = "")

: M_OS (OS), M_S1 (S1), M_S2 (S2), M_S3 (S3) {Restart (Expected_count);

Void Restart (unsigned long expected_count)

{

_count = _next_tic_count = _tic = 0;

_EXPECTED_COUNT = Expected_count;

M_OS << m_s1 << "0% 10 20 30 40 50 60 70 80 90 100% / N"

<< m_s2 << "| ---- | ---- | ------------ | ---- | - | ---- | "

<< std :: endl // endl importl item flush, Which EnsuRES Display

<< m_s3;

IF (! _expected_count) _expected_count = 1;

}

Unsigned long operator = (unsigned long increment)

{

IF ((_count = increment)> = _next_tic_count) {Display_tic ();} return_count;

}

Unsigned long operator () {return operator = (1);

Unsigned long count () const {return_count;}

Unsigned long expected_count () const {return_expected_count;

Private:

Std :: Ostream & M_OS;

Const std :: string m_s1;

Const std :: string m_s2;

Const std :: string m_s3;

Unsigned long _count, _expected_count, _next_tic_count;

Unsigned int _tic;

Void Display_tic ()

{

unsigned int tics_needed =

Static_cast

(static_cast (_ count) / _ expected_count) * 50.0);

Do {m_OS << '*' << std :: flush;} while ( _ tic

_next_tic_count =

Static_cast (_ tic / 50.0) * _ expected_count);

IF (_count == _expected_count) {

IF (_tic <51) m_OS << '*';

M_OS << std :: endl;

}

}

}

This class code is also relatively simple, there are several Data Member needs to explain:

_Count calibration now counts what location. _EXPECTED_COUNT calibration count can reach the maximum number. _next_tic_count The next count position to play the progress mark. ...

When you create an object, call the RESTART function, display the scale, the scale, M_S1, M_S2, M_S3 calibration of the scale.

Then, each progress bar needs to be added to call Menber Function: Operator = (), in this function, _count = increment> = _next_tic_count, increase _count, see if the next position needs to increase the progress bar, If yes, call Menber Function: Display_tic ().

The most difficult place in this class is DISPLAY_TIC (). This function has made a few things. When this call is displayed, the schedule that should be displayed, calculates the position of the next progress bar, when the progress The bar is also the case where the _count counts reaches the maximum value, it is determined whether or not to play the point on the last progress bar.

When we use this class, it is necessary to create an object, and call Operator () when it increases the progress bar. It is also more convenient to use.

转载请注明原文地址:https://www.9cbs.com/read-27564.html

New Post(0)