diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 962794b62d..95d0a36963 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -21,7 +21,6 @@ std::string state_class_to_string(StateClass state_class) { } Sensor::Sensor() : state(NAN), raw_state(NAN) {} -Sensor::~Sensor() { delete this->raw_callback_; } int8_t Sensor::get_accuracy_decimals() { if (this->accuracy_decimals_.has_value()) @@ -39,7 +38,7 @@ StateClass Sensor::get_state_class() { void Sensor::publish_state(float state) { this->raw_state = state; - if (this->raw_callback_ != nullptr) { + if (this->raw_callback_) { this->raw_callback_->call(state); } @@ -54,8 +53,8 @@ void Sensor::publish_state(float state) { void Sensor::add_on_state_callback(std::function &&callback) { this->callback_.add(std::move(callback)); } void Sensor::add_on_raw_state_callback(std::function &&callback) { - if (this->raw_callback_ == nullptr) { - this->raw_callback_ = new CallbackManager(); // NOLINT + if (!this->raw_callback_) { + this->raw_callback_ = std::make_unique>(); } this->raw_callback_->add(std::move(callback)); } diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index d8099116ac..8c7a2df7cd 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -7,6 +7,7 @@ #include "esphome/components/sensor/filter.h" #include +#include namespace esphome { namespace sensor { @@ -61,7 +62,6 @@ std::string state_class_to_string(StateClass state_class); class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBase_UnitOfMeasurement { public: explicit Sensor(); - ~Sensor(); /// Get the accuracy in decimals, using the manual override if set. int8_t get_accuracy_decimals(); @@ -153,8 +153,8 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBa void internal_send_state_to_frontend(float state); protected: - CallbackManager *raw_callback_{nullptr}; ///< Storage for raw state callbacks (lazy allocated). - CallbackManager callback_; ///< Storage for filtered state callbacks. + std::unique_ptr> raw_callback_; ///< Storage for raw state callbacks (lazy allocated). + CallbackManager callback_; ///< Storage for filtered state callbacks. Filter *filter_list_{nullptr}; ///< Store all active filters. diff --git a/esphome/components/text_sensor/text_sensor.cpp b/esphome/components/text_sensor/text_sensor.cpp index 000e6c2dd2..2a6300638d 100644 --- a/esphome/components/text_sensor/text_sensor.cpp +++ b/esphome/components/text_sensor/text_sensor.cpp @@ -6,11 +6,9 @@ namespace text_sensor { static const char *const TAG = "text_sensor"; -TextSensor::~TextSensor() { delete this->raw_callback_; } - void TextSensor::publish_state(const std::string &state) { this->raw_state = state; - if (this->raw_callback_ != nullptr) { + if (this->raw_callback_) { this->raw_callback_->call(state); } @@ -57,8 +55,8 @@ void TextSensor::add_on_state_callback(std::function callback this->callback_.add(std::move(callback)); } void TextSensor::add_on_raw_state_callback(std::function callback) { - if (this->raw_callback_ == nullptr) { - this->raw_callback_ = new CallbackManager(); // NOLINT + if (!this->raw_callback_) { + this->raw_callback_ = std::make_unique>(); } this->raw_callback_->add(std::move(callback)); } diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index de2702383e..2d7179d56b 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -6,6 +6,7 @@ #include "esphome/components/text_sensor/filter.h" #include +#include namespace esphome { namespace text_sensor { @@ -34,7 +35,6 @@ namespace text_sensor { class TextSensor : public EntityBase, public EntityBase_DeviceClass { public: TextSensor() = default; - ~TextSensor(); /// Getter-syntax for .state. std::string get_state() const; @@ -75,8 +75,9 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass { void internal_send_state_to_frontend(const std::string &state); protected: - CallbackManager *raw_callback_{nullptr}; ///< Storage for raw state callbacks (lazy allocated). - CallbackManager callback_; ///< Storage for filtered state callbacks. + std::unique_ptr> + raw_callback_; ///< Storage for raw state callbacks (lazy allocated). + CallbackManager callback_; ///< Storage for filtered state callbacks. Filter *filter_list_{nullptr}; ///< Store all active filters.