barrier/lib/base/CStopwatch.cpp

127 lines
1.9 KiB
C++
Raw Normal View History

2002-08-02 23:57:46 +04:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
2001-10-06 18:13:28 +04:00
#include "CStopwatch.h"
#include "CArch.h"
2001-10-06 18:13:28 +04:00
//
// CStopwatch
//
CStopwatch::CStopwatch(bool triggered) :
m_mark(0.0),
m_triggered(triggered),
m_stopped(triggered)
2001-10-06 18:13:28 +04:00
{
if (!triggered) {
m_mark = ARCH->time();
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::~CStopwatch()
{
// do nothing
}
double
CStopwatch::reset()
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
const double dt = m_mark;
m_mark = 0.0;
return dt;
}
else {
const double t = ARCH->time();
2001-10-06 18:13:28 +04:00
const double dt = t - m_mark;
m_mark = t;
return dt;
}
}
void
CStopwatch::stop()
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
2001-10-06 18:13:28 +04:00
return;
}
2001-10-06 18:13:28 +04:00
// save the elapsed time
m_mark = ARCH->time() - m_mark;
2001-10-06 18:13:28 +04:00
m_stopped = true;
}
void
CStopwatch::start()
2001-10-06 18:13:28 +04:00
{
m_triggered = false;
if (!m_stopped) {
2001-10-06 18:13:28 +04:00
return;
}
2001-10-06 18:13:28 +04:00
// set the mark such that it reports the time elapsed at stop()
m_mark = ARCH->time() - m_mark;
2001-10-06 18:13:28 +04:00
m_stopped = false;
}
void
CStopwatch::setTrigger()
2001-10-06 18:13:28 +04:00
{
stop();
m_triggered = true;
}
double
CStopwatch::getTime()
2001-10-06 18:13:28 +04:00
{
if (m_triggered) {
const double dt = m_mark;
start();
return dt;
}
else if (m_stopped) {
2001-10-06 18:13:28 +04:00
return m_mark;
}
else {
return ARCH->time() - m_mark;
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::operator double()
{
return getTime();
}
bool
CStopwatch::isStopped() const
2001-10-06 18:13:28 +04:00
{
return m_stopped;
}
double
CStopwatch::getTime() const
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
2001-10-06 18:13:28 +04:00
return m_mark;
}
else {
return ARCH->time() - m_mark;
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::operator double() const
{
return getTime();
}