filter.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 filval {
  11. class GenFilter : public DerivedValue<bool>{};
  12. class Filter : public GenFilter{
  13. private:
  14. std::function<bool()> filter_function;
  15. void update_value(){
  16. value = filter_function();
  17. }
  18. public:
  19. Filter(std::function<bool()> filter_function)
  20. :filter_function(filter_function){ }
  21. Filter* operator*(Filter *f){
  22. return new Filter([this, f](){return this->get_value() && f->get_value();});
  23. }
  24. Filter* operator+(Filter *f){
  25. return new Filter([this, f](){return this->get_value() || f->get_value();});
  26. }
  27. Filter* operator!(){
  28. return new Filter([this](){return !this->get_value();});
  29. }
  30. };
  31. template <typename T>
  32. class RangeFilter : public Filter{
  33. private:
  34. public:
  35. RangeFilter(Value<T>* test_value, T range_low, T range_high):
  36. Filter([test_value, range_low, range_high]{
  37. T val = test_value->get_value();
  38. return (val >= range_low) && (val < range_high);
  39. }){ }
  40. };
  41. }
  42. #endif // filter_h