container.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef container_hpp
  2. #define container_hpp
  3. #include "value.hpp"
  4. #include "filter.hpp"
  5. #include <vector>
  6. namespace filval{
  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. };
  35. typedef std::map<std::string, GenContainer*> ContainerSet;
  36. template <typename H>
  37. class Container : public GenContainer{
  38. protected:
  39. H* container;
  40. public:
  41. Container(const std::string& name, H* container)
  42. :GenContainer(name),
  43. container(container){ }
  44. virtual H* get_container(){
  45. return container;
  46. }
  47. };
  48. template <typename T>
  49. class ContainerVector : public Container<std::vector<T> >{
  50. private:
  51. Value<T>* value;
  52. void _fill(){
  53. this->container->push_back(value->get_value());
  54. }
  55. public:
  56. ContainerVector(const std::string& name, std::vector<T> *container, Value<T>* value)
  57. :Container<std::vector<T> >(name, container),
  58. value(value){ }
  59. ContainerVector(const std::string& name, Value<T>* value)
  60. :Container<std::vector<T> >(name, NULL),
  61. value(value){
  62. this->container = new std::vector<T>();
  63. }
  64. };
  65. template <typename T>
  66. class ContainerMean : public Container<T>{
  67. private:
  68. Value<T>* value;
  69. int count;
  70. T sum;
  71. void _fill(){
  72. count++;
  73. sum += value->get_value();
  74. }
  75. public:
  76. ContainerMean(const std::string& name, Value<T>* value)
  77. :Container<std::vector<T> >(name, NULL),
  78. value(value){
  79. this->container = new T();
  80. }
  81. T* get_container(){
  82. *(this->container) = sum/count;
  83. return (this->container);
  84. }
  85. };
  86. }
  87. #endif // container_hpp