mirror of
https://github.com/esphome/esphome.git
synced 2026-09-20 11:38:48 +00:00
[text_sensor] Avoid string copies in callbacks by passing const ref
This commit is contained in:
@@ -73,11 +73,11 @@ 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->callbacks_.add_second(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->callbacks_.add_first(std::move(callback), &this->raw_count_);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,7 +65,7 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
|
||||
void internal_send_state_to_frontend(const std::string &state);
|
||||
|
||||
protected:
|
||||
PartitionedCallbackManager<void(std::string)> callbacks_;
|
||||
PartitionedCallbackManager<void(const std::string &)> callbacks_;
|
||||
|
||||
Filter *filter_list_{nullptr}; ///< Store all active filters.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user