container.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef container_hpp
  2. #define container_hpp
  3. #include "value.hpp"
  4. #include "filter.hpp"
  5. #include <vector>
  6. namespace fv{
  7. class GenContainer{
  8. private:
  9. std::string name;
  10. std::string desc;
  11. std::vector<Filter*> filters;
  12. protected:
  13. virtual void _fill() = 0;
  14. public:
  15. GenContainer(const std::string name, const std::string& desc)
  16. :name(name),desc(desc) { }
  17. GenContainer(const std::string name)
  18. :GenContainer(name,"N/A"){ }
  19. void add_filter(GenValue* filter){
  20. filters.push_back(dynamic_cast<Filter*>(filter));
  21. }
  22. void fill(){
  23. for (auto filter : filters){
  24. if (!filter->get_value()) return;
  25. }
  26. _fill();
  27. }
  28. void set_description(const std::string& description){
  29. desc = description;
  30. }
  31. const std::string& get_name(){
  32. return name;
  33. }
  34. virtual void save_as(const std::string& fname) = 0;
  35. virtual void save() = 0;
  36. };
  37. typedef std::map<std::string, GenContainer*> ContainerSet;
  38. template <typename H>
  39. class Container : public GenContainer{
  40. protected:
  41. H* container;
  42. public:
  43. Container(const std::string& name, H* container)
  44. :GenContainer(name),
  45. container(container){ }
  46. virtual H* get_container(){
  47. return container;
  48. }
  49. };
  50. template <typename T>
  51. class ContainerVector : public Container<std::vector<T> >{
  52. private:
  53. Value<T>* value;
  54. void _fill(){
  55. this->container->push_back(value->get_value());
  56. }
  57. public:
  58. ContainerVector(const std::string& name, std::vector<T> *container, Value<T>* value)
  59. :Container<std::vector<T> >(name, container),
  60. value(value){ }
  61. ContainerVector(const std::string& name, Value<T>* value)
  62. :Container<std::vector<T> >(name, nullptr),
  63. value(value){
  64. this->container = new std::vector<T>();
  65. }
  66. void save_as(const std::string& fname) { }
  67. virtual void save() { }
  68. };
  69. template <typename T>
  70. class ContainerMean : public Container<T>{
  71. private:
  72. Value<T>* value;
  73. int count;
  74. T sum;
  75. void _fill(){
  76. count++;
  77. sum += value->get_value();
  78. }
  79. public:
  80. ContainerMean(const std::string& name, Value<T>* value)
  81. :Container<std::vector<T> >(name, nullptr),
  82. value(value){
  83. this->container = new T();
  84. }
  85. T* get_container(){
  86. *(this->container) = sum/count;
  87. return (this->container);
  88. }
  89. void save_as(const std::string& fname) { }
  90. virtual void save() { }
  91. };
  92. }
  93. #endif // container_hpp