[light] Use const remove_reference_t<T> & to avoid copies of non-ref Ts

This commit is contained in:
J. Nick Koston
2026-05-03 16:54:21 -05:00
parent dd0cff5f45
commit 5483b27bbf
2 changed files with 12 additions and 12 deletions
+5 -6
View File
@@ -37,14 +37,13 @@ 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 forwarded as `const std::remove_reference_t<Ts> &...`
// form caused codegen to emit `const T &` for each arg in the apply // (instead of `const Ts &...`) so codegen can emit the same form in the
// lambda's parameter list, which is invalid C++ source text when T is // apply lambda's parameter list without producing `const T & &` for
// already a reference (e.g. `const std::string & &` for triggers that // triggers whose Ts already carries a reference (e.g. `std::string &`).
// pass `std::string &`).
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_reference_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 {
+7 -6
View File
@@ -230,15 +230,16 @@ 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)}));"
) )
# Forward trigger args as Ts... so each (type, name) pair passes through # Match LightControlAction::ApplyFn signature: `const std::remove_reference_t<T> &`
# unchanged. Wrapping in `const &` here would emit invalid C++ source # for each trigger arg so non-reference Ts stay no-copy (`const T &`) and
# for trigger types that already carry a reference (e.g. `std::string &`) # reference Ts collapse correctly without producing `const T & &`.
# and fails on plain Python types (`float`/`bool`) which have no
# `.operator()` method.
apply_args = [ apply_args = [
(LightState.operator("ptr"), "parent"), (LightState.operator("ptr"), "parent"),
(LightCall.operator("ref"), "call"), (LightCall.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)],