[api] Use StringRef in handle_action_response to avoid temporary string (#12655)

This commit is contained in:
J. Nick Koston
2026-01-02 14:05:50 -10:00
committed by GitHub
parent e7001c5eea
commit 4cb066bcbf
3 changed files with 7 additions and 7 deletions
+2 -2
View File
@@ -394,7 +394,7 @@ void APIServer::register_action_response_callback(uint32_t call_id, ActionRespon
this->action_response_callbacks_.push_back({call_id, std::move(callback)}); this->action_response_callbacks_.push_back({call_id, std::move(callback)});
} }
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) { void APIServer::handle_action_response(uint32_t call_id, bool success, StringRef error_message) {
for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) { for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
if (it->call_id == call_id) { if (it->call_id == call_id) {
auto callback = std::move(it->callback); auto callback = std::move(it->callback);
@@ -406,7 +406,7 @@ void APIServer::handle_action_response(uint32_t call_id, bool success, const std
} }
} }
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message, void APIServer::handle_action_response(uint32_t call_id, bool success, StringRef error_message,
const uint8_t *response_data, size_t response_data_len) { const uint8_t *response_data, size_t response_data_len) {
for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) { for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) {
if (it->call_id == call_id) { if (it->call_id == call_id) {
+3 -3
View File
@@ -143,10 +143,10 @@ class APIServer : public Component,
// Action response handling // Action response handling
using ActionResponseCallback = std::function<void(const class ActionResponse &)>; using ActionResponseCallback = std::function<void(const class ActionResponse &)>;
void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback); void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback);
void handle_action_response(uint32_t call_id, bool success, const std::string &error_message); void handle_action_response(uint32_t call_id, bool success, StringRef error_message);
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
void handle_action_response(uint32_t call_id, bool success, const std::string &error_message, void handle_action_response(uint32_t call_id, bool success, StringRef error_message, const uint8_t *response_data,
const uint8_t *response_data, size_t response_data_len); size_t response_data_len);
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
#endif // USE_API_HOMEASSISTANT_SERVICES #endif // USE_API_HOMEASSISTANT_SERVICES
@@ -67,10 +67,10 @@ template<typename... Ts> class TemplatableKeyValuePair {
// the callback is invoked synchronously while the message is on the stack). // the callback is invoked synchronously while the message is on the stack).
class ActionResponse { class ActionResponse {
public: public:
ActionResponse(bool success, const std::string &error_message) : success_(success), error_message_(error_message) {} ActionResponse(bool success, StringRef error_message) : success_(success), error_message_(error_message) {}
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
ActionResponse(bool success, const std::string &error_message, const uint8_t *data, size_t data_len) ActionResponse(bool success, StringRef error_message, const uint8_t *data, size_t data_len)
: success_(success), error_message_(error_message) { : success_(success), error_message_(error_message) {
if (data == nullptr || data_len == 0) if (data == nullptr || data_len == 0)
return; return;