From: Mikko Rasa Date: Sat, 11 Jun 2011 11:01:43 +0000 (+0300) Subject: Use pimpl in Poller X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e46de55d3c8f65d3b0eeaee76247476695e9eb7c;p=libs%2Fcore.git Use pimpl in Poller --- diff --git a/source/io/eventdispatcher.cpp b/source/io/eventdispatcher.cpp index 06a594f..1583597 100644 --- a/source/io/eventdispatcher.cpp +++ b/source/io/eventdispatcher.cpp @@ -66,8 +66,8 @@ void EventDispatcher::object_deleted(Base *obj) void EventDispatcher::dispatch() { - const Poller::SlotSeq &result = poller.get_result(); - for(Poller::SlotSeq::const_iterator i=result.begin(); i!=result.end(); ++i) + const Poller::SlotList &result = poller.get_result(); + for(Poller::SlotList::const_iterator i=result.begin(); i!=result.end(); ++i) i->object->event(i->events); } diff --git a/source/io/poll.cpp b/source/io/poll.cpp index d610665..d78a2c0 100644 --- a/source/io/poll.cpp +++ b/source/io/poll.cpp @@ -1,5 +1,8 @@ #include #include +#ifndef WIN32 +#include +#endif #include #include #include @@ -87,8 +90,19 @@ inline PollEvent do_poll(Base &obj, PollEvent pe, int timeout) namespace Msp { namespace IO { +struct Poller::Private +{ +#ifdef WIN32 + vector handles; +#else + vector pfd; +#endif +}; + + Poller::Poller(): - pfd_dirty(false) + priv(new Private), + objs_changed(false) { } void Poller::set_object(Base &obj, PollEvent ev) @@ -97,15 +111,15 @@ void Poller::set_object(Base &obj, PollEvent ev) if(ev) obj.get_event_handle(); - SlotMap::iterator i = objects.find(&obj); + EventMap::iterator i = objects.find(&obj); if(i!=objects.end()) { if(ev) - i->second.events = ev; + i->second = ev; else objects.erase(i); - pfd_dirty = true; + objs_changed = true; } else if(ev) { @@ -113,9 +127,9 @@ void Poller::set_object(Base &obj, PollEvent ev) if(objects.size()>=MAXIMUM_WAIT_OBJECTS) throw logic_error("Maximum number of wait objects reached"); #endif - objects.insert(SlotMap::value_type(&obj, Slot(&obj, ev))); + objects.insert(EventMap::value_type(&obj, ev)); - pfd_dirty = true; + objs_changed = true; } } @@ -132,28 +146,32 @@ int Poller::poll(const Time::TimeDelta &timeout) return do_poll(static_cast(timeout/Time::msec)); } -void Poller::rebuild_pfd() +void Poller::rebuild_array() { - pfd.clear(); +#ifdef WIN32 + priv->handles.clear(); - pollfd p; + for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i) + priv->handles.push_back(i->first->get_event_handle()); +#else + priv->pfd.clear(); - for(SlotMap::iterator i=objects.begin(); i!=objects.end(); ++i) + for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i) { - p.fd = i->second.object->get_event_handle(); -#ifndef WIN32 - p.events = sys_poll_event(i->second.events); -#endif - pfd.push_back(p); + pollfd p; + p.fd = i->first->get_event_handle(); + p.events = sys_poll_event(i->second); + priv->pfd.push_back(p); } +#endif - pfd_dirty = false; + objs_changed = false; } int Poller::do_poll(int timeout) { - if(pfd_dirty) - rebuild_pfd(); + if(objs_changed) + rebuild_array(); poll_result.clear(); @@ -161,12 +179,12 @@ int Poller::do_poll(int timeout) if(timeout<0) timeout = INFINITE; - DWORD ret = WaitForMultipleObjects(pfd.size(), &pfd.front().fd, false, timeout); - if(/*ret>=WAIT_OBJECT_0 &&*/ rethandles.size(), &priv->handles.front(), false, timeout); + if(/*ret>=WAIT_OBJECT_0 &&*/ rethandles.size()) { - SlotMap::iterator i = objects.begin(); + EventMap::iterator i = objects.begin(); advance(i, ret-WAIT_OBJECT_0); - poll_result.push_back(Slot(i->second.object, i->second.events)); + poll_result.push_back(Slot(i->first, i->second)); return 1; } @@ -175,7 +193,7 @@ int Poller::do_poll(int timeout) return 0; #else - int ret = ::poll(&pfd.front(), pfd.size(), timeout); + int ret = ::poll(&priv->pfd.front(), priv->pfd.size(), timeout); if(ret==-1) { if(errno==EINTR) @@ -185,11 +203,11 @@ int Poller::do_poll(int timeout) } int n = ret; - SlotMap::iterator j = objects.begin(); - for(std::vector::iterator i=pfd.begin(); (i!=pfd.end() && n>0); ++i,++j) + EventMap::iterator j = objects.begin(); + for(vector::iterator i=priv->pfd.begin(); (i!=priv->pfd.end() && n>0); ++i, ++j) if(i->revents) { - poll_result.push_back(Slot(j->second.object, poll_event_from_sys(i->revents))); + poll_result.push_back(Slot(j->first, poll_event_from_sys(i->revents))); --n; } diff --git a/source/io/poll.h b/source/io/poll.h index 5952374..f59b499 100644 --- a/source/io/poll.h +++ b/source/io/poll.h @@ -1,14 +1,10 @@ #ifndef MSP_IO_POLL_H_ #define MSP_IO_POLL_H_ -#ifndef WIN32 -#include -#endif #include #include #include #include -#include "types.h" namespace Msp { namespace IO { @@ -45,21 +41,16 @@ public: Slot(Base *o, PollEvent e): object(o), events(e) { } }; - typedef std::list SlotSeq; + typedef std::list SlotList; private: - typedef std::map SlotMap; + typedef std::map EventMap; -#ifdef WIN32 - struct pollfd - { - Handle fd; - }; -#endif + struct Private; - SlotMap objects; - std::vector pfd; - bool pfd_dirty; - SlotSeq poll_result; + EventMap objects; + Private *priv; + bool objs_changed; + SlotList poll_result; public: Poller(); @@ -68,10 +59,10 @@ public: int poll(); int poll(const Time::TimeDelta &); private: - void rebuild_pfd(); + void rebuild_array(); int do_poll(int); public: - const SlotSeq &get_result() const { return poll_result; } + const SlotList &get_result() const { return poll_result; } }; PollEvent poll(Base &, PollEvent);