tibia-client/src/framework/core/dispatcher.h

47 lines
1.3 KiB
C
Raw Normal View History

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>
#include <boost/bind.hpp>
#include <boost/function.hpp>
2011-04-08 07:10:00 +02:00
2011-07-17 02:13:53 +02:00
struct ScheduledTask {
ScheduledTask(const boost::function<void()>& _callback) : ticks(0), callback(_callback) { }
ScheduledTask(int _ticks, const boost::function<void()>& _callback) : ticks(_ticks), callback(_callback) { }
bool operator<(const ScheduledTask& other) const { return ticks > other.ticks; }
2011-04-08 07:10:00 +02:00
int ticks;
2011-07-13 23:12:36 +02:00
boost::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> {
bool operator()(ScheduledTask*& t1,ScheduledTask*& t2) { return (*t1) < (*t2); }
2011-04-08 07:10:00 +02:00
};
class Dispatcher
{
public:
Dispatcher() { }
/// 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-13 23:12:36 +02:00
void addTask(const boost::function<void()>& callback, bool pushFront = false);
2011-04-10 17:37:15 +02:00
/// Schedula an event
2011-07-13 23:12:36 +02:00
void scheduleTask(const boost::function<void()>& callback, int delay);
2011-04-08 07:10:00 +02:00
private:
2011-07-13 23:12:36 +02:00
std::list<boost::function<void()>> m_taskList;
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