mirror of
https://github.com/esphome/esphome.git
synced 2026-06-04 01:18:26 +08:00
[valve] Forward trigger args as Ts... so inner-lambda calls type-check
This commit is contained in:
@@ -251,15 +251,12 @@ async def valve_control_to_code(config, action_id, template_arg, args):
|
|||||||
else:
|
else:
|
||||||
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
||||||
|
|
||||||
# Match ControlAction::ApplyFn signature: `const std::remove_reference_t<T> &`
|
# Match ControlAction::ApplyFn signature: forward trigger args as Ts...
|
||||||
# for each trigger arg so non-reference Ts stay no-copy (`const T &`) and
|
# so the apply lambda's parameter types match both ApplyFn and the
|
||||||
# reference Ts collapse correctly without producing `const T & &`.
|
# inner field lambdas (which are generated from `args` directly).
|
||||||
apply_args = [
|
apply_args = [
|
||||||
(ValveCall.operator("ref"), "call"),
|
(ValveCall.operator("ref"), "call"),
|
||||||
*(
|
*args,
|
||||||
(cg.RawExpression(f"const std::remove_reference_t<{cg.safe_exp(t)}> &"), n)
|
|
||||||
for t, n in args
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
apply_lambda = LambdaExpression(
|
apply_lambda = LambdaExpression(
|
||||||
["\n".join(body_lines)],
|
["\n".join(body_lines)],
|
||||||
|
|||||||
@@ -53,13 +53,16 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
|||||||
// Trigger args are forwarded to the apply function so user lambdas
|
// Trigger args are forwarded to the apply function so user lambdas
|
||||||
// (e.g. `position: !lambda "return x;"`) keep working.
|
// (e.g. `position: !lambda "return x;"`) keep working.
|
||||||
//
|
//
|
||||||
// Trigger args are forwarded as `const std::remove_reference_t<Ts> &...`
|
// Trigger args are forwarded as `Ts...`. The previous `const Ts &...`
|
||||||
// (instead of `const Ts &...`) so codegen can emit the same form in the
|
// form caused codegen to emit `const T &` for each arg in the apply
|
||||||
// apply lambda's parameter list without producing `const T & &` for
|
// lambda's parameter list, which is invalid C++ source text when T is
|
||||||
// triggers whose Ts already carries a reference (e.g. `std::string &`).
|
// 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 ControlAction : public Action<Ts...> {
|
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
using ApplyFn = void (*)(ValveCall &, const std::remove_reference_t<Ts> &...);
|
using ApplyFn = void (*)(ValveCall &, Ts...);
|
||||||
ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {}
|
ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {}
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
|
|||||||
Reference in New Issue
Block a user