mirror of
https://github.com/esphome/esphome.git
synced 2026-09-21 20:18:43 +00:00
Replace std::function with a lightweight type-erased Callback struct (8 bytes on 32-bit vs 16 for std::function) in CallbackManager and LazyCallbackManager. Using C++20 if constexpr, small trivially-copyable callables like [this] lambdas (81% of all callback registrations) are stored inline in the function pointer context without heap allocation. This eliminates std::function null checks (and __throw_bad_function_call) from all entity callback paths, and templatizes add_on_*_callback methods across all entity types so lambdas flow through without being wrapped in std::function first. Also converts EntityStateBase from hand-rolled nullable CallbackManager pointers to LazyCallbackManager for consistency. Measured flash savings: - ESP8266 simple: -160 B - ESP8266 ratgdo: -592 B - ESP32 IDF (large): -456 B - ESP32 IDF (minimal): -212 B
33 lines
959 B
C++
33 lines
959 B
C++
#include "number.h"
|
|
#include "esphome/core/defines.h"
|
|
#include "esphome/core/controller_registry.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome::number {
|
|
|
|
static const char *const TAG = "number";
|
|
|
|
// Function implementation of LOG_NUMBER macro to reduce code size
|
|
void log_number(const char *tag, const char *prefix, const char *type, Number *obj) {
|
|
if (obj == nullptr) {
|
|
return;
|
|
}
|
|
|
|
ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
|
|
LOG_ENTITY_ICON(tag, prefix, *obj);
|
|
LOG_ENTITY_UNIT_OF_MEASUREMENT(tag, prefix, *obj);
|
|
LOG_ENTITY_DEVICE_CLASS(tag, prefix, *obj);
|
|
}
|
|
|
|
void Number::publish_state(float state) {
|
|
this->set_has_state(true);
|
|
this->state = state;
|
|
ESP_LOGD(TAG, "'%s' >> %.2f", this->get_name().c_str(), state);
|
|
this->state_callback_.call(state);
|
|
#if defined(USE_NUMBER) && defined(USE_CONTROLLER_REGISTRY)
|
|
ControllerRegistry::notify_number_update(this);
|
|
#endif
|
|
}
|
|
|
|
} // namespace esphome::number
|