source: vendor/synergy/current/lib/base/CStopwatch.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 1.9 KB
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
22CStopwatch::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
32CStopwatch::~CStopwatch()
33{
34 // do nothing
35}
36
37double
38CStopwatch::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
53void
54CStopwatch::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
65void
66CStopwatch::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
78void
79CStopwatch::setTrigger()
80{
81 stop();
82 m_triggered = true;
83}
84
85double
86CStopwatch::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
101CStopwatch::operator double()
102{
103 return getTime();
104}
105
106bool
107CStopwatch::isStopped() const
108{
109 return m_stopped;
110}
111
112double
113CStopwatch::getTime() const
114{
115 if (m_stopped) {
116 return m_mark;
117 }
118 else {
119 return ARCH->time() - m_mark;
120 }
121}
122
123CStopwatch::operator double() const
124{
125 return getTime();
126}
Note: See TracBrowser for help on using the repository browser.