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"
|
2003-01-05 01:01:32 +03:00
|
|
|
#include "CArch.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CStopwatch
|
|
|
|
//
|
|
|
|
|
|
|
|
CStopwatch::CStopwatch(bool triggered) :
|
2002-06-10 20:49:46 +04:00
|
|
|
m_mark(0.0),
|
|
|
|
m_triggered(triggered),
|
|
|
|
m_stopped(triggered)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (!triggered) {
|
2003-01-05 01:01:32 +03:00
|
|
|
m_mark = ARCH->time();
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::~CStopwatch()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
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 {
|
2003-01-05 01:01:32 +03:00
|
|
|
const double t = ARCH->time();
|
2001-10-06 18:13:28 +04:00
|
|
|
const double dt = t - m_mark;
|
|
|
|
m_mark = t;
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::stop()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// save the elapsed time
|
2003-01-05 01:01:32 +03:00
|
|
|
m_mark = ARCH->time() - m_mark;
|
2001-10-06 18:13:28 +04:00
|
|
|
m_stopped = true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::start()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_triggered = false;
|
2002-06-10 20:49:46 +04:00
|
|
|
if (!m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// set the mark such that it reports the time elapsed at stop()
|
2003-01-05 01:01:32 +03:00
|
|
|
m_mark = ARCH->time() - m_mark;
|
2001-10-06 18:13:28 +04:00
|
|
|
m_stopped = false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CStopwatch::setTrigger()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
m_triggered = true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::getTime()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (m_triggered) {
|
|
|
|
const double dt = m_mark;
|
|
|
|
start();
|
|
|
|
return dt;
|
|
|
|
}
|
2002-06-10 20:49:46 +04:00
|
|
|
else if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
|
|
|
else {
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->time() - m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::operator double()
|
|
|
|
{
|
|
|
|
return getTime();
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
bool
|
|
|
|
CStopwatch::isStopped() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return m_stopped;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
double
|
|
|
|
CStopwatch::getTime() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-10 20:49:46 +04:00
|
|
|
if (m_stopped) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
|
|
|
else {
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->time() - m_mark;
|
2002-06-10 20:49:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CStopwatch::operator double() const
|
|
|
|
{
|
|
|
|
return getTime();
|
|
|
|
}
|