mirror of
https://github.com/esphome/esphome.git
synced 2026-05-24 18:06:27 +08:00
[light] Forward trigger args as Ts... so inner-lambda calls type-check
This commit is contained in:
@@ -37,13 +37,16 @@ template<bool HasTransitionLength, typename... Ts> class ToggleAction : public A
|
||||
// Trigger args are forwarded to the apply function so user lambdas
|
||||
// (e.g. `brightness: !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 forwarded as `Ts...`. The previous `const Ts &...`
|
||||
// form caused codegen to emit `const T &` for each arg in the apply
|
||||
// lambda's parameter list, which is invalid C++ source text when T is
|
||||
// already a reference (e.g. `const std::string & &` for triggers that
|
||||
// pass `std::string &`). Forwarding `Ts...` lets the codegen reuse the
|
||||
// trigger's `args` types unchanged for both the apply lambda and any
|
||||
// inner field lambdas, so they always type-match.
|
||||
template<typename... Ts> class LightControlAction : public Action<Ts...> {
|
||||
public:
|
||||
using ApplyFn = void (*)(LightState *, LightCall &, const std::remove_reference_t<Ts> &...);
|
||||
using ApplyFn = void (*)(LightState *, LightCall &, Ts...);
|
||||
LightControlAction(LightState *parent, ApplyFn apply) : parent_(parent), apply_(apply) {}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
|
||||
@@ -230,16 +230,13 @@ async def light_control_to_code(config, action_id, template_arg, args):
|
||||
f"call.set_effect(static_cast<uint32_t>({_resolve_effect_index(config)}));"
|
||||
)
|
||||
|
||||
# Match LightControlAction::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 & &`.
|
||||
# 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 = [
|
||||
(LightState.operator("ptr"), "parent"),
|
||||
(LightCall.operator("ref"), "call"),
|
||||
*(
|
||||
(cg.RawExpression(f"const std::remove_reference_t<{cg.safe_exp(t)}> &"), n)
|
||||
for t, n in args
|
||||
),
|
||||
*args,
|
||||
]
|
||||
apply_lambda = LambdaExpression(
|
||||
["\n".join(body_lines)],
|
||||
|
||||
Reference in New Issue
Block a user