From 26b11129a8321f01773cef165196d9ff3133eec1 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 17 May 2024 09:21:29 +0200 Subject: [PATCH] destroy container along with the builders --- gateway/include/ha.h | 4 ++-- gateway/include/mqtt.h | 6 ++---- gateway/include/utils.h | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index c8a4f33..81fe78f 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -118,8 +118,8 @@ namespace Ha { } static void deleteAll() { - List::exec(builders, [](AbstractBuilder* builder) - { delete builder; }); + builders.forEach([](AbstractBuilder* builder) { delete builder; }); + builders.empty(); } }; List AbstractBuilder::builders; diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index bfa5bcc..fdafe9e 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -51,15 +51,13 @@ namespace Mqtt { void publishInit() { Ha::publisher = publish; - List::exec(Component::components, [](Component* c) - { c->publishConfig(); }); + Component::components.forEach([](Component* c) { c->publishConfig(); }); AbstractBuilder::deleteAll(); ts.deleteTask(tPublishInit); } void publishCleanupConfig() { - List::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) { diff --git a/gateway/include/utils.h b/gateway/include/utils.h index b7d4076..4f63959 100644 --- a/gateway/include/utils.h +++ b/gateway/include/utils.h @@ -4,12 +4,12 @@ template 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; + } + } + };