2011-04-08 07:10:00 +02:00
|
|
|
#ifndef DISPATCHER_H
|
|
|
|
#define DISPATCHER_H
|
|
|
|
|
2011-07-13 23:12:36 +02:00
|
|
|
#include <global.h>
|
|
|
|
#include <queue>
|
|
|
|
|
2011-07-17 02:13:53 +02:00
|
|
|
struct ScheduledTask {
|
2011-07-27 01:13:27 +02:00
|
|
|
ScheduledTask(const std::function<void()>& _callback) : ticks(0), callback(_callback) { }
|
|
|
|
ScheduledTask(int _ticks, const std::function<void()>& _callback) : ticks(_ticks), callback(_callback) { }
|
2011-07-17 02:13:53 +02:00
|
|
|
bool operator<(const ScheduledTask& other) const { return ticks > other.ticks; }
|
2011-04-08 07:10:00 +02:00
|
|
|
int ticks;
|
2011-07-27 01:13:27 +02:00
|
|
|
std::function<void()> callback;
|
2011-04-08 07:10:00 +02:00
|
|
|
};
|
|
|
|
|
2011-07-17 02:13:53 +02:00
|
|
|
struct lessScheduledTask : public std::binary_function<ScheduledTask*&, ScheduledTask*&, bool> {
|
2011-05-02 06:13:57 +02:00
|
|
|
bool operator()(ScheduledTask*& t1,ScheduledTask*& t2) { return (*t1) < (*t2); }
|
2011-04-08 07:10:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Dispatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Dispatcher() { }
|
|
|
|
|
2011-07-12 21:27:37 +02:00
|
|
|
/// Cleanup scheduled events
|
|
|
|
void cleanup();
|
|
|
|
|
2011-04-10 17:37:15 +02:00
|
|
|
/// Execute scheduled events
|
2011-04-15 04:13:53 +02:00
|
|
|
void poll();
|
2011-04-08 07:10:00 +02:00
|
|
|
|
2011-04-10 17:37:15 +02:00
|
|
|
/// Add an event
|
2011-07-27 01:13:27 +02:00
|
|
|
void addTask(const std::function<void()>& callback, bool pushFront = false);
|
2011-04-10 17:37:15 +02:00
|
|
|
|
|
|
|
/// Schedula an event
|
2011-07-27 01:13:27 +02:00
|
|
|
void scheduleTask(const std::function<void()>& callback, int delay);
|
2011-04-08 07:10:00 +02:00
|
|
|
|
|
|
|
private:
|
2011-07-27 01:13:27 +02:00
|
|
|
std::list<std::function<void()>> m_taskList;
|
2011-05-02 06:13:57 +02:00
|
|
|
std::priority_queue<ScheduledTask*, std::vector<ScheduledTask*>, lessScheduledTask> m_scheduledTaskList;
|
2011-04-08 07:10:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Dispatcher g_dispatcher;
|
|
|
|
|
|
|
|
#endif // DISPATCHER_H
|