Use vectors for storage in Poller
[libs/core.git] / source / io / poll.cpp
index 6da777cf4470778f4e6401dbcc39f1a3a539b4e7..2e33502e8433969059dccdd1ecbde834a3e6aff7 100644 (file)
@@ -10,6 +10,7 @@ namespace IO {
 
 Poller::Poller():
        priv(new Private),
+       events_changed(false),
        objs_changed(false)
 { }
 
@@ -24,26 +25,31 @@ void Poller::set_object(EventObject &obj, PollEvent ev)
        if(ev)
                obj.get_event_handle();
 
-       EventMap::iterator i = objects.find(&obj);
-       if(i!=objects.end())
-       {
-               if(ev)
-                       i->second = ev;
-               else
-                       objects.erase(i);
+       for(vector<Slot>::iterator i=objects.begin(); i!=objects.end(); ++i)
+               if(i->object==&obj)
+               {
+                       if(ev)
+                               i->events = ev;
+                       else
+                       {
+                               *i = objects.back();
+                               objects.pop_back();
+                               objs_changed = true;
+                       }
+                       events_changed = true;
+                       return;
+               }
+
+       if(!ev)
+               return;
 
-               objs_changed = true;
-       }
-       else if(ev)
-       {
 #ifdef _WIN32
-               if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
-                       throw logic_error("Maximum number of wait objects reached");
+       if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
+               throw logic_error("Maximum number of wait objects reached");
 #endif
-               objects.insert(EventMap::value_type(&obj, ev));
 
-               objs_changed = true;
-       }
+       objects.push_back(Slot(&obj, ev));
+       objs_changed = true;
 }
 
 int Poller::poll()
@@ -61,8 +67,12 @@ int Poller::poll(const Time::TimeDelta &timeout)
 
 int Poller::do_poll(int timeout)
 {
-       if(objs_changed)
+       if(objs_changed || events_changed)
+       {
                rebuild_array();
+               events_changed = false;
+               objs_changed = false;
+       }
 
        poll_result.clear();