destroy container along with the builders

This commit is contained in:
Nicu Hodos 2024-05-17 09:21:29 +02:00
parent 4b4f543117
commit 26b11129a8
3 changed files with 18 additions and 11 deletions

View File

@ -118,8 +118,8 @@ namespace Ha {
}
static void deleteAll() {
List<AbstractBuilder>::exec(builders, [](AbstractBuilder* builder)
{ delete builder; });
builders.forEach([](AbstractBuilder* builder) { delete builder; });
builders.empty();
}
};
List<AbstractBuilder> AbstractBuilder::builders;

View File

@ -51,15 +51,13 @@ namespace Mqtt {
void publishInit() {
Ha::publisher = publish;
List<Component>::exec(Component::components, [](Component* c)
{ c->publishConfig(); });
Component::components.forEach([](Component* c) { c->publishConfig(); });
AbstractBuilder::deleteAll();
ts.deleteTask(tPublishInit);
}
void publishCleanupConfig() {
List<Component>::exec(Component::components, [](Component* c)
{ c->publishCleanupConfig(); });
Component::components.forEach([](Component* c) { c->publishCleanupConfig(); });
}
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {

View File

@ -4,12 +4,12 @@ template <class T>
struct List {
struct Container {
T* t;
Container* next;
Container* next = nullptr;
Container(T* t) : t(t) {}
};
Container* first;
Container* last;
Container* first = nullptr;
Container* last = nullptr;
void add(T* t) {
Container* c = new Container{t};
@ -17,10 +17,19 @@ struct List {
last = c;
}
static void exec(List list, void(*f)(T*)) {
for (List::Container *c = list.first; c; c = c->next) {
void forEach(void(*f)(T*)) {
for (Container *c = first; c; c = c->next) {
f(c->t);
}
}
void empty() {
Container *c = first;
while (c) {
auto n = c->next;
delete c;
c = n;
}
}
};