mirror of
https://github.com/esphome/esphome.git
synced 2026-09-13 16:18:41 +00:00
[climate] Use const remove_reference_t<T> & to avoid copies of non-ref Ts
This commit is contained in:
@@ -526,11 +526,15 @@ async def climate_control_to_code(config, action_id, template_arg, args):
|
||||
else:
|
||||
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
||||
|
||||
# Match ControlAction::ApplyFn signature: trigger args forwarded
|
||||
# by-value as Ts...
|
||||
# Match ControlAction::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 = [
|
||||
(ClimateCall.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(
|
||||
["\n".join(body_lines)],
|
||||
|
||||
@@ -11,11 +11,13 @@ namespace esphome::climate {
|
||||
// Trigger args are forwarded to the apply function so user lambdas
|
||||
// (e.g. `target_temperature: !lambda "return x;"`) keep working.
|
||||
//
|
||||
// Ts... must be forwarded by-value: `const T &` is ill-formed when T is
|
||||
// already a reference type (some triggers pass `std::string &`).
|
||||
// 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 &`).
|
||||
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||
public:
|
||||
using ApplyFn = void (*)(ClimateCall &, Ts...);
|
||||
using ApplyFn = void (*)(ClimateCall &, const std::remove_reference_t<Ts> &...);
|
||||
ControlAction(Climate *climate, ApplyFn apply) : climate_(climate), apply_(apply) {}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
|
||||
Reference in New Issue
Block a user