00001 #ifndef TIMER_IMPLEMENTATION_FILE
00002 #define TIMER_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mechanisms_implementation_only.h"
00019 #include "time_stamp.h"
00020 #include "timer.h"
00021
00022 #include <basis/definitions.h>
00023 #include <basis/function.h>
00024 #include <basis/guards.h>
00025
00026 timer::timer()
00027 : _status(UNSTARTED),
00028 _start_time(new time_stamp()),
00029 _stop_time(new time_stamp()),
00030 _total_so_far(0)
00031 {}
00032
00033 timer::timer(const timer &to_copy)
00034 : _status(UNSTARTED),
00035 _start_time(new time_stamp()),
00036 _stop_time(new time_stamp()),
00037 _total_so_far(0)
00038 { *this = to_copy; }
00039
00040 timer::~timer()
00041 {
00042 _status = UNSTARTED;
00043 WHACK(_start_time);
00044 WHACK(_stop_time);
00045 }
00046
00047 timer &timer::operator =(const timer &to_copy)
00048 {
00049 if (this == &to_copy) return *this;
00050 *_start_time = *to_copy._start_time;
00051 *_stop_time = *to_copy._stop_time;
00052 _status = to_copy._status;
00053 _total_so_far = to_copy._total_so_far;
00054 return *this;
00055 }
00056
00057 void timer::reset() { _status = UNSTARTED; _total_so_far = 0; }
00058
00059 int timer::milliseconds() { return common_measure(); }
00060
00061 void timer::start()
00062 {
00063 if (_status == RUNNING) return;
00064 *_start_time = time_stamp();
00065 _status = RUNNING;
00066 }
00067
00068 int timer::compute_diff(const time_stamp &t1, const time_stamp &t2)
00069 { return int(t2.value() - t1.value()); }
00070
00071 void timer::halt()
00072 {
00073 if (_status == STOPPED) return;
00074 else if (_status == UNSTARTED) return;
00075
00076 *_stop_time = time_stamp();
00077 _total_so_far += compute_diff(*_start_time, *_stop_time);
00078
00079 _status = STOPPED;
00080 }
00081
00082 int timer::common_measure()
00083 {
00084 bool restart = false;
00085 int to_return = 0;
00086 switch (_status) {
00087 case UNSTARTED: break;
00088 case RUNNING:
00089
00090 halt();
00091 restart = true;
00092
00093 default:
00094
00095 to_return = _total_so_far;
00096 break;
00097 }
00098 if (restart) start();
00099 return to_return;
00100 }
00101
00102
00103 #endif //TIMER_IMPLEMENTATION_FILE
00104