diff options
Diffstat (limited to 'Swiften/EventLoop')
-rw-r--r-- | Swiften/EventLoop/EventLoop.cpp | 4 | ||||
-rw-r--r-- | Swiften/EventLoop/EventLoop.h | 18 | ||||
-rw-r--r-- | Swiften/EventLoop/EventOwner.cpp | 8 | ||||
-rw-r--r-- | Swiften/EventLoop/EventOwner.h | 8 | ||||
-rw-r--r-- | Swiften/EventLoop/MainEventLoop.cpp | 4 | ||||
-rw-r--r-- | Swiften/EventLoop/MainEventLoop.h | 5 | ||||
-rw-r--r-- | Swiften/EventLoop/Makefile.inc | 1 | ||||
-rw-r--r-- | Swiften/EventLoop/SimpleEventLoop.cpp | 2 |
8 files changed, 33 insertions, 17 deletions
diff --git a/Swiften/EventLoop/EventLoop.cpp b/Swiften/EventLoop/EventLoop.cpp index cec149c..3c3c356 100644 --- a/Swiften/EventLoop/EventLoop.cpp +++ b/Swiften/EventLoop/EventLoop.cpp @@ -30,7 +30,7 @@ void EventLoop::handleEvent(const Event& event) { } } -void EventLoop::postEvent(boost::function<void ()> callback, void* owner) { +void EventLoop::postEvent(boost::function<void ()> callback, boost::shared_ptr<EventOwner> owner) { Event event(owner, callback); { boost::lock_guard<boost::mutex> lock(eventsMutex_); @@ -41,7 +41,7 @@ void EventLoop::postEvent(boost::function<void ()> callback, void* owner) { post(event); } -void EventLoop::removeEventsFromOwner(void* owner) { +void EventLoop::removeEventsFromOwner(boost::shared_ptr<EventOwner> owner) { boost::lock_guard<boost::mutex> lock(eventsMutex_); events_.remove_if(HasOwner(owner)); } diff --git a/Swiften/EventLoop/EventLoop.h b/Swiften/EventLoop/EventLoop.h index 2f04f32..bf6f929 100644 --- a/Swiften/EventLoop/EventLoop.h +++ b/Swiften/EventLoop/EventLoop.h @@ -1,22 +1,22 @@ -#ifndef SWIFTEN_EventLoop_H -#define SWIFTEN_EventLoop_H +#pragma once #include <boost/function.hpp> #include <boost/thread/mutex.hpp> #include <list> namespace Swift { + class EventOwner; class EventLoop { public: EventLoop(); virtual ~EventLoop(); - void postEvent(boost::function<void ()> event, void* owner); - void removeEventsFromOwner(void* owner); + void postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner = boost::shared_ptr<EventOwner>()); + void removeEventsFromOwner(boost::shared_ptr<EventOwner> owner); protected: struct Event { - Event(void* owner, const boost::function<void()>& callback) : + Event(boost::shared_ptr<EventOwner> owner, const boost::function<void()>& callback) : owner(owner), callback(callback) { } @@ -25,7 +25,7 @@ namespace Swift { } unsigned int id; - void* owner; + boost::shared_ptr<EventOwner> owner; boost::function<void()> callback; }; @@ -39,14 +39,12 @@ namespace Swift { private: struct HasOwner { - HasOwner(void* owner) : owner(owner) {} + HasOwner(boost::shared_ptr<EventOwner> owner) : owner(owner) {} bool operator()(const Event& event) { return event.owner == owner; } - void* owner; + boost::shared_ptr<EventOwner> owner; }; boost::mutex eventsMutex_; unsigned int nextEventID_; std::list<Event> events_; }; } - -#endif diff --git a/Swiften/EventLoop/EventOwner.cpp b/Swiften/EventLoop/EventOwner.cpp new file mode 100644 index 0000000..4818b3c --- /dev/null +++ b/Swiften/EventLoop/EventOwner.cpp @@ -0,0 +1,8 @@ +#include "Swiften/EventLoop/EventOwner.h" + +namespace Swift { + +EventOwner::~EventOwner() { +} + +} diff --git a/Swiften/EventLoop/EventOwner.h b/Swiften/EventLoop/EventOwner.h new file mode 100644 index 0000000..8da95e0 --- /dev/null +++ b/Swiften/EventLoop/EventOwner.h @@ -0,0 +1,8 @@ +#pragma once + +namespace Swift { + class EventOwner { + public: + virtual ~EventOwner(); + }; +} diff --git a/Swiften/EventLoop/MainEventLoop.cpp b/Swiften/EventLoop/MainEventLoop.cpp index c306d3f..d7de6c6 100644 --- a/Swiften/EventLoop/MainEventLoop.cpp +++ b/Swiften/EventLoop/MainEventLoop.cpp @@ -23,11 +23,11 @@ void MainEventLoop::resetInstance() { instance_ = 0; } -void MainEventLoop::postEvent(boost::function<void ()> event, void* owner) { +void MainEventLoop::postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner) { getInstance()->postEvent(event, owner); } -void MainEventLoop::removeEventsFromOwner(void* owner) { +void MainEventLoop::removeEventsFromOwner(boost::shared_ptr<EventOwner> owner) { getInstance()->removeEventsFromOwner(owner); } diff --git a/Swiften/EventLoop/MainEventLoop.h b/Swiften/EventLoop/MainEventLoop.h index f29dbd4..0046546 100644 --- a/Swiften/EventLoop/MainEventLoop.h +++ b/Swiften/EventLoop/MainEventLoop.h @@ -8,6 +8,7 @@ namespace Swift { class EventLoop; + class EventOwner; class MainEventLoop { friend class EventLoop; @@ -18,9 +19,9 @@ namespace Swift { * If the owner is destroyed, all events should be removed from the * loop using removeEventsFromOwner(). */ - static void postEvent(boost::function<void ()> event, void* owner = 0); + static void postEvent(boost::function<void ()> event, boost::shared_ptr<EventOwner> owner = 0); - static void removeEventsFromOwner(void* owner); + static void removeEventsFromOwner(boost::shared_ptr<EventOwner> owner); template<typename T> static void deleteLater(T* t) { diff --git a/Swiften/EventLoop/Makefile.inc b/Swiften/EventLoop/Makefile.inc index 894b18d..3347ae6 100644 --- a/Swiften/EventLoop/Makefile.inc +++ b/Swiften/EventLoop/Makefile.inc @@ -1,4 +1,5 @@ SWIFTEN_SOURCES += \ + Swiften/EventLoop/EventOwner.cpp \ Swiften/EventLoop/EventLoop.cpp \ Swiften/EventLoop/SimpleEventLoop.cpp \ Swiften/EventLoop/MainEventLoop.cpp diff --git a/Swiften/EventLoop/SimpleEventLoop.cpp b/Swiften/EventLoop/SimpleEventLoop.cpp index 96ad774..357e158 100644 --- a/Swiften/EventLoop/SimpleEventLoop.cpp +++ b/Swiften/EventLoop/SimpleEventLoop.cpp @@ -29,7 +29,7 @@ void SimpleEventLoop::run() { } void SimpleEventLoop::stop() { - postEvent(boost::bind(&SimpleEventLoop::doStop, this), 0); + postEvent(boost::bind(&SimpleEventLoop::doStop, this)); } void SimpleEventLoop::doStop() { |