filter.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 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. std::cout << std::string("!") << std::endl;
  31. std::cout << this << this->get_name() << std::endl;
  32. auto new_name = std::string("!(") + this->get_name() + std::string(")");
  33. std::cout << new_name << std::endl;
  34. return new Filter(new_name, [this](){return !this->get_value();});
  35. }
  36. };
  37. template <typename T>
  38. class RangeFilter : public Filter{
  39. private:
  40. public:
  41. RangeFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
  42. Filter(name, [test_value, range_low, range_high]{
  43. T val = test_value->get_value();
  44. return (val >= range_low) && (val < range_high);
  45. }){ }
  46. };
  47. }
  48. #endif // filter_h