37 lines
832 B
C++
37 lines
832 B
C++
#include "TimerSet.h"
|
|
#include "Timer.h"
|
|
#include <iostream>
|
|
#include <thread>
|
|
|
|
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
|
|
pollingThread.detach();
|
|
}
|
|
|
|
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
|
|
|
void TimerSet::pollAllTimers() {
|
|
std::time_t timeNow;
|
|
if (time(&timeNow) > previousTime) {
|
|
previousTime = timeNow;
|
|
|
|
for (auto it = timers.begin(); it != timers.end(); it++) {
|
|
(*it)->tick();
|
|
}
|
|
}
|
|
}
|
|
|
|
TimerSet &TimerSet::Instance() {
|
|
// Meyer's Singleton implementation
|
|
static TimerSet pInstance;
|
|
return pInstance;
|
|
}
|
|
|
|
void TimerSet::poll10Hz() {
|
|
using namespace std::chrono_literals;
|
|
while (true) {
|
|
auto const pause = 100ms;
|
|
pollAllTimers();
|
|
std::this_thread::sleep_for(pause);
|
|
}
|
|
}
|