mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 11:26:39 +00:00
[api] Use StringRef in handle_action_response to avoid temporary string (#12655)
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user