Timer.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                             Timer.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (c) 1999,2005 by Vladislav Grinchenko
00006 //
00007 //  This library is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU Library General Public
00009 //  License as published by the Free Software Foundation; either
00010 //  version 2 of the License, or (at your option) any later version.
00011 //------------------------------------------------------------------------------
00012 //  Create: 09/27/1999
00013 //------------------------------------------------------------------------------
00014 #ifndef TIMER_H
00015 #define TIMER_H
00016 
00017 #include <sys/select.h>
00018 #include <sys/time.h>
00019 
00020 #include "EventHandler.h"
00021 #include "assa/TimeVal.h"
00022 
00023 namespace ASSA {
00024 
00025 class TimerQueue;       // forward declaration
00026 
00032 class Timer 
00033 {
00034 public:
00036     Timer ();
00037 
00044     Timer (const EventHandler* eh_, 
00045            const TimeVal&      tm_,
00046            const TimeVal&      delta_,
00047            const std::string&  name_);
00048 
00050     Timer (const Timer& t_);
00051 
00053     ~Timer ();
00054 
00056     Timer& operator= (const Timer& t_);
00057 
00059     bool operator< (const Timer& t_) const;
00060 
00062     bool operator== (const Timer& t_) const;
00063 
00065     EventHandler* getHandler () const { return m_eh; }
00066 
00068     const TimeVal& getExpirationTime () const { return m_timer; }
00069 
00071     const TimeVal& getDeltaTime () const { return m_delta; }
00072 
00074     void rescheduleExpirationTime ();
00075 
00077     void dump (void);
00078 
00082     void set_id (const std::string& id_) { m_id = id_; }
00083 
00086     std::string get_id () const { return m_id; }
00087 
00088 private:
00090     EventHandler* m_eh;
00091 
00093     TimeVal m_timer;
00094 
00096     TimeVal m_delta;
00097 
00099     std::string m_id;
00100 };
00101 //------------------------------------------------------------------------------
00102 // Timer class inlines
00103 //------------------------------------------------------------------------------
00104 
00105 inline  
00106 Timer::
00107 Timer () 
00108     : m_eh (NULL), m_id ("<unknown>")
00109 {
00110     trace("Timer::Timer");
00111 }
00112 
00113 inline
00114 Timer::
00115 Timer (const EventHandler* eh_, const TimeVal& tm_,
00116        const TimeVal& delta_, const std::string& name_)
00117     : m_eh ((EventHandler*) eh_), m_timer (tm_), m_delta (delta_), m_id (name_)
00118 {
00119     trace("Timer::Timer(EH*, TV&)");
00120 }
00121 
00122 inline
00123 Timer::
00124 Timer (const Timer& t_)
00125     : m_eh (t_.m_eh), m_timer (t_.m_timer), 
00126       m_delta (t_.m_delta), m_id (t_.m_id)
00127 {
00128     trace("Timer::Timer(Timer&)");
00129 }
00130 
00131 inline
00132 Timer::
00133 ~Timer ()
00134 {
00135     trace("Timer::~Timer");
00136 }
00137 
00138 inline Timer& 
00139 Timer::
00140 operator=(const Timer& t_)
00141 {
00142     m_eh    = t_.m_eh;
00143     m_timer = t_.m_timer;
00144     m_delta = t_.m_delta;
00145     m_id    = t_.m_id;
00146 
00147     return *this;
00148 }
00149 
00150 inline bool 
00151 Timer::
00152 operator<(const Timer& t_) const
00153 {
00154     return m_timer < t_.m_timer;
00155 }
00156 
00157 inline bool 
00158 Timer::
00159 operator==(const Timer& t_) const
00160 {
00161     return m_timer == t_.m_timer;
00162 }
00163 
00164 inline void 
00165 Timer::
00166 rescheduleExpirationTime ()
00167 {
00168     TimeVal now (TimeVal::gettimeofday ());
00169     m_timer = now + m_delta;
00170 }
00171 
00172 inline void
00173 Timer::
00174 dump (void)
00175 {
00176     DL((REACT,"Timer %s (EH=%s) expires at %s (delta=%s)\n",
00177         get_id ().c_str (),
00178         m_eh->get_id ().c_str (), 
00179         m_timer.fmtString ().c_str(),
00180         m_delta.fmt_mm_ss_mls ().c_str()));
00181 }
00182 
00186 struct TimerCompare 
00187 {
00188     bool operator() (const Timer* t1_, const Timer* t2_) const 
00189         {
00190             return (*t1_ < *t2_);
00191         }
00192 };
00193 
00194 } // end namespace ASSA
00195 
00196 #endif /* TIMER_H */  

Generated on Mon Dec 19 16:20:18 2005 for libassa by  doxygen 1.4.5