Implement support for coroutines in C++

This commit is contained in:
Eduardo Bart
2012-08-04 01:46:04 -03:00
parent 6a68569319
commit fa15c25951
3 changed files with 121 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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;