added a clear() method to the TimerEventProxy to allow the removal of all pending...
[senf.git] / senf / Scheduler / TimerEventProxy.ct
index a5548d9..0caaf20 100644 (file)
     \brief TimerEventProxy non-inline template implementation */
 
 // Custom includes
+#include <senf/Utils/membind.hh>
 
 #define prefix_
-///////////////////////////////ct.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 
 template<typename IdType>
 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
@@ -40,7 +41,7 @@ prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string co
 template<typename IdType>
 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
 {
-    ClockService::clock_type now = ClockService::now();
+    ClockService::clock_type now = senf::scheduler::now();
     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
     while (it != entrySetByTimeout.end() && it->timeout <= now) {
         Entry item (*it);
@@ -58,8 +59,15 @@ template<typename IdType>
 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
         ClockService::clock_type timeout, IdType const & id, Callback cb)
 {
-    // insert new entry
-    entrySetByTimeout.insert( Entry(timeout, id, cb));
+    // insert new entry or replace the timeout of an entry already indexed
+    typename EntrySetById_t::iterator i = entrySetById.find(id);
+    if(i == entrySetById.end())
+       entrySetByTimeout.insert( Entry(timeout, id, cb));
+       else{
+               Entry tmp = *i;
+               tmp.timeout = timeout;
+               entrySetById.replace(i,tmp);
+       }
     // the scheduler time to the first earliest timeout (ordered index)
     timer.timeout( entrySetByTimeout.begin()->timeout);
 }
@@ -97,7 +105,20 @@ prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::sc
     return tmp;
 }
 
-///////////////////////////////ct.e////////////////////////////////////////
+template<typename IdType>
+prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
+  const
+{
+    return entrySetByTimeout.size();
+}
+
+template<typename IdType>
+prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
+{
+    entrySetByTimeout.clear();
+}
+
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 
 \f