filter.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ObsFilter : public DerivedValue<bool>{
  44. private:
  45. Function<bool()>& filter_function;
  46. void update_value(){
  47. value = filter_function();
  48. }
  49. void verify_integrity(){ };
  50. public:
  51. ObsFilter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
  52. :DerivedValue<bool>(name),
  53. filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
  54. /** Return a new filter that is the conjuction of the two source
  55. * filters.
  56. */
  57. ObsFilter* operator*(ObsFilter *f){
  58. auto new_name = this->get_name() + "&&" + f->get_name();
  59. return new ObsFilter(new_name, [this, f](){return this->get_value() && f->get_value();});
  60. }
  61. /** Return a new filter that is the disjunction of the two source
  62. * filters.
  63. */
  64. ObsFilter* operator+(ObsFilter *f){
  65. auto new_name = this->get_name() + "||" + f->get_name();
  66. return new ObsFilter(new_name, [this, f](){return this->get_value() || f->get_value();});
  67. }
  68. /** Return a new filter that is the negation of the source filter.
  69. */
  70. ObsFilter* operator!(){
  71. auto new_name = std::string("!(") + this->get_name() + std::string(")");
  72. return new ObsFilter(new_name, [this](){return !this->get_value();});
  73. }
  74. };
  75. template <typename T>
  76. class RangeObsFilter : public ObsFilter{
  77. private:
  78. public:
  79. RangeObsFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
  80. ObsFilter(name, [test_value, range_low, range_high]{
  81. T val = test_value->get_value();
  82. return (val >= range_low) && (val < range_high);
  83. }) { }
  84. };
  85. }
  86. #endif // filter_h