CStopwatch.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "CStopwatch.h"
00016 #include "CArch.h"
00017 
00018 //
00019 // CStopwatch
00020 //
00021 
00022 CStopwatch::CStopwatch(bool triggered) :
00023     m_mark(0.0),
00024     m_triggered(triggered),
00025     m_stopped(triggered)
00026 {
00027     if (!triggered) {
00028         m_mark = ARCH->time();
00029     }
00030 }
00031 
00032 CStopwatch::~CStopwatch()
00033 {
00034     // do nothing
00035 }
00036 
00037 double
00038 CStopwatch::reset()
00039 {
00040     if (m_stopped) {
00041         const double dt = m_mark;
00042         m_mark = 0.0;
00043         return dt;
00044     }
00045     else {
00046         const double t  = ARCH->time();
00047         const double dt = t - m_mark;
00048         m_mark = t;
00049         return dt;
00050     }
00051 }
00052 
00053 void
00054 CStopwatch::stop()
00055 {
00056     if (m_stopped) {
00057         return;
00058     }
00059 
00060     // save the elapsed time
00061     m_mark    = ARCH->time() - m_mark;
00062     m_stopped = true;
00063 }
00064 
00065 void
00066 CStopwatch::start()
00067 {
00068     m_triggered = false;
00069     if (!m_stopped) {
00070         return;
00071     }
00072 
00073     // set the mark such that it reports the time elapsed at stop()
00074     m_mark    = ARCH->time() - m_mark;
00075     m_stopped = false;
00076 }
00077 
00078 void
00079 CStopwatch::setTrigger()
00080 {
00081     stop();
00082     m_triggered = true;
00083 }
00084 
00085 double
00086 CStopwatch::getTime()
00087 {
00088     if (m_triggered) {
00089         const double dt = m_mark;
00090         start();
00091         return dt;
00092     }
00093     else if (m_stopped) {
00094         return m_mark;
00095     }
00096     else {
00097         return ARCH->time() - m_mark;
00098     }
00099 }
00100 
00101 CStopwatch::operator double()
00102 {
00103     return getTime();
00104 }
00105 
00106 bool
00107 CStopwatch::isStopped() const
00108 {
00109     return m_stopped;
00110 }
00111 
00112 double
00113 CStopwatch::getTime() const
00114 {
00115     if (m_stopped) {
00116         return m_mark;
00117     }
00118     else {
00119         return ARCH->time() - m_mark;
00120     }
00121 }
00122 
00123 CStopwatch::operator double() const
00124 {
00125     return getTime();
00126 }

Generated on Fri Nov 6 00:18:45 2009 for synergy-plus by  doxygen 1.4.7