Use a regular class member instead of a function static variable to store the state
@@ -33,23 +33,24 @@ class Hysteresis : public LambdaTransform<IN, OUT, IN, IN, OUT, OUT> {
Hysteresis(IN lower_threshold, IN upper_threshold, OUT low_output,
OUT high_output, String config_path = "")
: LambdaTransform<IN, OUT, IN, IN, OUT, OUT>(
- &(Hysteresis::function), lower_threshold, upper_threshold,
- low_output, high_output, hysteresis_param_info, config_path) {}
-
- private:
- // Function implementing the actual hysteresis logic
- static OUT function(IN input, IN lower_threshold, IN upper_threshold,
+ // the lambda function needs to be defined in this awkward
+ // location because it needs to be able to capture `this`
+ [this](IN input, IN lower_threshold, IN upper_threshold,
OUT low_output, OUT high_output) {
- static OUT last_value = low_output;
if (input < lower_threshold) {
- last_value = low_output;
+ this->last_value_ = low_output;
} else if (upper_threshold <= input) {
- last_value = high_output;
+ this->last_value_ = high_output;
}
- // If neither of the above conditions were met, input is between the
- // lower and upper thresholds (in the hysteresis region)
- return last_value;
- };
+ // If neither of the above conditions were met, input is between
+ // the lower and upper thresholds (in the hysteresis region)
+ return this->last_value_;
+ },
+ lower_threshold, upper_threshold, low_output, high_output,
+ hysteresis_param_info, config_path) {}
+
+ private:
+ OUT last_value_;
};
#endif
