]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Add move semantics to Variant
[libs/core.git] / source / time / timer.cpp
index e0af57cc78fcc41363632e169e33a8219653e9f1..eda782e0d92c5bf82c00418584e12917d03fa673 100644 (file)
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
@@ -9,21 +9,20 @@ namespace Msp {
 namespace Time {
 
 Timer::Timer():
-       sem(1),
-       blocking(false)
+       sem(1)
 { }
 
 Timer::~Timer()
 {
-       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
-               delete i->slot;
+       for(const SlotProxy &s: slots)
+               delete s.slot;
 }
 
 Timer::Slot &Timer::add(const TimeDelta &td)
 {
        Slot *s = new Slot(td);
        MutexLock l(mutex);
-       slots.push_back(s);
+       slots.push_back({ s });
        push_heap(slots.begin(), slots.end());
        if(blocking)
                sem.signal();
@@ -34,7 +33,7 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
 {
        Slot *s = new Slot(ts);
        MutexLock l(mutex);
-       slots.push_back(s);
+       slots.push_back({ s });
        push_heap(slots.begin(), slots.end());
        if(blocking)
                sem.signal();
@@ -44,22 +43,13 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
 void Timer::cancel(Slot &slot)
 {
        MutexLock l(mutex);
-       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
-               if(i->slot==&slot)
-               {
-                       delete i->slot;
-                       slots.erase(i);
-                       make_heap(slots.begin(), slots.end());
-                       return;
-               }
-}
-
-void Timer::tick(bool block)
-{
-       if(block)
-               tick();
-       else
-               tick(zero);
+       auto i = find_member(slots, &slot, &SlotProxy::slot);
+       if(i!=slots.end())
+       {
+               delete i->slot;
+               slots.erase(i);
+               make_heap(slots.begin(), slots.end());
+       }
 }
 
 void Timer::tick()
@@ -81,7 +71,7 @@ void Timer::do_tick(const TimeDelta &timeout)
        if(timeout>=zero)
                deadline = now()+timeout;
 
-       Slot *next = 0;
+       Slot *next = nullptr;
        {
                MutexLock l(mutex);
                while(1)
@@ -123,7 +113,7 @@ void Timer::do_tick(const TimeDelta &timeout)
                if(next->signal_timeout.emit() && next->increment())
                {
                        MutexLock l(mutex);
-                       slots.push_back(next);
+                       slots.push_back({ next });
                        push_heap(slots.begin(), slots.end());
                }
                else
@@ -162,10 +152,6 @@ bool Timer::Slot::increment()
 }
 
 
-Timer::SlotProxy::SlotProxy(Slot *s):
-       slot(s)
-{ }
-
 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
        return slot->get_timeout()>sp.slot->get_timeout();