📄 passivetimer.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// $Id: passivetimer.cpp,v 1.7 2001/05/27 12:48:20 rosimildo Exp $
//
// Copyright (c) 1996-2000 ConnectTel, Inc. All rights reserved.
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies at the top.
//
// MODULE DESCRIPTION:
// This module implements a passive timer used to check if a timeout
// has been elapsed. The basic idea is to have a "free-running-counter"
// with increments in milliseconds. Internally the class keep the
// difference of the counter of two points in time.
//
// MODIFICATION/HISTORY:
//
// $Log: passivetimer.cpp,v $
// Revision 1.7 2001/05/27 12:48:20 rosimildo
// Updated to get passive timer working on Linux.
//
// Revision 1.6 2001/03/17 23:33:03 rosimildo
// Compiled with VC++.
//
// Revision 1.5 2001/03/13 03:00:50 rosimildo
// Updated to work under Ecos.
//
//
// Created 2001. Rosimildo da Silva, ConnectTel Inc. [rdasilva@connecttel.com]
/////////////////////////////////////////////////////////////////////////////
/**
* This timer class could be used to set timeouts, alarms and others
* periodical events that might happen in your system.
*
* @version 1.0, 06/19/96
* @author Rosimildo da Silva
*/
#include <stdio.h>
#include <time.h>
// could not get clock() working on linux...
#ifdef __linux__
#include <sys/timeb.h>
#endif
#include "passivetimer.h"
PassiveTimer::PassiveTimer(unsigned long duration)
{
interval = duration;
reset();
}
void PassiveTimer::setNewInterval(unsigned long duration)
{
interval = duration;
reset();
}
/**
* Make the current time to be the begin of the interval to be measured.
*/
void PassiveTimer::reset()
{
initialRef = getMsecs();
}
/**
* Check if the time elapsed is longer than the one specified
* by the parameter.
* @param tm - interval to be checked against.
* @return true - it has timed out; false - it has not timed out;
*/
bool PassiveTimer::timed_out() const
{
if(getMsecs() - initialRef >= interval)
return true;
else
return false;
}
unsigned long PassiveTimer::elapsed() const
{
return getMsecs() - initialRef;
}
unsigned long PassiveTimer::getMsecs()
{
#ifdef __linux__
struct timeb tb;
ftime( &tb );
return (tb.time*1000+tb.millitm);
#else
return ( ( clock()*1000 )/CLOCKS_PER_SEC );
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -