]>
Dogcows Code - chaz/openbox/blob - timer.hh
509a4a070d5a16170b1a533db4ec2fe4f91eaed3
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
6 #ifdef TIME_WITH_SYS_TIME
9 #else // !TIME_WITH_SYS_TIME
10 # ifdef HAVE_SYS_TIME_H
11 # include <sys/time.h>
12 # else // !HAVE_SYS_TIME_H
14 # endif // HAVE_SYS_TIME_H
15 #endif // TIME_WITH_SYS_TIME
20 class OBTimerQueueManager
;
22 //! The data passed to the OBTimeoutHandler function.
24 Note: this is a very useful place to put an object instance, and set the
25 event handler to a static function in the same class.
27 typedef void *OBTimeoutData
;
28 //! The type of function which can be set as the callback for an OBTimer firing
29 typedef void (*OBTimeoutHandler
)(OBTimeoutData
);
31 //! A Timer class which will fire a function when its time elapses
34 //! The manager which to add ourself to and remove ourself after we are done
35 OBTimerQueueManager
*manager
;
36 //! The function to call when the time elapses
37 OBTimeoutHandler handler
;
38 //! The data which gets passed along to the OBTimeoutHandler
40 //! Determines if the timer is currently started
42 //! When this is true, the timer will reset itself to fire again every time
45 //! The time at which the timer started
47 //! The time at which the timer is going to fire
50 //! Disallows copying of OBTimer objects
51 OBTimer(const OBTimer
&);
52 //! Disallows copying of OBTimer objects
53 OBTimer
& operator=(const OBTimer
&);
56 //! Constructs a new OBTimer object
58 @param m The OBTimerQueueManager with which to associate. The manager
59 specified will be resposible for making this timer fire.
60 @param h The function to call when the timer fires
61 @param d The data to pass along to the function call when the timer fires
63 OBTimer(OBTimerQueueManager
*m
, OBTimeoutHandler h
, OBTimeoutData d
);
64 //! Destroys the OBTimer object
67 //! Fires the timer, calling its OBTimeoutHandler
70 //! Returns if the OBTimer is started and timing
71 inline bool isTiming() const { return timing
; }
72 //! Returns if the OBTimer is going to repeat
73 inline bool isRecurring() const { return recur
; }
75 //! Gets the amount of time the OBTimer should last before firing
76 inline const timeval
&getTimeout() const { return _timeout
; }
77 //! Gets the time at which the OBTimer started
78 inline const timeval
&getStartTime() const { return _start
; }
80 //! Gets the amount of time left before the OBTimer fires
81 timeval
timeRemaining(const timeval
&tm
) const;
82 //! Returns if the OBTimer is past its timeout time, and should fire
83 bool shouldFire(const timeval
&tm
) const;
85 //! Gets the time at which the OBTimer will fire
86 timeval
endpoint() const;
88 //! Sets the OBTimer to repeat or not
90 @param b If true, the timer is set to repeat; otherwise, it will fire only
93 inline void recurring(bool b
) { recur
= b
; }
95 //! Sets the amount of time for the OBTimer to last before firing in
98 @param t The number of milliseconds the timer should last
100 void setTimeout(long t
);
101 //! Sets the amount of time the OBTimer should last before firing
103 @param t The amount of time the timer should last
105 void setTimeout(const timeval
&t
);
107 //! Causes the timer to begin
109 The timer fires after the time in OBTimer::getTimeout has passed since this
111 Calling this function while the timer is already started will cause it to
112 restart its countdown.
114 void start(); // manager acquires timer
115 //! Causes the timer to stop
117 The timer will no longer fire once this function has been called.
118 Calling this function more than once does not have any effect.
120 void stop(); // manager releases timer
122 //! Determines if this OBTimer will fire before a second OBTimer object
124 @param other The second OBTimer with which to compare
125 @return true if this OBTimer will fire before 'other'; otherwise, false
127 bool operator<(const OBTimer
& other
) const
128 { return shouldFire(other
.endpoint()); }
This page took 0.043743 seconds and 3 git commands to generate.