00001
00002
00003
00004
00005
00006
00007 #pragma once
00008
00009 #include <boost/function.hpp>
00010 #include <boost/thread/mutex.hpp>
00011 #include <list>
00012 #include <deque>
00013
00014 #include <Swiften/Base/API.h>
00015 #include <Swiften/EventLoop/Event.h>
00016
00017 namespace Swift {
00018 class EventOwner;
00019
00020 class SWIFTEN_API EventLoop {
00021 public:
00022 EventLoop();
00023 virtual ~EventLoop();
00024
00025 void postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner = boost::shared_ptr<EventOwner>());
00026 void removeEventsFromOwner(boost::shared_ptr<EventOwner> owner);
00027
00028 protected:
00033 virtual void post(const Event& event) = 0;
00034
00035 void handleEvent(const Event& event);
00036
00037 private:
00038 struct HasOwner {
00039 HasOwner(boost::shared_ptr<EventOwner> owner) : owner(owner) {}
00040 bool operator()(const Event& event) const { return event.owner == owner; }
00041 boost::shared_ptr<EventOwner> owner;
00042 };
00043 boost::mutex eventsMutex_;
00044 unsigned int nextEventID_;
00045 std::list<Event> events_;
00046 bool handlingEvents_;
00047 std::deque<Event> eventsToHandle_;
00048 };
00049 }