feisty meow concerns codebase  2.140
stopwatch.cpp
Go to the documentation of this file.
1 /***********************
2 * *
3 * Name : stopwatch
4 * Author : Chris Koeritz
5 * *
6 *******************************************************************************
7 * Copyright (c) 1991-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include "time_stamp.h"
16 #include "stopwatch.h"
17 
18 #include <basis/definitions.h>
19 #include <basis/functions.h>
20 #include <basis/guards.h>
21 
22 using namespace basis;
23 
24 namespace timely {
25 
26 stopwatch::stopwatch()
27 : _status(UNSTARTED),
28  _start_time(new time_stamp()),
29  _stop_time(new time_stamp()),
30  _total_so_far(0)
31 {}
32 
34 : _status(UNSTARTED),
35  _start_time(new time_stamp()),
36  _stop_time(new time_stamp()),
37  _total_so_far(0)
38 { *this = to_copy; }
39 
41 {
42  _status = UNSTARTED;
43  WHACK(_start_time);
44  WHACK(_stop_time);
45 }
46 
48 {
49  if (this == &to_copy) return *this;
50  *_start_time = *to_copy._start_time;
51  *_stop_time = *to_copy._stop_time;
52  _status = to_copy._status;
53  _total_so_far = to_copy._total_so_far;
54  return *this;
55 }
56 
57 void stopwatch::reset() { _status = UNSTARTED; _total_so_far = 0; }
58 
59 int stopwatch::milliseconds() { return common_measure(); }
60 
62 {
63  if (_status == RUNNING) return;
64  *_start_time = time_stamp();
65  _status = RUNNING;
66 }
67 
68 int stopwatch::compute_diff(const time_stamp &t1, const time_stamp &t2)
69 { return int(t2.value() - t1.value()); }
70 
72 {
73  if (_status == STOPPED) return;
74  else if (_status == UNSTARTED) return;
75 
76  *_stop_time = time_stamp();
77  _total_so_far += compute_diff(*_start_time, *_stop_time);
78 
79  _status = STOPPED;
80 }
81 
82 int stopwatch::common_measure()
83 {
84  bool restart = false;
85  int to_return = 0;
86  switch (_status) {
87  case UNSTARTED: break;
88  case RUNNING:
89  // stop stopwatch, restart afterwards.
90  halt();
91  restart = true;
92  // intentional fall through to default, so no break.
93  default:
94  // set the return value to the accumulated time.
95  to_return = _total_so_far;
96  break;
97  }
98  if (restart) start(); // crank the stopwatch back up if we were supposed to.
99  return to_return;
100 }
101 
102 } //namespace.
103 
A class for measuring event durations in real time.
Definition: stopwatch.h:29
stopwatch & operator=(const stopwatch &to_copy)
Definition: stopwatch.cpp:47
int milliseconds()
Returns the elapsed number of milliseconds on the stopwatch, overall.
Definition: stopwatch.cpp:59
void halt()
Stops the timing.
Definition: stopwatch.cpp:71
void reset()
Stops the stopwatch and clears it to zero time elapsed.
Definition: stopwatch.cpp:57
virtual ~stopwatch()
Definition: stopwatch.cpp:40
void start()
Begins the timing.
Definition: stopwatch.cpp:61
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38
time_representation value() const
returns the time_stamp in terms of the lower level type.
Definition: time_stamp.h:61
Constants and objects used throughout HOOPLE.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
#include <time.h>
Definition: earth_time.cpp:37