use unique_ptr to avoid manual deletion
This commit is contained in:
parent
12663901ab
commit
fd40969c20
30
src/ha.h
30
src/ha.h
@ -97,14 +97,12 @@ namespace Ha {
|
||||
serializeJson(jsonDoc, message);
|
||||
|
||||
auto configTopic = buildConfigTopic();
|
||||
publisher(configTopic, message);
|
||||
delete configTopic;
|
||||
publisher(configTopic.get(), message);
|
||||
}
|
||||
|
||||
void publishCleanupConfig() {
|
||||
auto configTopic = buildConfigTopic();
|
||||
publisher(configTopic, "");
|
||||
delete configTopic;
|
||||
publisher(configTopic.get(), "");
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& jsonDoc) {
|
||||
@ -135,14 +133,14 @@ namespace Ha {
|
||||
jsonDoc["unique_id"] = uniqueId;
|
||||
}
|
||||
|
||||
const char* buildConfigTopic() {
|
||||
char* configTopic = new char[TOPIC_SIZE];
|
||||
unique_ptr<char[]> buildConfigTopic() {
|
||||
unique_ptr<char[]> topic(new char[TOPIC_SIZE]);
|
||||
if (multiValueComponent && deviceClass) {
|
||||
snprintf(configTopic, TOPIC_SIZE, CONFIG_TOPIC"_%s""/config", type, deviceClass, id);
|
||||
snprintf(topic.get(), TOPIC_SIZE, CONFIG_TOPIC"_%s""/config", type, deviceClass, id);
|
||||
} else {
|
||||
snprintf(configTopic, TOPIC_SIZE, CONFIG_TOPIC"/config", type, id);
|
||||
snprintf(topic.get(), TOPIC_SIZE, CONFIG_TOPIC"/config", type, id);
|
||||
}
|
||||
return configTopic;
|
||||
return topic;
|
||||
}
|
||||
};
|
||||
|
||||
@ -153,9 +151,8 @@ namespace Ha {
|
||||
|
||||
Command(Component* cmp, onMessage f) : f(f), cmp(cmp) {
|
||||
auto commandTopic = buildTopic();
|
||||
mapCommandTopics.insert({ string(commandTopic), this });
|
||||
mapCommandTopics.insert({ string(commandTopic.get()), this });
|
||||
mapCommandIds.insert({ string(cmp->id), this });
|
||||
delete commandTopic;
|
||||
}
|
||||
|
||||
virtual void onCommand(const char* msg) {
|
||||
@ -171,17 +168,16 @@ namespace Ha {
|
||||
|
||||
void buildConfig(JsonDocument& jsonDoc) override {
|
||||
auto commandTopic = buildTopic();
|
||||
jsonDoc["command_topic"] = commandTopic;
|
||||
delete commandTopic;
|
||||
jsonDoc["command_topic"] = commandTopic.get();
|
||||
if (retain) jsonDoc["retain"] = true;
|
||||
}
|
||||
private:
|
||||
Component* cmp;
|
||||
|
||||
char* buildTopic() {
|
||||
char* commandTopic = new char[TOPIC_SIZE];
|
||||
snprintf(commandTopic, TOPIC_SIZE, BASE_TOPIC"/set", cmp->id);
|
||||
return commandTopic;
|
||||
unique_ptr<char[]> buildTopic() {
|
||||
unique_ptr<char[]> topic(new char[TOPIC_SIZE]);
|
||||
snprintf(topic.get(), TOPIC_SIZE, BASE_TOPIC "/set", cmp->id);
|
||||
return topic;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user