mirror of
https://github.com/esphome/esphome.git
synced 2025-11-01 15:41:52 +00:00
Merge branch 'stateless_lambdas' into integration
This commit is contained in:
@@ -114,18 +114,16 @@ class LambdaFilter : public Filter {
|
|||||||
/** Optimized lambda filter for stateless lambdas (no capture).
|
/** Optimized lambda filter for stateless lambdas (no capture).
|
||||||
*
|
*
|
||||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
* 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 {
|
class StatelessLambdaFilter : public Filter {
|
||||||
public:
|
public:
|
||||||
using stateless_lambda_filter_t = optional<bool> (*)(bool);
|
explicit StatelessLambdaFilter(optional<bool> (*f)(bool)) : f_(f) {}
|
||||||
|
|
||||||
explicit StatelessLambdaFilter(stateless_lambda_filter_t f) : f_(f) {}
|
|
||||||
|
|
||||||
optional<bool> new_value(bool value) override { return this->f_(value); }
|
optional<bool> new_value(bool value) override { return this->f_(value); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
stateless_lambda_filter_t f_;
|
optional<bool> (*f_)(bool);
|
||||||
};
|
};
|
||||||
|
|
||||||
class SettleFilter : public Filter, public Component {
|
class SettleFilter : public Filter, public Component {
|
||||||
|
|||||||
@@ -299,18 +299,16 @@ class LambdaFilter : public Filter {
|
|||||||
/** Optimized lambda filter for stateless lambdas (no capture).
|
/** Optimized lambda filter for stateless lambdas (no capture).
|
||||||
*
|
*
|
||||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
* 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 {
|
class StatelessLambdaFilter : public Filter {
|
||||||
public:
|
public:
|
||||||
using stateless_lambda_filter_t = optional<float> (*)(float);
|
explicit StatelessLambdaFilter(optional<float> (*lambda_filter)(float)) : lambda_filter_(lambda_filter) {}
|
||||||
|
|
||||||
explicit StatelessLambdaFilter(stateless_lambda_filter_t lambda_filter) : lambda_filter_(lambda_filter) {}
|
|
||||||
|
|
||||||
optional<float> new_value(float value) override { return this->lambda_filter_(value); }
|
optional<float> new_value(float value) override { return this->lambda_filter_(value); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
stateless_lambda_filter_t lambda_filter_;
|
optional<float> (*lambda_filter_)(float);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A simple filter that adds `offset` to each value it receives.
|
/// A simple filter that adds `offset` to each value it receives.
|
||||||
|
|||||||
@@ -65,18 +65,16 @@ class LambdaFilter : public Filter {
|
|||||||
/** Optimized lambda filter for stateless lambdas (no capture).
|
/** Optimized lambda filter for stateless lambdas (no capture).
|
||||||
*
|
*
|
||||||
* Uses function pointer instead of std::function to reduce memory overhead.
|
* 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 {
|
class StatelessLambdaFilter : public Filter {
|
||||||
public:
|
public:
|
||||||
using stateless_lambda_filter_t = optional<std::string> (*)(std::string);
|
explicit StatelessLambdaFilter(optional<std::string> (*lambda_filter)(std::string)) : lambda_filter_(lambda_filter) {}
|
||||||
|
|
||||||
explicit StatelessLambdaFilter(stateless_lambda_filter_t lambda_filter) : lambda_filter_(lambda_filter) {}
|
|
||||||
|
|
||||||
optional<std::string> new_value(std::string value) override { return this->lambda_filter_(value); }
|
optional<std::string> new_value(std::string value) override { return this->lambda_filter_(value); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
stateless_lambda_filter_t lambda_filter_;
|
optional<std::string> (*lambda_filter_)(std::string);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A simple filter that converts all text to uppercase
|
/// A simple filter that converts all text to uppercase
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ template<typename... Ts> class LambdaCondition : public Condition<Ts...> {
|
|||||||
|
|
||||||
/// Optimized lambda condition for stateless lambdas (no capture).
|
/// Optimized lambda condition for stateless lambdas (no capture).
|
||||||
/// Uses function pointer instead of std::function to reduce memory overhead.
|
/// 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...> {
|
template<typename... Ts> class StatelessLambdaCondition : public Condition<Ts...> {
|
||||||
public:
|
public:
|
||||||
explicit StatelessLambdaCondition(bool (*f)(Ts...)) : f_(f) {}
|
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).
|
/// Optimized lambda action for stateless lambdas (no capture).
|
||||||
/// Uses function pointer instead of std::function to reduce memory overhead.
|
/// 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...> {
|
template<typename... Ts> class StatelessLambdaAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
explicit StatelessLambdaAction(void (*f)(Ts...)) : f_(f) {}
|
explicit StatelessLambdaAction(void (*f)(Ts...)) : f_(f) {}
|
||||||
|
|||||||
@@ -213,6 +213,23 @@ class TestLambdaExpression:
|
|||||||
"+[](int32_t foo, float bar) -> float {\n return foo + bar;\n}"
|
"+[](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:
|
class TestLiterals:
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
Reference in New Issue
Block a user