don't create Builders on heap, use temporary objects instead

This commit is contained in:
Nicu Hodos 2025-10-08 09:43:59 +02:00
parent 7150dc08c9
commit 9511f08d2b
4 changed files with 13 additions and 36 deletions

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "ha-mqtt", "name": "ha-mqtt",
"version": "1.13.0", "version": "2.0.0",
"description": "Home Assistant classes for integration with MQTT auto discovery", "description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -22,7 +22,7 @@ namespace HaESP {
ESP.getHeapStats(&hfree, &hmax, &hfrag); ESP.getHeapStats(&hfree, &hmax, &hfrag);
char value[128]; char value[128];
snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax); snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax);
GenericSensor::mapSensors["heap_fragmentation"]->updateState(value); GenericSensor::mapSensors["heap_fragmentation"]->updateState(value);
GenericSensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str()); GenericSensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
GenericSensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str()); GenericSensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());
@ -42,19 +42,19 @@ namespace HaESP {
template <class T> template <class T>
Builder<T>& heapStats(Builder<T>& builder) { Builder<T>& heapStats(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" }) builder.addDiagnostic(Builder<Sensor>(new Sensor{ "Heap fragmentation", "heap_fragmentation" })
.withUnitMeasure("%") .withUnitMeasure("%")
.withPrecision(0) .withPrecision(0)
.withValueTemplate("{{ value_json.fragmentation }}") .withValueTemplate("{{ value_json.fragmentation }}")
.withJsonAttributes("{{ value_json.heap | tojson }}") .withJsonAttributes("{{ value_json.heap | tojson }}")
.build()); .build());
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" }) builder.addDiagnostic(Builder<Sensor>(new Sensor{ "Heap free", "heap_free" })
.withDeviceClass("data_size") .withDeviceClass("data_size")
.withUnitMeasure("B") .withUnitMeasure("B")
.withPrecision(0) .withPrecision(0)
.build() .build()
); );
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap max free block", "heap_max_free_block" }) builder.addDiagnostic(Builder<Sensor>(new Sensor{ "Heap max free block", "heap_max_free_block" })
.withDeviceClass("data_size") .withDeviceClass("data_size")
.withUnitMeasure("B") .withUnitMeasure("B")
.withPrecision(0) .withPrecision(0)
@ -66,14 +66,14 @@ namespace HaESP {
template <class T> template <class T>
Builder<T>& restartInfo(Builder<T>& builder) { Builder<T>& restartInfo(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance((new Sensor{ "Restart reason", "restart_reason" })).build()); builder.addDiagnostic((new Sensor{ "Restart reason", "restart_reason" }));
activeSensors.restartInfo = true; activeSensors.restartInfo = true;
return builder; return builder;
} }
template <class T> template <class T>
Builder<T>& wifiInfo(Builder<T>& builder) { Builder<T>& wifiInfo(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance(( builder.addDiagnostic(Builder<Sensor>((
new Sensor{ "WiFi Signal (RSSI)", "wifi_signal_strength" })) new Sensor{ "WiFi Signal (RSSI)", "wifi_signal_strength" }))
.withDeviceClass("signal_strength") .withDeviceClass("signal_strength")
.withUnitMeasure("dBm") .withUnitMeasure("dBm")
@ -85,12 +85,12 @@ namespace HaESP {
} }
Builder<Button>& restartButton() { Builder<Button>& restartButton() {
return Builder<Button>::instance(new Button{"Restart", "restart", return (new Builder<Button>(new Button{"Restart", "restart",
[](const char* msg) { [](const char* msg) {
if (strcmp("PRESS", msg) == 0) tRestart.restartDelayed(); if (strcmp("PRESS", msg) == 0) tRestart.restartDelayed();
} }
}) }))
.withIcon("mdi:restart"); ->withIcon("mdi:restart");
} }
void enableSensors() { void enableSensors() {

View File

@ -385,38 +385,16 @@ namespace Ha {
} }
}; };
struct AbstractBuilder {
inline static List<AbstractBuilder> builders;
static void deleteAll() {
builders.forEach([](AbstractBuilder* builder) { delete builder; });
builders.empty();
}
protected:
AbstractBuilder() {
builders.add(this);
}
};
template <class T> template <class T>
struct Builder : AbstractBuilder { struct Builder {
T* cmp; T* cmp;
Builder(T* cmp) : AbstractBuilder(), cmp(cmp) {} Builder(T* cmp) : cmp(cmp) {}
Builder(const char* id) : AbstractBuilder() { Builder(const char* id) {
cmp = new T{ id }; cmp = new T{ id };
} }
static Builder& instance(T* c) {
return *(new Builder<T>(c));
}
static Builder& instance(const char* id) {
return *(new Builder<T>(id));
}
T* build() { T* build() {
return static_cast<T*>(cmp); return static_cast<T*>(cmp);
} }

View File

@ -30,7 +30,6 @@ namespace Mqtt {
static bool firstTime = true; static bool firstTime = true;
if (firstTime) { if (firstTime) {
Component::components.forEach([](Component* c) { c->publishConfig(); }); Component::components.forEach([](Component* c) { c->publishConfig(); });
AbstractBuilder::deleteAll();
HaESP::enableSensors(); HaESP::enableSensors();
firstTime = false; firstTime = false;
} }