]>
Dogcows Code - chaz/openbox/blob - src/timer.hh
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 #ifndef _BLACKBOX_Timer_hh
3 #define _BLACKBOX_Timer_hh
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
24 // forward declaration
25 class OBTimerQueueManager
;
27 class TimeoutHandler
{
29 virtual void timeout(void) = 0;
34 TimeoutHandler
*handler
;
37 timeval _start
, _timeout
;
39 OBTimer(const OBTimer
&);
40 OBTimer
& operator=(const OBTimer
&);
43 OBTimer(TimeoutHandler
*h
);
44 virtual ~OBTimer(void);
46 void fireTimeout(void);
48 inline bool isTiming(void) const { return timing
; }
49 inline bool isRecurring(void) const { return recur
; }
51 inline const timeval
&getTimeout(void) const { return _timeout
; }
52 inline const timeval
&getStartTime(void) const { return _start
; }
54 timeval
timeRemaining(const timeval
&tm
) const;
55 bool shouldFire(const timeval
&tm
) const;
56 timeval
endpoint(void) const;
58 inline void recurring(bool b
) { recur
= b
; }
60 void setTimeout(long t
);
61 void setTimeout(const timeval
&t
);
63 void start(void); // manager acquires timer
64 void stop(void); // manager releases timer
65 void halt(void); // halts the timer
67 bool operator<(const OBTimer
& other
) const
68 { return shouldFire(other
.endpoint()); }
72 template <class _Tp
, class _Sequence
, class _Compare
>
73 class _timer_queue
: protected std::priority_queue
<_Tp
, _Sequence
, _Compare
> {
75 typedef std::priority_queue
<_Tp
, _Sequence
, _Compare
> _Base
;
77 _timer_queue(void): _Base() {}
78 ~_timer_queue(void) {}
80 void release(const _Tp
& value
) {
81 c
.erase(std::remove(c
.begin(), c
.end(), value
), c
.end());
82 // after removing the item we need to make the heap again
83 std::make_heap(c
.begin(), c
.end(), comp
);
85 bool empty(void) const { return _Base::empty(); }
86 size_t size(void) const { return _Base::size(); }
87 void push(const _Tp
& value
) { _Base::push(value
); }
88 void pop(void) { _Base::pop(); }
89 const _Tp
& top(void) const { return _Base::top(); }
92 _timer_queue(const _timer_queue
&) {}
93 _timer_queue
& operator=(const _timer_queue
&) {}
96 struct TimerLessThan
{
97 bool operator()(const OBTimer
* const l
, const OBTimer
* const r
) const {
102 typedef _timer_queue
<OBTimer
*,
103 std::vector
<OBTimer
*>, TimerLessThan
> TimerQueue
;
105 class OBTimerQueueManager
{
107 TimerQueue timerList
;
109 OBTimerQueueManager() {}
110 virtual ~OBTimerQueueManager() {}
112 //! Will wait for and fire the next timer in the queue.
114 The function will stop waiting if an event is received from the X server.
118 virtual void addTimer(OBTimer
* timer
);
119 virtual void removeTimer(OBTimer
* timer
);
124 #endif // _BLACKBOX_Timer_hh
This page took 0.042813 seconds and 5 git commands to generate.