[text_sensor] Avoid string copies in callbacks by passing const ref (#12503)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
J. Nick Koston
2025-12-20 06:46:13 -10:00
committed by GitHub
parent 121375ff39
commit 64269334ce
2 changed files with 6 additions and 6 deletions

View File

@@ -71,10 +71,10 @@ void TextSensor::clear_filters() {
this->filter_list_ = nullptr;
}
void TextSensor::add_on_state_callback(std::function<void(std::string)> callback) {
void TextSensor::add_on_state_callback(std::function<void(const std::string &)> callback) {
this->callback_.add(std::move(callback));
}
void TextSensor::add_on_raw_state_callback(std::function<void(std::string)> callback) {
void TextSensor::add_on_raw_state_callback(std::function<void(const std::string &)> callback) {
this->raw_callback_.add(std::move(callback));
}

View File

@@ -55,9 +55,9 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
/// Clear the entire filter chain.
void clear_filters();
void add_on_state_callback(std::function<void(std::string)> callback);
void add_on_state_callback(std::function<void(const std::string &)> callback);
/// Add a callback that will be called every time the sensor sends a raw value.
void add_on_raw_state_callback(std::function<void(std::string)> callback);
void add_on_raw_state_callback(std::function<void(const std::string &)> callback);
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
@@ -65,8 +65,8 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
void internal_send_state_to_frontend(const std::string &state);
protected:
LazyCallbackManager<void(std::string)> raw_callback_; ///< Storage for raw state callbacks.
LazyCallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.
LazyCallbackManager<void(const std::string &)> raw_callback_; ///< Storage for raw state callbacks.
LazyCallbackManager<void(const std::string &)> callback_; ///< Storage for filtered state callbacks.
Filter *filter_list_{nullptr}; ///< Store all active filters.
};