Skip to content

Commit e97a6fe

Browse files
committed
Implement a filter transform that emits its input conditionally
1 parent f675988 commit e97a6fe

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/sensesp/transforms/filter.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef SENSESP_SRC_TRANSFORMS_FILTER_H_
2+
#define SENSESP_SRC_TRANSFORMS_FILTER_H_
3+
4+
#include <functional>
5+
6+
namespace sensesp {
7+
8+
/**
9+
* @brief Transform that only emits the output if the filter condition returns
10+
* true.
11+
*
12+
*/
13+
template <typename T>
14+
class Filter : public Transform<T, T> {
15+
public:
16+
Filter(std::function<bool(const T&)> filter, String config_path = "")
17+
: Transform<T, T>(config_path), filter_{filter} {
18+
this->load_configuration();
19+
}
20+
virtual void set(const T& new_value) override {
21+
if (filter_(new_value)) {
22+
this->emit(new_value);
23+
}
24+
}
25+
26+
private:
27+
std::function<bool(const T&)> filter_;
28+
29+
};
30+
31+
32+
} // namespace sensesp
33+
34+
#endif // SENSESP_SRC_TRANSFORMS_FILTER_H_

0 commit comments

Comments
 (0)