container.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef container_hpp
  2. #define container_hpp
  3. #include <typeindex>
  4. #include <vector>
  5. #include <map>
  6. #include "value.hpp"
  7. #include "filter.hpp"
  8. namespace fv::util{
  9. std::string& get_type_name(const std::type_index& index){
  10. std::map<std::type_index, std::string> _map;
  11. // Add to this list as needed :)
  12. _map[typeid(int)]="int";
  13. _map[typeid(unsigned int)]="unsigned int";
  14. _map[typeid(float)]="float";
  15. _map[typeid(double)]="double";
  16. if (_map[index] == ""){
  17. CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
  18. }
  19. return _map[index];
  20. }
  21. }
  22. namespace fv{
  23. enum SaveOption{
  24. PNG = 0,
  25. PDF = 1,
  26. ROOT = 2
  27. };
  28. class GenContainer{
  29. private:
  30. std::string name;
  31. std::string desc;
  32. std::vector<Filter*> filters;
  33. protected:
  34. virtual void _fill() = 0;
  35. public:
  36. GenContainer(const std::string name, const std::string& desc)
  37. :name(name),desc(desc) { }
  38. GenContainer(const std::string name)
  39. :GenContainer(name,"N/A"){ }
  40. void add_filter(GenValue* filter){
  41. filters.push_back(dynamic_cast<Filter*>(filter));
  42. }
  43. void fill(){
  44. for (auto filter : filters){
  45. if (!filter->get_value()) return;
  46. }
  47. _fill();
  48. }
  49. void set_description(const std::string& description){
  50. desc = description;
  51. }
  52. const std::string& get_name(){
  53. return name;
  54. }
  55. virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
  56. virtual void save(const SaveOption& option=SaveOption::PNG) {
  57. save_as(get_name(), option);
  58. }
  59. };
  60. typedef std::map<std::string, GenContainer*> ContainerSet;
  61. template <typename H>
  62. class Container : public GenContainer{
  63. protected:
  64. H* container;
  65. public:
  66. Container(const std::string& name, H* container)
  67. :GenContainer(name),
  68. container(container){ }
  69. virtual H* get_container(){
  70. return container;
  71. }
  72. };
  73. template <typename T>
  74. class ContainerVector : public Container<std::vector<T> >{
  75. private:
  76. Value<T>* value;
  77. void _fill(){
  78. this->container->push_back(value->get_value());
  79. }
  80. public:
  81. ContainerVector(const std::string& name, std::vector<T> *container, Value<T>* value)
  82. :Container<std::vector<T> >(name, container),
  83. value(value){ }
  84. ContainerVector(const std::string& name, Value<T>* value)
  85. :Container<std::vector<T> >(name, nullptr),
  86. value(value){
  87. this->container = new std::vector<T>();
  88. }
  89. void save_as(const std::string& fname) {
  90. WARNING("Saving of ContainerVector objects not supported");
  91. }
  92. };
  93. template <typename T>
  94. class ContainerMean : public Container<T>{
  95. private:
  96. Value<T>* value;
  97. int count;
  98. T sum;
  99. void _fill(){
  100. count++;
  101. sum += value->get_value();
  102. }
  103. public:
  104. ContainerMean(const std::string& name, Value<T>* value)
  105. :Container<std::vector<T> >(name, nullptr),
  106. value(value){
  107. this->container = new T();
  108. }
  109. T* get_container(){
  110. *(this->container) = sum/count;
  111. return (this->container);
  112. }
  113. void save_as(const std::string& fname) {
  114. WARNING("Saving of ContainerMean objects not supported");
  115. }
  116. };
  117. }
  118. #endif // container_hpp