Fawkes API  Fawkes Development Version
watch.cpp
1 
2 /***************************************************************************
3  * watch.cpp - A stopwatch
4  *
5  * Generated: Sun June 03 15:38:24 2007
6  * Copyright 2007 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/time/clock.h>
25 #include <utils/time/time.h>
26 #include <utils/time/watch.h>
27 
28 namespace fawkes {
29 
30 /** @class Watch <utils/time/watch.h>
31  * This is a stop-watch. Also, one can request the current time from the
32  * clock. Every watch counts time w.r.t. a certain time source.
33  * @author Daniel Beck
34  */
35 
36 /** Constructor.
37  * @param clock clock instance to use for measurement.
38  */
39 Watch::Watch(Clock *clock)
40 {
41  this->clock = clock;
42 
43  is_running = false;
44  is_paused = false;
45 }
46 
47 /** Destructor. */
49 {
50 }
51 
52 /** Starts the watch.
53  * This starts the watch. In case it is paused, currently, the watch
54  * is restarted
55  * @param t the time at which the watch is started is written to this time object
56  */
57 void
59 {
60  timeval now;
61  clock->get_time(&now);
62 
63  if (is_running && is_paused) {
64  pause_stop.set_time(&now);
65  is_paused = false;
66 
67  pause_time += pause_stop - pause_start;
68  } else if (!is_running) {
69  pause_time.set_time(0.0f);
70  is_running = true;
71 
72  watch_start.set_time(&now);
73  } else /* is_running && !is_paused */
74  {
75  // todo
76  }
77 
78  if (0 != t) {
79  t->set_time(&now);
80  }
81 }
82 
83 /** Stops the watch.
84  * This stops the watch also when it is paused, currently
85  * @param t the time at which the watch is started is written to this time object
86  */
87 void
88 Watch::stop(Time *t)
89 {
90  timeval now;
91  clock->get_time(&now);
92  watch_stop.set_time(&now);
93  is_running = false;
94 
95  if (is_paused) {
96  pause_stop.set_time(&now);
97  pause_time += pause_stop - pause_start;
98 
99  is_paused = false;
100  }
101 
102  if (0 != t) {
103  t->set_time(&now);
104  }
105 }
106 
107 /** Pauses the watch.
108  * Puts the watch into pause mode
109  * @param t the time at which the watch is started is written to this time object
110  */
111 void
112 Watch::pause(Time *t)
113 {
114  timeval now;
115  clock->get_time(&now);
116 
117  if (!is_paused) {
118  pause_start.set_time(&now);
119  is_paused = true;
120  }
121 
122  if (0 != t) {
123  t->set_time(&now);
124  ;
125  }
126 }
127 
128 /** Reset time. */
129 void
130 Watch::reset()
131 {
132  timeval now;
133  clock->get_time(&now);
134  watch_start.set_time(&now);
135 }
136 
137 /** Returns the current watch time.
138  * @return the current watch time
139  */
140 Time
142 {
143  timeval now;
144  clock->get_time(&now);
145 
146  Time ret(&now);
147 
148  if (is_running && !is_paused) {
149  ret -= watch_start + pause_time;
150  } else if (is_running && is_paused) {
151  Time cur_pause;
152  cur_pause = ret - pause_start;
153  ret -= watch_start + pause_time + cur_pause;
154  } else {
155  ret = watch_stop - watch_start - pause_time;
156  }
157 
158  return ret;
159 }
160 
161 /** Returns the current clock time.
162  * @return the current clock time
163  */
164 Time
166 {
167  timeval now;
168  clock->get_time(&now);
169  Time t(&now);
170  return t;
171 }
172 
173 } // end namespace fawkes
fawkes::Time::set_time
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:251
fawkes::Watch::Watch
Watch(Clock *clock)
Constructor.
Definition: watch.cpp:43
fawkes::Watch::pause
void pause(Time *t=0)
Pauses the watch.
Definition: watch.cpp:116
fawkes::Clock::get_time
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:166
fawkes::Watch::clock_time
Time clock_time()
Returns the current clock time.
Definition: watch.cpp:169
fawkes
fawkes::Watch::stop
void stop(Time *t=0)
Stops the watch.
Definition: watch.cpp:92
fawkes::Watch::watch_time
Time watch_time()
Returns the current watch time.
Definition: watch.cpp:145
fawkes::Watch::~Watch
virtual ~Watch()
Destructor.
Definition: watch.cpp:52
fawkes::Watch::reset
void reset()
Reset time.
Definition: watch.cpp:134
fawkes::Time
Definition: time.h:96
fawkes::Watch::start
void start(Time *t=0)
Starts the watch.
Definition: watch.cpp:62