mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-25 13:13:48 +01:00 
			
		
		
		
	[network] Optimize get_use_address() to return const reference instead of a copy (#11218)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
		| @@ -689,14 +689,9 @@ void EthernetComponent::add_phy_register(PHYRegister register_value) { this->phy | |||||||
| void EthernetComponent::set_type(EthernetType type) { this->type_ = type; } | void EthernetComponent::set_type(EthernetType type) { this->type_ = type; } | ||||||
| void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; } | void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; } | ||||||
|  |  | ||||||
| std::string EthernetComponent::get_use_address() const { | // set_use_address() is guaranteed to be called during component setup by Python code generation, | ||||||
|   if (this->use_address_.empty()) { | // so use_address_ will always be valid when get_use_address() is called - no fallback needed. | ||||||
|     // ".local" suffix length for mDNS hostnames | const std::string &EthernetComponent::get_use_address() const { return this->use_address_; } | ||||||
|     constexpr size_t mdns_local_suffix_len = 5; |  | ||||||
|     return make_name_with_suffix(App.get_name(), '.', "local", mdns_local_suffix_len); |  | ||||||
|   } |  | ||||||
|   return this->use_address_; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } | void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -88,7 +88,7 @@ class EthernetComponent : public Component { | |||||||
|  |  | ||||||
|   network::IPAddresses get_ip_addresses(); |   network::IPAddresses get_ip_addresses(); | ||||||
|   network::IPAddress get_dns_address(uint8_t num); |   network::IPAddress get_dns_address(uint8_t num); | ||||||
|   std::string get_use_address() const; |   const std::string &get_use_address() const; | ||||||
|   void set_use_address(const std::string &use_address); |   void set_use_address(const std::string &use_address); | ||||||
|   void get_eth_mac_address_raw(uint8_t *mac); |   void get_eth_mac_address_raw(uint8_t *mac); | ||||||
|   std::string get_eth_mac_address_pretty(); |   std::string get_eth_mac_address_pretty(); | ||||||
|   | |||||||
| @@ -85,22 +85,25 @@ network::IPAddresses get_ip_addresses() { | |||||||
|   return {}; |   return {}; | ||||||
| } | } | ||||||
|  |  | ||||||
| std::string get_use_address() { | const std::string &get_use_address() { | ||||||
|  |   // Global component pointers are guaranteed to be set by component constructors when USE_* is defined | ||||||
| #ifdef USE_ETHERNET | #ifdef USE_ETHERNET | ||||||
|   if (ethernet::global_eth_component != nullptr) |   return ethernet::global_eth_component->get_use_address(); | ||||||
|     return ethernet::global_eth_component->get_use_address(); |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef USE_MODEM | #ifdef USE_MODEM | ||||||
|   if (modem::global_modem_component != nullptr) |   return modem::global_modem_component->get_use_address(); | ||||||
|     return modem::global_modem_component->get_use_address(); |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef USE_WIFI | #ifdef USE_WIFI | ||||||
|   if (wifi::global_wifi_component != nullptr) |   return wifi::global_wifi_component->get_use_address(); | ||||||
|     return wifi::global_wifi_component->get_use_address(); | #endif | ||||||
|  |  | ||||||
|  | #if !defined(USE_ETHERNET) && !defined(USE_MODEM) && !defined(USE_WIFI) | ||||||
|  |   // Fallback when no network component is defined (e.g., host platform) | ||||||
|  |   static const std::string empty; | ||||||
|  |   return empty; | ||||||
| #endif | #endif | ||||||
|   return ""; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| }  // namespace network | }  // namespace network | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ bool is_connected(); | |||||||
| /// Return whether the network is disabled (only wifi for now) | /// Return whether the network is disabled (only wifi for now) | ||||||
| bool is_disabled(); | bool is_disabled(); | ||||||
| /// Get the active network hostname | /// Get the active network hostname | ||||||
| std::string get_use_address(); | const std::string &get_use_address(); | ||||||
| IPAddresses get_ip_addresses(); | IPAddresses get_ip_addresses(); | ||||||
|  |  | ||||||
| }  // namespace network | }  // namespace network | ||||||
|   | |||||||
| @@ -265,14 +265,9 @@ network::IPAddress WiFiComponent::get_dns_address(int num) { | |||||||
|     return this->wifi_dns_ip_(num); |     return this->wifi_dns_ip_(num); | ||||||
|   return {}; |   return {}; | ||||||
| } | } | ||||||
| std::string WiFiComponent::get_use_address() const { | // set_use_address() is guaranteed to be called during component setup by Python code generation, | ||||||
|   if (this->use_address_.empty()) { | // so use_address_ will always be valid when get_use_address() is called - no fallback needed. | ||||||
|     // ".local" suffix length for mDNS hostnames | const std::string &WiFiComponent::get_use_address() const { return this->use_address_; } | ||||||
|     constexpr size_t mdns_local_suffix_len = 5; |  | ||||||
|     return make_name_with_suffix(App.get_name(), '.', "local", mdns_local_suffix_len); |  | ||||||
|   } |  | ||||||
|   return this->use_address_; |  | ||||||
| } |  | ||||||
| void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } | void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } | ||||||
|  |  | ||||||
| #ifdef USE_WIFI_AP | #ifdef USE_WIFI_AP | ||||||
|   | |||||||
| @@ -275,7 +275,7 @@ class WiFiComponent : public Component { | |||||||
|  |  | ||||||
|   network::IPAddress get_dns_address(int num); |   network::IPAddress get_dns_address(int num); | ||||||
|   network::IPAddresses get_ip_addresses(); |   network::IPAddresses get_ip_addresses(); | ||||||
|   std::string get_use_address() const; |   const std::string &get_use_address() const; | ||||||
|   void set_use_address(const std::string &use_address); |   void set_use_address(const std::string &use_address); | ||||||
|  |  | ||||||
|   const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; } |   const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user