watch.cpp

00001 
00002 /***************************************************************************
00003  *  watch.cpp - A stopwatch
00004  *
00005  *  Generated: Sun June 03 15:38:24 2007
00006  *  Copyright  2007  Daniel Beck 
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <utils/time/watch.h>
00025 #include <utils/time/clock.h>
00026 #include <utils/time/time.h>
00027 
00028 namespace fawkes {
00029 
00030 /** @class Watch <utils/time/watch.h>
00031  * This is a stop-watch. Also, one can request the current time from the 
00032  * clock. Every watch counts time w.r.t. a certain time source.
00033  * @author Daniel Beck
00034  */
00035 
00036 
00037 /** Constructor.
00038  * @param clock clock instance to use for measurement.
00039  */
00040 Watch::Watch(Clock *clock)
00041 {
00042   this->clock = clock;
00043 
00044   is_running = false;
00045   is_paused = false;
00046 
00047 }
00048 
00049 
00050 /** Destructor. */
00051 Watch::~Watch()
00052 {
00053 }
00054 
00055 
00056 /** Starts the watch.
00057  * This starts the watch. In case it is paused, currently, the watch
00058  * is restarted
00059  * @param t the time at which the watch is started is written to this time object
00060  */
00061 void
00062 Watch::start(Time* t)
00063 {
00064   timeval now;
00065   clock->get_time(&now);
00066 
00067   if (is_running && is_paused)
00068     {
00069       pause_stop.set_time(&now);
00070       is_paused = false;
00071   
00072       pause_time += pause_stop - pause_start;
00073     }
00074   else if (!is_running)
00075     {
00076       pause_time.set_time( 0.0f );
00077       is_running = true;
00078 
00079       watch_start.set_time(&now);
00080     }
00081   else /* is_running && !is_paused */
00082     {
00083       // todo
00084     }
00085 
00086   if (0 != t) {
00087     t->set_time(&now);
00088   }
00089 }
00090 
00091 
00092 /** Stops the watch.
00093  * This stops the watch also when it is paused, currently
00094  * @param t the time at which the watch is started is written to this time object
00095  */
00096 void
00097 Watch::stop(Time* t)
00098 {
00099   timeval now;
00100   clock->get_time(&now);
00101   watch_stop.set_time(&now);
00102   is_running = false;
00103 
00104   if (is_paused)
00105     {
00106       pause_stop.set_time(&now);
00107       pause_time += pause_stop - pause_start;
00108 
00109       is_paused = false;
00110     }
00111 
00112   if (0 != t) {
00113     t->set_time(&now);
00114   }
00115 }
00116 
00117 
00118 /** Pauses the watch.
00119  * Puts the watch into pause mode
00120  * @param t the time at which the watch is started is written to this time object
00121  */
00122 void
00123 Watch::pause(Time* t)
00124 {
00125   timeval now;
00126   clock->get_time(&now);
00127 
00128   if (!is_paused) {
00129     pause_start.set_time(&now);
00130     is_paused = true;
00131   }
00132 
00133   if (0 != t) {
00134     t->set_time(&now);;
00135   }
00136 }
00137 
00138 
00139 /** Reset time. */
00140 void
00141 Watch::reset()
00142 {
00143   timeval now;
00144   clock->get_time(&now);  
00145   watch_start.set_time(&now);
00146 }
00147 
00148 
00149 /** Returns the current watch time.
00150  * @return the current watch time
00151  */
00152 Time
00153 Watch::watch_time()
00154 {
00155   timeval now;
00156   clock->get_time(&now);
00157 
00158   Time ret(&now);
00159 
00160   if (is_running && !is_paused) 
00161     {
00162       ret -= watch_start + pause_time;
00163     }
00164   else if (is_running && is_paused) 
00165     {
00166       Time cur_pause;
00167       cur_pause = ret - pause_start;
00168       ret -= watch_start + pause_time + cur_pause;
00169     }
00170   else
00171     {
00172       ret = watch_stop - watch_start - pause_time;
00173     }
00174 
00175   return ret;
00176 }
00177 
00178 
00179 /** Returns the current clock time.
00180  * @return the current clock time 
00181  */
00182 Time
00183 Watch::clock_time()
00184 {
00185   timeval now;
00186   clock->get_time(&now);
00187   Time t(&now);
00188   return t;
00189 }
00190 
00191 } // end namespace fawkes