d98c29ecbb
the idea is that an invoker holds a weak_ptr to a steppable. and anything with an invoker can advance a steppable app::add_sort has become too heavy because of the way timer and invoker are initialized now.
42 lines
1022 B
C++
42 lines
1022 B
C++
#include <chrono>
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
#include "timer.hpp"
|
|
#include "Invoker.hpp"
|
|
|
|
using namespace std::chrono;
|
|
using namespace std;
|
|
|
|
Timer::Timer(size_t triggeramount, std::unique_ptr<Invoker> invoker, milliseconds triggerinterval)
|
|
:triggeramount(triggeramount)
|
|
, invoker(std::move(invoker))
|
|
, triggerinterval(triggerinterval){}
|
|
|
|
void Timer::add_time(milliseconds deltatime) {
|
|
accumulator += deltatime;
|
|
if (accumulator >= triggerinterval) {
|
|
accumulator -= triggerinterval;
|
|
cout << "request in timer\n";
|
|
invoker->request();
|
|
triggercount++;
|
|
if (triggeramount != 0 && triggercount >= triggeramount){
|
|
isexpired=true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void TimerManager::add_timer(Timer timer){
|
|
timers.push_back(std::move(timer));
|
|
}
|
|
|
|
void TimerManager::update_timers() {
|
|
steady_clock::time_point currenttime =
|
|
steady_clock::now();
|
|
milliseconds deltatime = duration_cast<milliseconds>(currenttime-previoustime);
|
|
for(Timer &mytimer: timers){
|
|
mytimer.add_time(deltatime);
|
|
}
|
|
previoustime = currenttime;
|
|
}
|