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

This commit is contained in:
J. Nick Koston
2026-05-03 17:30:01 -05:00
parent 1509db2a2c
commit e4de502bd4
2 changed files with 18 additions and 13 deletions
+11 -8
View File
@@ -358,26 +358,29 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args):
(CONF_DIRECTION, "set_direction", FanDirection),
)
# 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 TurnOnAction::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)
body_lines: list[str] = []
for conf_key, setter, type_ in FIELDS:
if (value := config.get(conf_key)) is None:
continue
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}));")
else:
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
# Match TurnOnAction::ApplyFn signature: `const std::remove_reference_t<T> &`
# for each trigger arg so non-reference Ts stay no-copy (`const T &`) and
# reference Ts collapse correctly without producing `const T & &`.
apply_args = [
(FanCall.operator("ref"), "call"),
*(
(cg.RawExpression(f"const std::remove_reference_t<{cg.safe_exp(t)}> &"), n)
for t, n in args
),
*normalized_args,
]
apply_lambda = LambdaExpression(
["\n".join(body_lines)],
+7 -5
View File
@@ -13,13 +13,15 @@ namespace fan {
// Trigger args are forwarded to the apply function so user lambdas
// (e.g. `speed: !lambda "return x;"`) keep working.
//
// Trigger args are forwarded as `const std::remove_reference_t<Ts> &...`
// (instead of `const Ts &...`) so codegen can emit the same form in the
// apply lambda's parameter list without producing `const T & &` for
// triggers whose Ts already carries a reference (e.g. `std::string &`).
// Trigger args are normalized to `const std::remove_cvref_t<Ts> &...` so
// the codegen can emit a matching parameter list for both the apply lambda
// and any inner field lambdas without producing invalid C++ source text
// (e.g. `const T & &` if Ts already carries a reference, or `const const
// T &` if Ts already carries a const). This keeps trigger args no-copy
// regardless of whether the trigger supplies `T`, `T &`, or `const T &`.
template<typename... Ts> class TurnOnAction : public Action<Ts...> {
public:
using ApplyFn = void (*)(FanCall &, const std::remove_reference_t<Ts> &...);
using ApplyFn = void (*)(FanCall &, const std::remove_cvref_t<Ts> &...);
TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {}
void play(const Ts &...x) override {