filter.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * A Filter is a special type of derived value that can only return a boolean.
  33. * Container objects have a vector of filters that control if a "fill" call
  34. * actually places data into the container or not. This file contains a variety
  35. * of generic filters to aide in analysis.
  36. */
  37. #ifndef filter_h
  38. #define filter_h
  39. #include <iostream>
  40. #include <functional>
  41. #include "value.hpp"
  42. namespace fv {
  43. class Filter : public DerivedValue<bool>{
  44. private:
  45. Function<bool()>& filter_function;
  46. void update_value(){
  47. value = filter_function();
  48. }
  49. public:
  50. Filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
  51. :DerivedValue<bool>(name),
  52. filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
  53. /** Return a new filter that is the conjuction of the two source
  54. * filters.
  55. */
  56. Filter* operator*(Filter *f){
  57. auto new_name = this->get_name() + "&&" + f->get_name();
  58. return new Filter(new_name, [this, f](){return this->get_value() && f->get_value();});
  59. }
  60. /** Return a new filter that is the disjunction of the two source
  61. * filters.
  62. */
  63. Filter* operator+(Filter *f){
  64. auto new_name = this->get_name() + "||" + f->get_name();
  65. return new Filter(new_name, [this, f](){return this->get_value() || f->get_value();});
  66. }
  67. /** Return a new filter that is the negation of the source filter.
  68. */
  69. Filter* operator!(){
  70. auto new_name = std::string("!(") + this->get_name() + std::string(")");
  71. return new Filter(new_name, [this](){return !this->get_value();});
  72. }
  73. };
  74. template <typename T>
  75. class RangeFilter : public Filter{
  76. private:
  77. public:
  78. RangeFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
  79. Filter(name, [test_value, range_low, range_high]{
  80. T val = test_value->get_value();
  81. return (val >= range_low) && (val < range_high);
  82. }) { }
  83. };
  84. }
  85. #endif // filter_h