1 /***********************
4 * Author : Chris Koeritz
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 \*****************************************************************************/
15 #include "time_stamp.h"
16 #include "stopwatch.h"
18 #include <basis/definitions.h>
19 #include <basis/functions.h>
20 #include <basis/guards.h>
22 using namespace basis;
26 stopwatch::stopwatch()
28 _start_time(new time_stamp()),
29 _stop_time(new time_stamp()),
33 stopwatch::stopwatch(const stopwatch &to_copy)
35 _start_time(new time_stamp()),
36 _stop_time(new time_stamp()),
40 stopwatch::~stopwatch()
47 stopwatch &stopwatch::operator =(const stopwatch &to_copy)
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;
57 void stopwatch::reset() { _status = UNSTARTED; _total_so_far = 0; }
59 int stopwatch::milliseconds() { return common_measure(); }
61 void stopwatch::start()
63 if (_status == RUNNING) return;
64 *_start_time = time_stamp();
68 int stopwatch::compute_diff(const time_stamp &t1, const time_stamp &t2)
69 { return int(t2.value() - t1.value()); }
71 void stopwatch::halt()
73 if (_status == STOPPED) return;
74 else if (_status == UNSTARTED) return;
76 *_stop_time = time_stamp();
77 _total_so_far += compute_diff(*_start_time, *_stop_time);
82 int stopwatch::common_measure()
87 case UNSTARTED: break;
89 // stop stopwatch, restart afterwards.
92 // intentional fall through to default, so no break.
94 // set the return value to the accumulated time.
95 to_return = _total_so_far;
98 if (restart) start(); // crank the stopwatch back up if we were supposed to.