container.hpp 2.9 KB

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