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/watch.h>
25 #include <utils/time/clock.h>
26 #include <utils/time/time.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 
37 /** Constructor.
38  * @param clock clock instance to use for measurement.
39  */
41 {
42  this->clock = clock;
43 
44  is_running = false;
45  is_paused = false;
46 
47 }
48 
49 
50 /** Destructor. */
52 {
53 }
54 
55 
56 /** Starts the watch.
57  * This starts the watch. In case it is paused, currently, the watch
58  * is restarted
59  * @param t the time at which the watch is started is written to this time object
60  */
61 void
63 {
64  timeval now;
65  clock->get_time(&now);
66 
67  if (is_running && is_paused)
68  {
69  pause_stop.set_time(&now);
70  is_paused = false;
71 
72  pause_time += pause_stop - pause_start;
73  }
74  else if (!is_running)
75  {
76  pause_time.set_time( 0.0f );
77  is_running = true;
78 
79  watch_start.set_time(&now);
80  }
81  else /* is_running && !is_paused */
82  {
83  // todo
84  }
85 
86  if (0 != t) {
87  t->set_time(&now);
88  }
89 }
90 
91 
92 /** Stops the watch.
93  * This stops the watch also when it is paused, currently
94  * @param t the time at which the watch is started is written to this time object
95  */
96 void
98 {
99  timeval now;
100  clock->get_time(&now);
101  watch_stop.set_time(&now);
102  is_running = false;
103 
104  if (is_paused)
105  {
106  pause_stop.set_time(&now);
107  pause_time += pause_stop - pause_start;
108 
109  is_paused = false;
110  }
111 
112  if (0 != t) {
113  t->set_time(&now);
114  }
115 }
116 
117 
118 /** Pauses the watch.
119  * Puts the watch into pause mode
120  * @param t the time at which the watch is started is written to this time object
121  */
122 void
124 {
125  timeval now;
126  clock->get_time(&now);
127 
128  if (!is_paused) {
129  pause_start.set_time(&now);
130  is_paused = true;
131  }
132 
133  if (0 != t) {
134  t->set_time(&now);;
135  }
136 }
137 
138 
139 /** Reset time. */
140 void
142 {
143  timeval now;
144  clock->get_time(&now);
145  watch_start.set_time(&now);
146 }
147 
148 
149 /** Returns the current watch time.
150  * @return the current watch time
151  */
152 Time
154 {
155  timeval now;
156  clock->get_time(&now);
157 
158  Time ret(&now);
159 
160  if (is_running && !is_paused)
161  {
162  ret -= watch_start + pause_time;
163  }
164  else if (is_running && is_paused)
165  {
166  Time cur_pause;
167  cur_pause = ret - pause_start;
168  ret -= watch_start + pause_time + cur_pause;
169  }
170  else
171  {
172  ret = watch_stop - watch_start - pause_time;
173  }
174 
175  return ret;
176 }
177 
178 
179 /** Returns the current clock time.
180  * @return the current clock time
181  */
182 Time
184 {
185  timeval now;
186  clock->get_time(&now);
187  Time t(&now);
188  return t;
189 }
190 
191 } // end namespace fawkes
void reset()
Reset time.
Definition: watch.cpp:141
Time clock_time()
Returns the current clock time.
Definition: watch.cpp:183
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:176
Fawkes library namespace.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
A class for handling time.
Definition: time.h:91
void stop(Time *t=0)
Stops the watch.
Definition: watch.cpp:97
virtual ~Watch()
Destructor.
Definition: watch.cpp:51
void start(Time *t=0)
Starts the watch.
Definition: watch.cpp:62
Watch(Clock *clock)
Constructor.
Definition: watch.cpp:40
void pause(Time *t=0)
Pauses the watch.
Definition: watch.cpp:123
Time watch_time()
Returns the current watch time.
Definition: watch.cpp:153
void set_time(const timeval *tv)
Sets the time.
Definition: time.cpp:262