• Main Page
  • Classes
  • Files
  • File List

CStopwatch.cpp

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

Generated on Tue May 21 2013 00:00:06 for Synergy by  doxygen 1.7.1