mirror of
https://github.com/edubart/otclient.git
synced 2025-10-17 13:03:27 +02:00
Implement support for coroutines in C++
This commit is contained in:
@@ -75,6 +75,16 @@ void EventDispatcher::poll()
|
||||
}
|
||||
m_pollEventsSize = m_eventList.size();
|
||||
}
|
||||
|
||||
for(auto it = m_coroutines.begin(); it != m_coroutines.end();) {
|
||||
stdext::coroutine& coroutine = (*it);
|
||||
if(coroutine.is_finished())
|
||||
it = m_coroutines.erase(it);
|
||||
else {
|
||||
coroutine.resume();
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ScheduledEventPtr EventDispatcher::scheduleEvent(const std::function<void()>& callback, int delay)
|
||||
@@ -114,3 +124,8 @@ EventPtr EventDispatcher::addEvent(const std::function<void()>& callback, bool p
|
||||
m_eventList.push_back(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
void EventDispatcher::addCoroutine(const stdext::coroutine& coroutine)
|
||||
{
|
||||
m_coroutines.push_back(coroutine);
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@
|
||||
#include "scheduledevent.h"
|
||||
|
||||
#include <queue>
|
||||
#include <framework/stdext/coroutine.h>
|
||||
|
||||
// @bindsingleton g_dispatcher
|
||||
class EventDispatcher
|
||||
@@ -35,6 +36,7 @@ public:
|
||||
void shutdown();
|
||||
void poll();
|
||||
|
||||
void addCoroutine(const stdext::coroutine& coroutine);
|
||||
EventPtr addEvent(const std::function<void()>& callback, bool pushFront = false);
|
||||
ScheduledEventPtr scheduleEvent(const std::function<void()>& callback, int delay);
|
||||
ScheduledEventPtr cycleEvent(const std::function<void()>& callback, int delay);
|
||||
@@ -44,6 +46,7 @@ private:
|
||||
int m_pollEventsSize;
|
||||
stdext::boolean<false> m_disabled;
|
||||
std::priority_queue<ScheduledEventPtr, std::vector<ScheduledEventPtr>, lessScheduledEvent> m_scheduledEventList;
|
||||
std::list<stdext::coroutine> m_coroutines;
|
||||
};
|
||||
|
||||
extern EventDispatcher g_dispatcher;
|
||||
|
Reference in New Issue
Block a user