container.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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) {
  41. save_as(get_name(), option);
  42. }
  43. };
  44. typedef std::map<std::string, GenContainer*> ContainerSet;
  45. template <typename H>
  46. class Container : public GenContainer{
  47. protected:
  48. H* container;
  49. public:
  50. Container(const std::string& name, H* container)
  51. :GenContainer(name),
  52. container(container){ }
  53. virtual H* get_container(){
  54. return container;
  55. }
  56. };
  57. template <typename T>
  58. class ContainerVector : public Container<std::vector<T> >{
  59. private:
  60. Value<T>* value;
  61. void _fill(){
  62. this->container->push_back(value->get_value());
  63. }
  64. public:
  65. ContainerVector(const std::string& name, std::vector<T> *container, Value<T>* value)
  66. :Container<std::vector<T> >(name, container),
  67. value(value){ }
  68. ContainerVector(const std::string& name, Value<T>* value)
  69. :Container<std::vector<T> >(name, nullptr),
  70. value(value){
  71. this->container = new std::vector<T>();
  72. }
  73. void save_as(const std::string& fname) { }
  74. virtual void save() { }
  75. };
  76. template <typename T>
  77. class ContainerMean : public Container<T>{
  78. private:
  79. Value<T>* value;
  80. int count;
  81. T sum;
  82. void _fill(){
  83. count++;
  84. sum += value->get_value();
  85. }
  86. public:
  87. ContainerMean(const std::string& name, Value<T>* value)
  88. :Container<std::vector<T> >(name, nullptr),
  89. value(value){
  90. this->container = new T();
  91. }
  92. T* get_container(){
  93. *(this->container) = sum/count;
  94. return (this->container);
  95. }
  96. void save_as(const std::string& fname) { }
  97. virtual void save() { }
  98. };
  99. }
  100. #endif // container_hpp