mirs_esp_pio
読み取り中…
検索中…
一致する文字列を見つけられません
Registry.hpp
[詳解]
1#pragma once
2#include <stddef.h>
3#include <stdint.h>
4
13template <typename Plugin, typename Data, size_t Capacity>
14class Registry {
15public:
21 bool add(Plugin *plugin, uint32_t divider = 1) {
22 if (plugin == nullptr || count_ >= Capacity || divider == 0) {
23 return false;
24 }
25 entries_[count_] = Entry{plugin, divider, 0, false};
26 ++count_;
27 return true;
28 }
29
31 size_t size() const { return count_; }
32
35 const char *name(size_t index) const {
36 return index < count_ ? entries_[index].plugin->name() : "";
37 }
38
41 bool isEnabled(size_t index) const {
42 return index < count_ && entries_[index].enabled;
43 }
44
49 bool beginPlugin(size_t index) {
50 if (index >= count_) {
51 return false;
52 }
53 Entry &entry = entries_[index];
54 entry.enabled = entry.plugin->begin();
55 return entry.enabled;
56 }
57
61 void updateAll(Data &data) {
62 for (size_t i = 0; i < count_; ++i) {
63 Entry &entry = entries_[i];
64 if (!entry.enabled) {
65 continue;
66 }
67 if (++entry.counter < entry.divider) {
68 continue;
69 }
70 entry.counter = 0;
71 entry.plugin->update(data);
72 }
73 }
74
75private:
76 struct Entry {
77 Plugin *plugin = nullptr;
78 uint32_t divider = 1;
79 uint32_t counter = 0;
80 bool enabled = false;
81 };
82 Entry entries_[Capacity] = {};
83 size_t count_ = 0;
84};
固定容量のタスク内プラグインレジストリ。ヒープ不使用。
size_t size() const
bool beginPlugin(size_t index)
プラグインを開始する。失敗時は無効化される。
const char * name(size_t index) const
void updateAll(Data &data)
有効なプラグインのうち周期到来分を更新する。
bool isEnabled(size_t index) const
bool add(Plugin *plugin, uint32_t divider=1)
プラグインを登録する。