Boost库的progress_timer和progress_display讲解

一、概述

progress_timerprogress_display这两个类定义在文件progress.hppprogress_timer是继承自timer的一个定时器,progress_timer的行为类似timer函数,只是progress_timer的析构函数在适当的地方以正确的形式展示一条消逝的时间信息。也就是说,该函数会在析构时自动输出时间。progress_display在适当的地方以适当的形式显示已程序的执行过程,也就是一个进度条,这样它可以提供一个友好的用户界面。

二、类的代码详解

#ifndef BOOST_PROGRESS_HPP
#define BOOST_PROGRESS_HPP
#include <boost/timer.hpp>
#include <boost/noncopyable.hpp>
#include <boost/cstdint.hpp>  //为是使用这个变量uintmax_t
#include <iostream>        
#include <string>            
namespace boost {
// progress_timer类和timer一样,除了析构函数能在合适的地方以适当的形式显示已经消逝的时间信息
class progress_timer : public timer, private noncopyable
{
 public:
  explicit progress_timer( std::ostream & os = std::cout )
     // os显示: 执行可能会被忽略,特别是在嵌入式系统中
     : timer(), noncopyable(), 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;
};

// progress_display在适当的地方一正确的形式显示一个程序的执行过程,也就是进度条
class progress_display : private noncopyable
{
 public:
  explicit progress_display( unsigned long expected_count_,
                             std::ostream & os = std::cout,
                             const std::string & s1 = "\n",
                             const std::string & s2 = "",
                             const std::string & s3 = "" )
  // os显示: 执行可能会被忽略,特别是在嵌入式系统中
   : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }

  void  restart( unsigned long expected_count_ )
  //  显示合适的比例
  //  后置条件: count()==0, expected_count()==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  
         << m_s3;
    if ( !_expected_count ) _expected_count = 1;  // 以0为分界线
  }

  unsigned long  operator+=( unsigned long increment )
  //  如果有需要,显示一个适当的过程
  //  后置条件: count()== original count() + increment
  //  返回: count().
  {
    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()
  {
    //使用浮点型能确保大小计数器都能正常运行,static_cast<>() 在几个地方使用防止假的编译警告
    unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
        / static_cast<double>(_expected_count)) * 50.0);
    do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
    _next_tic_count =
      static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
    if ( _count == _expected_count ) {
      if ( _tic < 51 ) m_os << '*';
      m_os << std::endl;
      }
  }
};
}
#endif 
1、progress_timer详解

progress_timer继承自timer,因此它拥有timer的所有功能。但它又比timer多一些功能,构造函数explicit progress_timer( std::ostream & os = std::cout )默认的参数是std::cout,它允许将析构时的输出定向到指定的IO里。如果有特别的需求,可以用其他标准库输出流替换,或者使用cout.rdbuf()重定向cout输出。

下面的代码把progress_timer的输出转移到了stringstream中,他可以被转换为字符串供其他的应用使用

stringstream ss;
{
    progress_timer t(ss);
}
cout << ss.str();
2、progress_display详解

progress_display构造后,会显示下图所示的图形化用户界面

_expected_count表示用于进度条显示的基数,成员函数count()可以返回当前计数,当计数达到_expected_count时,表示任务执行完成。

operator+=、operator++用来增加计数,用字符“*”来显示进度百分比count()/expected_count。

static_cast<>() 在几个地方使用防止假的编译警告

三、使用案例

1.progress_timer的使用案例
#include <iostream>
#include <boost/progress.hpp>
using namespace boost;
using namespace std;

int main()
{
    progress_timer t;
    int i;
    for(i = 0; i < 100000; i++)
    {
        cout << "i=" << i << endl;
    }
    return 0;    
}

执行结果如下:
这里写图片描述

2. progress_display的使用案例

案例 1

#include <boost/progress.hpp>  
#include <iostream>  
#include <vector>  
using namespace std;  
using namespace boost;  

int main()
{
    vector<string> v(100, "aaa");
    v[10] = " "; v[23] = " ";
    ofstream fs("./test.txt");
    progress_display pd(v.size());

    for(auto post = v.begin(); post != v.end(); ++post)
    {
        fs << *post << endl;
        ++pd;    
        if(post->empty())
        {
            cout << "null string #" << (post - v.begin()) << endl;
        }
    }
}

由于forauto遍历是C++11的新特性,如果你的g++版本比较低的话,这段代码是编译不过去,所以编译这段代码你需要升级你的g++版本。也可以把auto改成迭代器vector<string>::iterator post = v.begin()

案例 2

#include <boost/progress.hpp>  
#include <iostream>  
#include <vector>  
#include <fstream>
using namespace std;  
using namespace boost; 
int main()
{
    vector<string> v(100);
    std::ofstream fs("./test.txt");
    progress_display pd(v.size());
    for(vector<string>::iterator it = v.begin(); it != v.end();)
    {
        fs << *it++ << endl;
        ++pd;    
    }
}

执行结果,表示执行到100%的时候
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
boost::asio::high_resolution_timer是一个定时器类,用于在boost::asio中进行高分辨率的定时操作。 boost::asio是一个用于网络和异步编程的C++。它提供了丰富的功能,包括异步IO操作、定时器、socket通信等。其中,boost::asio::high_resolution_timer是其中的一个定时器类,它使用了高分辨率的时钟来进行精确的定时操作。 使用boost::asio::high_resolution_timer,我们可以创建一个定时器对象,并设置定时的时间间隔。可以使用成员函数expires_from_now()指定定时的时间间隔,参数为一个duration类型的对象,表示时间间隔的长度。 例如,以下代码创建了一个定时器对象timer,设置了定时时间间隔为1秒: boost::asio::high_resolution_timer timer(io_context); timer.expires_from_now(boost::posix_time::seconds(1)); 然后,我们可以调用定时器对象的async_wait()函数来启动定时器,并指定一个回调函数,在定时器超时时被调用。回调函数可以是一个lambda函数,也可以是一个函数对象。 例如,以下代码定义了一个lambda函数作为回调函数: timer.async_wait([](const boost::system::error_code& ec) { if (!ec) { // 定时器超时,执行相应操作 } }); 在定时器超时时,回调函数会被触发,并执行相应操作。 总之,boost::asio::high_resolution_timer是一个用于高分辨率定时操作的定时器类,可以帮助我们在异步编程中进行精确的定时操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

The_Web3_社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值