]> Dogcows Code - chaz/openbox/blob - src/timer.hh
WE DONT USE BASE DISPLAY FOR ANYTHING ANY MORE!!@^!*@*!! YAY
[chaz/openbox] / src / timer.hh
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 #ifndef _BLACKBOX_Timer_hh
3 #define _BLACKBOX_Timer_hh
4
5 extern "C" {
6 #ifdef TIME_WITH_SYS_TIME
7 # include <sys/time.h>
8 # include <time.h>
9 #else // !TIME_WITH_SYS_TIME
10 # ifdef HAVE_SYS_TIME_H
11 # include <sys/time.h>
12 # else // !HAVE_SYS_TIME_H
13 # include <time.h>
14 # endif // HAVE_SYS_TIME_H
15 #endif // TIME_WITH_SYS_TIME
16 }
17
18 #include <queue>
19 #include <algorithm>
20 #include <vector>
21
22 namespace ob {
23
24 // forward declaration
25 class TimerQueueManager;
26
27 class TimeoutHandler {
28 public:
29 virtual void timeout(void) = 0;
30 };
31
32 class BTimer {
33 private:
34 TimerQueueManager *manager;
35 TimeoutHandler *handler;
36 bool timing, recur;
37
38 timeval _start, _timeout;
39
40 BTimer(const BTimer&);
41 BTimer& operator=(const BTimer&);
42
43 public:
44 BTimer(TimerQueueManager *m, TimeoutHandler *h);
45 virtual ~BTimer(void);
46
47 void fireTimeout(void);
48
49 inline bool isTiming(void) const { return timing; }
50 inline bool isRecurring(void) const { return recur; }
51
52 inline const timeval &getTimeout(void) const { return _timeout; }
53 inline const timeval &getStartTime(void) const { return _start; }
54
55 timeval timeRemaining(const timeval &tm) const;
56 bool shouldFire(const timeval &tm) const;
57 timeval endpoint(void) const;
58
59 inline void recurring(bool b) { recur = b; }
60
61 void setTimeout(long t);
62 void setTimeout(const timeval &t);
63
64 void start(void); // manager acquires timer
65 void stop(void); // manager releases timer
66 void halt(void); // halts the timer
67
68 bool operator<(const BTimer& other) const
69 { return shouldFire(other.endpoint()); }
70 };
71
72
73 template <class _Tp, class _Sequence, class _Compare>
74 class _timer_queue: protected std::priority_queue<_Tp, _Sequence, _Compare> {
75 public:
76 typedef std::priority_queue<_Tp, _Sequence, _Compare> _Base;
77
78 _timer_queue(void): _Base() {}
79 ~_timer_queue(void) {}
80
81 void release(const _Tp& value) {
82 c.erase(std::remove(c.begin(), c.end(), value), c.end());
83 // after removing the item we need to make the heap again
84 std::make_heap(c.begin(), c.end(), comp);
85 }
86 bool empty(void) const { return _Base::empty(); }
87 size_t size(void) const { return _Base::size(); }
88 void push(const _Tp& value) { _Base::push(value); }
89 void pop(void) { _Base::pop(); }
90 const _Tp& top(void) const { return _Base::top(); }
91 private:
92 // no copying!
93 _timer_queue(const _timer_queue&) {}
94 _timer_queue& operator=(const _timer_queue&) {}
95 };
96
97 struct TimerLessThan {
98 bool operator()(const BTimer* const l, const BTimer* const r) const {
99 return *r < *l;
100 }
101 };
102
103 typedef _timer_queue<BTimer*, std::vector<BTimer*>, TimerLessThan> TimerQueue;
104
105 class TimerQueueManager {
106 public:
107 virtual void addTimer(BTimer* timer) = 0;
108 virtual void removeTimer(BTimer* timer) = 0;
109 };
110
111 }
112
113 #endif // _BLACKBOX_Timer_hh
This page took 0.035923 seconds and 4 git commands to generate.