summaryrefslogtreecommitdiff
path: root/otk/timer.hh
diff options
context:
space:
mode:
authorDana Jansens <danakj@orodu.net>2002-11-03 14:29:34 +0000
committerDana Jansens <danakj@orodu.net>2002-11-03 14:29:34 +0000
commit9259ec5732851dd66f7c598d629e3808ac7ab3d8 (patch)
tree5452b84b8937cde5f6977f26c24361cc1c0a5f08 /otk/timer.hh
parentad80ef0f667e3b72d9e35d7a93451a1e2dfa0ab6 (diff)
new timer infrastructure. takes a function pointer for the timeout, with a void* parameter (useful for holding a class instance!)
Diffstat (limited to 'otk/timer.hh')
-rw-r--r--otk/timer.hh124
1 files changed, 124 insertions, 0 deletions
diff --git a/otk/timer.hh b/otk/timer.hh
new file mode 100644
index 00000000..2deeba58
--- /dev/null
+++ b/otk/timer.hh
@@ -0,0 +1,124 @@
+// -*- mode: C++; indent-tabs-mode: nil; -*-
+#ifndef _BLACKBOX_Timer_hh
+#define _BLACKBOX_Timer_hh
+
+extern "C" {
+#ifdef TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else // !TIME_WITH_SYS_TIME
+# ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+# else // !HAVE_SYS_TIME_H
+# include <time.h>
+# endif // HAVE_SYS_TIME_H
+#endif // TIME_WITH_SYS_TIME
+}
+
+#include <queue>
+#include <algorithm>
+#include <vector>
+
+namespace otk {
+
+// forward declaration
+class OBTimerQueueManager;
+
+typedef void *OBTimeoutData;
+typedef void (*OBTimeoutHandler)(OBTimeoutData);
+
+class OBTimer {
+private:
+ OBTimerQueueManager *manager;
+ OBTimeoutHandler handler;
+ OBTimeoutData data;
+ bool timing, recur;
+
+ timeval _start, _timeout;
+
+ OBTimer(const OBTimer&);
+ OBTimer& operator=(const OBTimer&);
+
+public:
+ OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d);
+ virtual ~OBTimer();
+
+ void fireTimeout();
+
+ inline bool isTiming() const { return timing; }
+ inline bool isRecurring() const { return recur; }
+
+ inline const timeval &getTimeout() const { return _timeout; }
+ inline const timeval &getStartTime() const { return _start; }
+
+ timeval timeRemaining(const timeval &tm) const;
+ bool shouldFire(const timeval &tm) const;
+ timeval endpoint() const;
+
+ inline void recurring(bool b) { recur = b; }
+
+ void setTimeout(long t);
+ void setTimeout(const timeval &t);
+
+ void start(); // manager acquires timer
+ void stop(); // manager releases timer
+ void halt(); // halts the timer
+
+ bool operator<(const OBTimer& other) const
+ { return shouldFire(other.endpoint()); }
+};
+
+
+template <class _Tp, class _Sequence, class _Compare>
+class _timer_queue: protected std::priority_queue<_Tp, _Sequence, _Compare> {
+public:
+ typedef std::priority_queue<_Tp, _Sequence, _Compare> _Base;
+
+ _timer_queue(): _Base() {}
+ ~_timer_queue() {}
+
+ void release(const _Tp& value) {
+ c.erase(std::remove(c.begin(), c.end(), value), c.end());
+ // after removing the item we need to make the heap again
+ std::make_heap(c.begin(), c.end(), comp);
+ }
+ bool empty() const { return _Base::empty(); }
+ size_t size() const { return _Base::size(); }
+ void push(const _Tp& value) { _Base::push(value); }
+ void pop() { _Base::pop(); }
+ const _Tp& top() const { return _Base::top(); }
+private:
+ // no copying!
+ _timer_queue(const _timer_queue&) {}
+ _timer_queue& operator=(const _timer_queue&) {}
+};
+
+struct TimerLessThan {
+ bool operator()(const OBTimer* const l, const OBTimer* const r) const {
+ return *r < *l;
+ }
+};
+
+typedef _timer_queue<OBTimer*,
+ std::vector<OBTimer*>, TimerLessThan> TimerQueue;
+
+class OBTimerQueueManager {
+private:
+ TimerQueue timerList;
+public:
+ OBTimerQueueManager() {}
+ virtual ~OBTimerQueueManager() {}
+
+ //! Will wait for and fire the next timer in the queue.
+ /*!
+ The function will stop waiting if an event is received from the X server.
+ */
+ virtual void fire();
+
+ virtual void addTimer(OBTimer* timer);
+ virtual void removeTimer(OBTimer* timer);
+};
+
+}
+
+#endif // _BLACKBOX_Timer_hh