timer.cpp

Go to the documentation of this file.
00001 #ifndef TIMER_IMPLEMENTATION_FILE
00002 #define TIMER_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : timer                                                             *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
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       // stop timer, restart afterwards.
00090       halt();
00091       restart = true;
00092       // intentional fall through to default.
00093     default:
00094       // set the return value to the accumulated time.
00095       to_return = _total_so_far;
00096       break;
00097   }
00098   if (restart) start();  // crank the timer back up if we were supposed to.
00099   return to_return;
00100 }
00101 
00102 
00103 #endif //TIMER_IMPLEMENTATION_FILE
00104 

Generated on Fri Nov 28 04:29:16 2008 for HOOPLE Libraries by  doxygen 1.5.1