-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateManager.cpp
More file actions
56 lines (47 loc) · 1.24 KB
/
UpdateManager.cpp
File metadata and controls
56 lines (47 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <limits>
#include <algorithm>
#include "UpdateManager.h"
#include <Arduino.h>
namespace SA
{
IUpdatable::IUpdatable()
{
UpdateManager::Get().Register(*this);
}
IUpdatable::~IUpdatable()
{
UpdateManager::Get().Unregister(*this);
}
void UpdateManager::Update()
{
for (Entry& entry : m_updatables)
{
if (millis() < entry.m_nextUpdateTimestamp)
{
break;
}
entry.m_updatable->Update();
entry.m_nextUpdateTimestamp = millis() + entry.m_updatable->GetInterval();
}
std::sort(m_updatables.begin(), m_updatables.end(), [](const Entry& a, const Entry& b)
{
return a.m_nextUpdateTimestamp < b.m_nextUpdateTimestamp;
});
}
void UpdateManager::Register(IUpdatable& updatable)
{
m_updatables.push_back({&updatable, 0u});
}
void UpdateManager::Unregister(IUpdatable& updatable)
{
m_updatables.erase(std::find_if(m_updatables.begin(), m_updatables.end(), [&](const Entry& entry) { return entry.m_updatable == &updatable; }));
}
unsigned long UpdateManager::GetNextUpdateTimestamp() const
{
if (m_updatables.empty())
{
return std::numeric_limits<unsigned long>::max();
}
return m_updatables[0].m_nextUpdateTimestamp;
}
}