1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00

Merge branch 'stateless_lambdas' into integration

This commit is contained in:
J. Nick Koston
2025-10-26 01:31:14 -07:00
5 changed files with 28 additions and 17 deletions

View File

@@ -114,18 +114,16 @@ class LambdaFilter : public Filter {
/** Optimized lambda filter for stateless lambdas (no capture).
*
* Uses function pointer instead of std::function to reduce memory overhead.
* Memory: 8 bytes (function pointer) vs 32 bytes (std::function).
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
*/
class StatelessLambdaFilter : public Filter {
public:
using stateless_lambda_filter_t = optional<bool> (*)(bool);
explicit StatelessLambdaFilter(stateless_lambda_filter_t f) : f_(f) {}
explicit StatelessLambdaFilter(optional<bool> (*f)(bool)) : f_(f) {}
optional<bool> new_value(bool value) override { return this->f_(value); }
protected:
stateless_lambda_filter_t f_;
optional<bool> (*f_)(bool);
};
class SettleFilter : public Filter, public Component {

View File

@@ -299,18 +299,16 @@ class LambdaFilter : public Filter {
/** Optimized lambda filter for stateless lambdas (no capture).
*
* Uses function pointer instead of std::function to reduce memory overhead.
* Memory: 8 bytes (function pointer) vs 32 bytes (std::function).
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
*/
class StatelessLambdaFilter : public Filter {
public:
using stateless_lambda_filter_t = optional<float> (*)(float);
explicit StatelessLambdaFilter(stateless_lambda_filter_t lambda_filter) : lambda_filter_(lambda_filter) {}
explicit StatelessLambdaFilter(optional<float> (*lambda_filter)(float)) : lambda_filter_(lambda_filter) {}
optional<float> new_value(float value) override { return this->lambda_filter_(value); }
protected:
stateless_lambda_filter_t lambda_filter_;
optional<float> (*lambda_filter_)(float);
};
/// A simple filter that adds `offset` to each value it receives.

View File

@@ -65,18 +65,16 @@ class LambdaFilter : public Filter {
/** Optimized lambda filter for stateless lambdas (no capture).
*
* Uses function pointer instead of std::function to reduce memory overhead.
* Memory: 8 bytes (function pointer) vs 32 bytes (std::function).
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
*/
class StatelessLambdaFilter : public Filter {
public:
using stateless_lambda_filter_t = optional<std::string> (*)(std::string);
explicit StatelessLambdaFilter(stateless_lambda_filter_t lambda_filter) : lambda_filter_(lambda_filter) {}
explicit StatelessLambdaFilter(optional<std::string> (*lambda_filter)(std::string)) : lambda_filter_(lambda_filter) {}
optional<std::string> new_value(std::string value) override { return this->lambda_filter_(value); }
protected:
stateless_lambda_filter_t lambda_filter_;
optional<std::string> (*lambda_filter_)(std::string);
};
/// A simple filter that converts all text to uppercase

View File

@@ -81,7 +81,7 @@ template<typename... Ts> class LambdaCondition : public Condition<Ts...> {
/// Optimized lambda condition for stateless lambdas (no capture).
/// Uses function pointer instead of std::function to reduce memory overhead.
/// Memory: 8 bytes (function pointer) vs 32 bytes (std::function).
/// Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
template<typename... Ts> class StatelessLambdaCondition : public Condition<Ts...> {
public:
explicit StatelessLambdaCondition(bool (*f)(Ts...)) : f_(f) {}
@@ -204,7 +204,7 @@ template<typename... Ts> class LambdaAction : public Action<Ts...> {
/// Optimized lambda action for stateless lambdas (no capture).
/// Uses function pointer instead of std::function to reduce memory overhead.
/// Memory: 8 bytes (function pointer) vs 32 bytes (std::function).
/// Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function).
template<typename... Ts> class StatelessLambdaAction : public Action<Ts...> {
public:
explicit StatelessLambdaAction(void (*f)(Ts...)) : f_(f) {}

View File

@@ -213,6 +213,23 @@ class TestLambdaExpression:
"+[](int32_t foo, float bar) -> float {\n return foo + bar;\n}"
)
def test_str__with_capture_no_prefix(self):
"""Test lambda with capture (not stateless) does NOT get + prefix"""
target = cg.LambdaExpression(
("return captured_var + x;",),
((int, "x"),),
"captured_var", # Has capture (not stateless)
int,
)
actual = str(target)
# Should NOT have + prefix
assert actual == (
"[captured_var](int32_t x) -> int32_t {\n return captured_var + x;\n}"
)
assert not actual.startswith("+")
class TestLiterals:
@pytest.mark.parametrize(