filter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef filter_h
  2. #define filter_h
  3. #include <iostream>
  4. #include "value.hpp"
  5. /* A Filter is a special type of derived value that can only return a boolean.
  6. * Container objects have a vector of filters that control if a "fill" call
  7. * actually places data into the container or not.
  8. */
  9. namespace filval {
  10. class Filter : public DerivedValue<bool>{ };
  11. class FilterAnd : public Filter {
  12. protected:
  13. Filter *filterA;
  14. Filter *filterB;
  15. void update_value(){
  16. value = filterA->get_value() && filterB->get_value();
  17. }
  18. public:
  19. FilterAnd(Filter *filterA, Filter *filterB)
  20. :filterA(filterA), filterB(filterB){ }
  21. };
  22. class FilterOr : public Filter {
  23. private:
  24. Filter *filterA;
  25. Filter *filterB;
  26. void update_value(){
  27. value = filterA->get_value() || filterB->get_value();
  28. }
  29. public:
  30. FilterOr(Filter *filterA, Filter *filterB)
  31. :filterA(filterA), filterB(filterB){ }
  32. };
  33. class FilterInv : public Filter {
  34. private:
  35. Filter *filter;
  36. void update_value(){
  37. value = !filter->get_value();
  38. }
  39. public:
  40. FilterInv(Filter *filter)
  41. :filter(filter){ }
  42. };
  43. template <typename T>
  44. class FilterGreaterThan : public Filter {
  45. private:
  46. Value<T> *filter_value;
  47. Value<T> *range_low;
  48. void update_value(){
  49. value = filter_value->get_value() > range_low->get_value();
  50. }
  51. public:
  52. FilterGreaterThan(GenValue* filter_value, GenValue* range_low)
  53. :filter_value(dynamic_cast<Value<T>*>(filter_value)),
  54. range_low(dynamic_cast<Value<T>*>(range_low)) { }
  55. FilterGreaterThan(GenValue* filter_value, T range_low)
  56. :filter_value(dynamic_cast<Value<T>*>(filter_value)){
  57. this->range_low = new ConstantValue<T>(range_low);
  58. }
  59. };
  60. template <typename T>
  61. class FilterLessThan : public Filter {
  62. private:
  63. Value<T> *filter_value;
  64. Value<T> *range_high;
  65. void update_value(){
  66. value = filter_value->get_value() < range_high->get_value();
  67. }
  68. public:
  69. FilterLessThan(GenValue* filter_value, GenValue* range_high)
  70. :filter_value(dynamic_cast<Value<T>*>(filter_value)),
  71. range_high(dynamic_cast<Value<T>*>(range_high)) { }
  72. FilterLessThan(GenValue* filter_value, T range_high)
  73. :filter_value(dynamic_cast<Value<T>*>(filter_value)){
  74. this->range_high = new ConstantValue<T>(range_high);
  75. }
  76. };
  77. template <typename T>
  78. class RangeFilter : public FilterAnd {
  79. public:
  80. RangeFilter(Value<T> *filter_value, Value<T> *range_low, Value<T> *range_high){
  81. this->filterA = new FilterLessThan<T>(filter_value, range_high);
  82. this->filterB = new FilterGreaterThan<T>(filter_value, range_low);
  83. }
  84. RangeFilter(Value<T> *filter_value, T range_low, T range_high){
  85. this->filterA = new FilterLessThan<T>(filter_value, range_high);
  86. this->filterB = new FilterGreaterThan<T>(filter_value, range_low);
  87. }
  88. };
  89. }
  90. #endif // filter_h