filter.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef filter_h
  2. #define filter_h
  3. #include <iostream>
  4. #include <functional>
  5. #include "value.hpp"
  6. /* A Filter is a special type of derived value that can only return a boolean.
  7. * Container objects have a vector of filters that control if a "fill" call
  8. * actually places data into the container or not.
  9. */
  10. namespace fv {
  11. class Filter : public DerivedValue<bool>{
  12. private:
  13. std::function<bool()> filter_function;
  14. void update_value(){
  15. value = filter_function();
  16. }
  17. public:
  18. Filter(const std::string& name, std::function<bool()> filter_function)
  19. :DerivedValue<bool>(name),
  20. filter_function(filter_function){ }
  21. Filter* operator*(Filter *f){
  22. auto new_name = this->get_name() + "&&" + f->get_name();
  23. return new Filter(new_name, [this, f](){return this->get_value() && f->get_value();});
  24. }
  25. Filter* operator+(Filter *f){
  26. auto new_name = this->get_name() + "||" + f->get_name();
  27. return new Filter(new_name, [this, f](){return this->get_value() || f->get_value();});
  28. }
  29. Filter* operator!(){
  30. auto new_name = std::string("!(") + this->get_name() + std::string(")");
  31. return new Filter(new_name, [this](){return !this->get_value();});
  32. }
  33. };
  34. template <typename T>
  35. class RangeFilter : public Filter{
  36. private:
  37. public:
  38. RangeFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
  39. Filter(name, [test_value, range_low, range_high]{
  40. T val = test_value->get_value();
  41. return (val >= range_low) && (val < range_high);
  42. }){ }
  43. };
  44. }
  45. #endif // filter_h