[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:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user