Switch Controller
Loading...
Searching...
No Matches
MacroCollection.h
Go to the documentation of this file.
1#pragma once
2
3#include "pch.h"
4
5#include "Macro.h"
6
8private:
9 std::vector<std::shared_ptr<Macro>> macros;
10 std::map<std::shared_ptr<Macro>, std::chrono::steady_clock::time_point>
11 activeMacros;
12
13public:
14 MacroCollection(const std::vector<std::shared_ptr<Macro>> &macros)
15 : macros(macros){};
16 MacroCollection(const MacroCollection &other) = delete;
17 MacroCollection(MacroCollection &&other) = default;
19
20 std::array<uint8_t, 8>
21 getData(std::array<uint8_t, 8> intitial,
22 const std::function<std::array<uint8_t, 8>(std::array<uint8_t, 8>,
23 std::array<uint8_t, 8>)>
24 mergeFunction) {
25
26 std::array<uint8_t, 8> res = intitial;
27 if (activeMacros.size()) {
28 auto now = std::chrono::steady_clock::now();
29 std::vector<std::shared_ptr<Macro>> toAdd;
30 std::vector<std::shared_ptr<Macro>> toRemove;
31
32 for (auto it : activeMacros) {
33 auto diff = now - it.second;
34 uint64_t time = static_cast<uint64_t>(
35 std::chrono::duration_cast<std::chrono::milliseconds>(diff)
36 .count());
37 if (auto data = it.first->getDataframe(time)) {
38 res = mergeFunction(res, data.value());
39 } else {
40 toAdd.push_back(it.first->getNextMacro());
41 toRemove.push_back(it.first);
42 }
43 }
44
45 for (auto m : toRemove)
46 activeMacros.erase(m);
47
48 for (auto m : toAdd)
49 if (m)
50 activeMacros.insert({m, std::chrono::steady_clock::now()});
51 }
52 return res;
53 }
55 for (auto m : macros) {
56 if (activeMacros.find(m) == activeMacros.end() && m->active()) {
57 activeMacros.insert({m, std::chrono::steady_clock::now()});
58 }
59 }
60 }
61 bool isMacroActive() const { return activeMacros.size(); }
62 void deactivateMacros() { activeMacros.clear(); }
63 void pushBackMacro(std::shared_ptr<Macro> macro) {
64 macros.push_back(macro);
65 }
66};
Definition: MacroCollection.h:7
bool isMacroActive() const
Definition: MacroCollection.h:61
MacroCollection(const MacroCollection &other)=delete
MacroCollection & operator=(MacroCollection &&other)=default
void deactivateMacros()
Definition: MacroCollection.h:62
MacroCollection(const std::vector< std::shared_ptr< Macro > > &macros)
Definition: MacroCollection.h:14
std::array< uint8_t, 8 > getData(std::array< uint8_t, 8 > intitial, const std::function< std::array< uint8_t, 8 >(std::array< uint8_t, 8 >, std::array< uint8_t, 8 >)> mergeFunction)
Definition: MacroCollection.h:21
void pushBackMacro(std::shared_ptr< Macro > macro)
Definition: MacroCollection.h:63
MacroCollection(MacroCollection &&other)=default
void activateMacros()
Definition: MacroCollection.h:54