[light] Normalize trigger args to const remove_cvref_t<T> &

This commit is contained in:
J. Nick Koston
2026-05-03 17:29:25 -05:00
parent 22252ef5b5
commit 10ea8c3492
2 changed files with 19 additions and 14 deletions
+7 -8
View File
@@ -37,16 +37,15 @@ template<bool HasTransitionLength, typename... Ts> class ToggleAction : public A
// Trigger args are forwarded to the apply function so user lambdas // Trigger args are forwarded to the apply function so user lambdas
// (e.g. `brightness: !lambda "return x;"`) keep working. // (e.g. `brightness: !lambda "return x;"`) keep working.
// //
// Trigger args are forwarded as `Ts...`. The previous `const Ts &...` // Trigger args are normalized to `const std::remove_cvref_t<Ts> &...` so
// form caused codegen to emit `const T &` for each arg in the apply // the codegen can emit a matching parameter list for both the apply lambda
// lambda's parameter list, which is invalid C++ source text when T is // and any inner field lambdas without producing invalid C++ source text
// already a reference (e.g. `const std::string & &` for triggers that // (e.g. `const T & &` if Ts already carries a reference, or `const const
// pass `std::string &`). Forwarding `Ts...` lets the codegen reuse the // T &` if Ts already carries a const). This keeps trigger args no-copy
// trigger's `args` types unchanged for both the apply lambda and any // regardless of whether the trigger supplies `T`, `T &`, or `const T &`.
// inner field lambdas, so they always type-match.
template<typename... Ts> class LightControlAction : public Action<Ts...> { template<typename... Ts> class LightControlAction : public Action<Ts...> {
public: public:
using ApplyFn = void (*)(LightState *, LightCall &, Ts...); using ApplyFn = void (*)(LightState *, LightCall &, const std::remove_cvref_t<Ts> &...);
LightControlAction(LightState *parent, ApplyFn apply) : parent_(parent), apply_(apply) {} LightControlAction(LightState *parent, ApplyFn apply) : parent_(parent), apply_(apply) {}
void play(const Ts &...x) override { void play(const Ts &...x) override {
+12 -6
View File
@@ -200,6 +200,15 @@ async def light_control_to_code(config, action_id, template_arg, args):
(CONF_WARM_WHITE, "set_warm_white", cg.float_), (CONF_WARM_WHITE, "set_warm_white", cg.float_),
) )
# Normalize trigger args to `const std::remove_cvref_t<T> &` so the
# apply lambda and any inner field lambdas (generated below via
# `process_lambda`) share one parameter spelling that's well-formed for
# any T (value, ref, or const-ref). Matches LightControlAction::ApplyFn.
normalized_args = [
(cg.RawExpression(f"const std::remove_cvref_t<{cg.safe_exp(t)}> &"), n)
for t, n in args
]
fwd_args = ", ".join(name for _, name in args) fwd_args = ", ".join(name for _, name in args)
body_lines: list[str] = [] body_lines: list[str] = []
@@ -208,7 +217,7 @@ async def light_control_to_code(config, action_id, template_arg, args):
continue continue
value = config[conf_key] value = config[conf_key]
if isinstance(value, Lambda): if isinstance(value, Lambda):
inner = await cg.process_lambda(value, args, return_type=type_) inner = await cg.process_lambda(value, normalized_args, return_type=type_)
body_lines.append(f"call.{setter}(({inner})({fwd_args}));") body_lines.append(f"call.{setter}(({inner})({fwd_args}));")
else: else:
body_lines.append(f"call.{setter}({cg.safe_exp(value)});") body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
@@ -216,7 +225,7 @@ async def light_control_to_code(config, action_id, template_arg, args):
if CONF_EFFECT in config: if CONF_EFFECT in config:
if isinstance(config[CONF_EFFECT], Lambda): if isinstance(config[CONF_EFFECT], Lambda):
inner_lambda = await cg.process_lambda( inner_lambda = await cg.process_lambda(
config[CONF_EFFECT], args, return_type=cg.std_string config[CONF_EFFECT], normalized_args, return_type=cg.std_string
) )
body_lines.append( body_lines.append(
f"{{ auto __effect_s = ({inner_lambda})({fwd_args});\n" f"{{ auto __effect_s = ({inner_lambda})({fwd_args});\n"
@@ -230,13 +239,10 @@ async def light_control_to_code(config, action_id, template_arg, args):
f"call.set_effect(static_cast<uint32_t>({_resolve_effect_index(config)}));" f"call.set_effect(static_cast<uint32_t>({_resolve_effect_index(config)}));"
) )
# Match LightControlAction::ApplyFn signature: forward trigger args as Ts...
# so the apply lambda's parameter types match both ApplyFn and the
# inner field lambdas (which are generated from `args` directly).
apply_args = [ apply_args = [
(LightState.operator("ptr"), "parent"), (LightState.operator("ptr"), "parent"),
(LightCall.operator("ref"), "call"), (LightCall.operator("ref"), "call"),
*args, *normalized_args,
] ]
apply_lambda = LambdaExpression( apply_lambda = LambdaExpression(
["\n".join(body_lines)], ["\n".join(body_lines)],