[core] Deduplicate ControllerRegistry notify dispatch loop (#14394)

This commit is contained in:
J. Nick Koston
2026-03-02 06:57:59 -10:00
committed by GitHub
parent 54f410901f
commit f278250740
2 changed files with 28 additions and 8 deletions
+13 -8
View File
@@ -10,21 +10,26 @@ StaticVector<Controller *, CONTROLLER_REGISTRY_MAX> ControllerRegistry::controll
void ControllerRegistry::register_controller(Controller *controller) { controllers.push_back(controller); }
void ControllerRegistry::notify(void *obj, DispatchFunc dispatch) {
for (auto *controller : controllers) {
dispatch(controller, obj);
}
}
// Macro for standard registry notification dispatch - calls on_<entity_name>_update()
// Each wrapper passes a small trampoline lambda that calls the correct virtual method.
// NOLINTBEGIN(bugprone-macro-parentheses)
#define CONTROLLER_REGISTRY_NOTIFY(entity_type, entity_name) \
void ControllerRegistry::notify_##entity_name##_update(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \
for (auto *controller : controllers) { \
controller->on_##entity_name##_update(obj); \
} \
void ControllerRegistry::notify_##entity_name##_update(entity_type *obj) { \
notify(obj, [](Controller *c, void *o) { c->on_##entity_name##_update(static_cast<entity_type *>(o)); }); \
}
// Macro for entities where controller method has no "_update" suffix (Event, Update)
#define CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX(entity_type, entity_name) \
void ControllerRegistry::notify_##entity_name(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \
for (auto *controller : controllers) { \
controller->on_##entity_name(obj); \
} \
void ControllerRegistry::notify_##entity_name(entity_type *obj) { \
notify(obj, [](Controller *c, void *o) { c->on_##entity_name(static_cast<entity_type *>(o)); }); \
}
// NOLINTEND(bugprone-macro-parentheses)
#ifdef USE_BINARY_SENSOR
CONTROLLER_REGISTRY_NOTIFY(binary_sensor::BinarySensor, binary_sensor)
+15
View File
@@ -247,6 +247,21 @@ class ControllerRegistry {
#endif
protected:
/** Type-erased dispatch function pointer.
*
* Each notify method passes a small trampoline that calls the
* correct virtual method on Controller. The shared notify() loop
* iterates controllers once, calling the trampoline for each.
*/
using DispatchFunc = void (*)(Controller *, void *);
/** Shared dispatch loop - iterates controllers and calls dispatch for each.
*
* Marked noinline to ensure only one copy of the loop exists in flash,
* rather than being duplicated into each notify_*_update wrapper.
*/
static void __attribute__((noinline)) notify(void *obj, DispatchFunc dispatch);
static StaticVector<Controller *, CONTROLLER_REGISTRY_MAX> controllers;
};