Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/otk/timer.hh

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 #ifndef   __timer_hh
00003 #define   __timer_hh
00004 
00005 /*! @file timer.hh
00006   @brief Contains the Timer class, used for timed callbacks.
00007 */
00008 
00009 extern "C" {
00010 #ifdef    TIME_WITH_SYS_TIME
00011 #  include <sys/time.h>
00012 #  include <time.h>
00013 #else // !TIME_WITH_SYS_TIME
00014 #  ifdef    HAVE_SYS_TIME_H
00015 #    include <sys/time.h>
00016 #  else // !HAVE_SYS_TIME_H
00017 #    include <time.h>
00018 #  endif // HAVE_SYS_TIME_H
00019 #endif // TIME_WITH_SYS_TIME
00020 }
00021 
00022 #include <queue>
00023 #include <vector>
00024 
00025 namespace otk {
00026 
00027 //! The Timer class implements timed callbacks.
00028 /*!
00029   The Timer class can be used to have a callback fire after a given time
00030   interval. A created Timer will fire repetitively until it is destroyed.
00031 */
00032 class Timer {
00033 public:
00034   //! Data type of Timer callback
00035   typedef void (*TimeoutHandler)(void *data);
00036 
00037 private:
00038   //! Compares two timeval structs
00039   struct TimerCompare {
00040      //! Compares two timeval structs
00041      inline bool operator()(const Timer *a, const Timer *b) const {
00042        return timercmp(&a->_timeout, &b->_timeout, >);
00043      }
00044   };
00045   friend struct TimerCompare; // give access to _timeout for shitty compilers
00046 
00047   typedef
00048   std::priority_queue<Timer*, std::vector<Timer*>, TimerCompare> TimerQ;
00049 
00050   //! Milliseconds between timer firings
00051   long _delay;
00052   //! Callback for timer expiry
00053   TimeoutHandler _action;
00054   //! Data sent to callback
00055   void *_data;
00056   //! We overload the delete operator to just set this to true
00057   bool _del_me;
00058   //! The time the last fire should've been at
00059   struct timeval _last;
00060   //! When this timer will next trigger
00061   struct timeval _timeout;
00062 
00063   //! Queue of pending timers
00064   static TimerQ _q;
00065   //! Time next timer will expire
00066   static timeval _nearest_timeout;
00067   //! Time at start of current processing loop
00068   static timeval _now;
00069 
00070   //! Really delete something (not just flag for later)
00071   /*!
00072     @param self Timer to be deleted.
00073   */
00074   static void realDelete(Timer *self);
00075 
00076   //! Adds a millisecond delay to a timeval structure
00077   /*!
00078     @param a Amount of time to increment.
00079     @param msec Number of milliseconds to increment by.
00080   */
00081   static void timevalAdd(timeval &a, long msec);
00082 
00083 public:
00084   //! Constructs a new running timer and queues it
00085   /*!
00086     @param delay Time in milliseconds between firings
00087     @param cb The function to be called on fire.
00088     @param data Data to be passed to the callback on fire.
00089   */
00090   Timer(long delay, TimeoutHandler cb, void *data);
00091 
00092   //! Overloaded delete so we can leave deleted objects in queue for later reap
00093   /*!
00094     @param self Pointer to current instance of Timer.
00095   */
00096   void operator delete(void *self);
00097 
00098   //! Dispatches all elligible timers, then optionally waits for X events
00099   /*!
00100     @param wait Whether to wait for X events after processing timers.
00101   */
00102   static void dispatchTimers(bool wait = true);
00103 
00104   //! Returns a relative timeval (to pass select) of the next timer
00105   /*!
00106     @param tm Changed to hold the time until next timer.
00107     @return true if there are any timers queued, and the timeout is being
00108             returned in 'tm'. false if there are no timers queued.
00109   */
00110   static bool nearestTimeout(struct timeval &tm);
00111 
00112   //! Initializes internal data before use
00113   static void initialize(void);
00114 
00115   //! Deletes all waiting timers
00116   static void destroy(void);
00117 };
00118 
00119 }
00120 
00121 #endif // __timer.hh

Generated on Tue Feb 4 22:58:56 2003 for Openbox by doxygen1.3-rc2