first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / timely / stopwatch.cpp
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
33 stopwatch::stopwatch(const stopwatch &to_copy)
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
40 stopwatch::~stopwatch()
41 {
42   _status = UNSTARTED;
43   WHACK(_start_time);
44   WHACK(_stop_time);
45 }
46
47 stopwatch &stopwatch::operator =(const stopwatch &to_copy)
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
61 void stopwatch::start()
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
71 void stopwatch::halt()
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.
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