don't deref null
[blackbox.git] / lib / Timer.hh
blob852366ecf8f4c8a6138045f557deb4b2f34e7f04
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Timer.hh for Blackbox - An X11 Window Manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 #ifndef __Timer_hh
26 #define __Timer_hh
28 #include "Util.hh"
30 #include <algorithm>
31 #include <queue>
32 #include <vector>
34 // forward declare to avoid the header
35 struct timeval;
37 namespace bt {
39 // use a wrapper class to avoid the header as well
40 struct timeval {
41 long tv_sec;
42 long tv_usec;
44 inline timeval(void)
45 { tv_sec = tv_usec = 0l; }
47 // POSIX<->bt conversion
48 timeval(const ::timeval &);
49 timeval &operator=(const ::timeval &);
50 operator ::timeval() const;
53 timeval normalizeTimeval(const timeval &tm);
55 // forward declaration
56 class TimerQueueManager;
57 class Timer;
59 class TimeoutHandler {
60 public:
61 virtual void timeout(Timer *t) = 0;
64 class Timer: public NoCopy {
65 private:
66 TimerQueueManager *manager;
67 TimeoutHandler *handler;
68 bool timing, recur;
70 timeval _start, _timeout;
72 public:
73 Timer(TimerQueueManager *m, TimeoutHandler *h);
74 virtual ~Timer(void);
76 void fireTimeout(void);
78 inline bool isTiming(void) const
79 { return timing; }
80 inline bool isRecurring(void) const
81 { return recur; }
83 inline const timeval &timeout(void) const
84 { return _timeout; }
85 inline const timeval &startTime(void) const
86 { return _start; }
88 timeval timeRemaining(const timeval &tm) const;
89 bool shouldFire(const timeval &tm) const;
90 timeval endpoint(void) const;
92 inline void recurring(bool b)
93 { recur = b; }
95 void setTimeout(long t);
96 void setTimeout(const timeval &t);
98 void start(void); // manager acquires timer
99 void stop(void); // manager releases timer
100 void halt(void); // halts the timer
102 inline bool operator<(const Timer& other) const
103 { return shouldFire(other.endpoint()); }
106 template <class _Tp, class _Sequence, class _Compare>
107 class _timer_queue: protected std::priority_queue<_Tp, _Sequence, _Compare> {
108 public:
109 typedef std::priority_queue<_Tp, _Sequence, _Compare> _Base;
111 inline _timer_queue(void)
112 : _Base()
114 inline ~_timer_queue(void)
117 inline void release(const _Tp& value) {
118 _Base::c.erase(std::remove(_Base::c.begin(), _Base::c.end(), value),
119 _Base::c.end());
120 // after removing the item we need to make the heap again
121 std::make_heap(_Base::c.begin(), _Base::c.end(), _Base::comp);
123 inline bool empty(void) const
124 { return _Base::empty(); }
125 inline size_t size(void) const
126 { return _Base::size(); }
127 inline void push(const _Tp& value)
128 { _Base::push(value); }
129 inline void pop(void)
130 { _Base::pop(); }
131 inline const _Tp& top(void) const
132 { return _Base::top(); }
133 private:
134 // no copying!
135 _timer_queue(const _timer_queue&);
136 _timer_queue& operator=(const _timer_queue&);
139 struct TimerLessThan {
140 inline bool operator()(const Timer* const l, const Timer* const r) const
141 { return *r < *l; }
144 typedef _timer_queue<Timer*, std::vector<Timer*>, TimerLessThan> TimerQueue;
146 class TimerQueueManager {
147 public:
148 virtual void addTimer(Timer* timer) = 0;
149 virtual void removeTimer(Timer* timer) = 0;
152 } // namespace bt
154 #endif // __Timer_hh