container.hpp 2.8 KB

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