| Line |  | 
|---|
| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software; you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include "CStopwatch.h" | 
|---|
| 16 | #include "CArch.h" | 
|---|
| 17 |  | 
|---|
| 18 | // | 
|---|
| 19 | // CStopwatch | 
|---|
| 20 | // | 
|---|
| 21 |  | 
|---|
| 22 | CStopwatch::CStopwatch(bool triggered) : | 
|---|
| 23 | m_mark(0.0), | 
|---|
| 24 | m_triggered(triggered), | 
|---|
| 25 | m_stopped(triggered) | 
|---|
| 26 | { | 
|---|
| 27 | if (!triggered) { | 
|---|
| 28 | m_mark = ARCH->time(); | 
|---|
| 29 | } | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | CStopwatch::~CStopwatch() | 
|---|
| 33 | { | 
|---|
| 34 | // do nothing | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | double | 
|---|
| 38 | CStopwatch::reset() | 
|---|
| 39 | { | 
|---|
| 40 | if (m_stopped) { | 
|---|
| 41 | const double dt = m_mark; | 
|---|
| 42 | m_mark = 0.0; | 
|---|
| 43 | return dt; | 
|---|
| 44 | } | 
|---|
| 45 | else { | 
|---|
| 46 | const double t  = ARCH->time(); | 
|---|
| 47 | const double dt = t - m_mark; | 
|---|
| 48 | m_mark = t; | 
|---|
| 49 | return dt; | 
|---|
| 50 | } | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | void | 
|---|
| 54 | CStopwatch::stop() | 
|---|
| 55 | { | 
|---|
| 56 | if (m_stopped) { | 
|---|
| 57 | return; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | // save the elapsed time | 
|---|
| 61 | m_mark    = ARCH->time() - m_mark; | 
|---|
| 62 | m_stopped = true; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void | 
|---|
| 66 | CStopwatch::start() | 
|---|
| 67 | { | 
|---|
| 68 | m_triggered = false; | 
|---|
| 69 | if (!m_stopped) { | 
|---|
| 70 | return; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | // set the mark such that it reports the time elapsed at stop() | 
|---|
| 74 | m_mark    = ARCH->time() - m_mark; | 
|---|
| 75 | m_stopped = false; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void | 
|---|
| 79 | CStopwatch::setTrigger() | 
|---|
| 80 | { | 
|---|
| 81 | stop(); | 
|---|
| 82 | m_triggered = true; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | double | 
|---|
| 86 | CStopwatch::getTime() | 
|---|
| 87 | { | 
|---|
| 88 | if (m_triggered) { | 
|---|
| 89 | const double dt = m_mark; | 
|---|
| 90 | start(); | 
|---|
| 91 | return dt; | 
|---|
| 92 | } | 
|---|
| 93 | else if (m_stopped) { | 
|---|
| 94 | return m_mark; | 
|---|
| 95 | } | 
|---|
| 96 | else { | 
|---|
| 97 | return ARCH->time() - m_mark; | 
|---|
| 98 | } | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | CStopwatch::operator double() | 
|---|
| 102 | { | 
|---|
| 103 | return getTime(); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | bool | 
|---|
| 107 | CStopwatch::isStopped() const | 
|---|
| 108 | { | 
|---|
| 109 | return m_stopped; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | double | 
|---|
| 113 | CStopwatch::getTime() const | 
|---|
| 114 | { | 
|---|
| 115 | if (m_stopped) { | 
|---|
| 116 | return m_mark; | 
|---|
| 117 | } | 
|---|
| 118 | else { | 
|---|
| 119 | return ARCH->time() - m_mark; | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | CStopwatch::operator double() const | 
|---|
| 124 | { | 
|---|
| 125 | return getTime(); | 
|---|
| 126 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.