TTTT Analysis  0.1
container.hpp
1 #ifndef container_hpp
2 #define container_hpp
3 #include <typeindex>
4 #include <vector>
5 #include <map>
6 
7 #include "value.hpp"
8 #include "filter.hpp"
9 
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 
19  if (_map[index] == ""){
20  CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
21  }
22  return _map[index];
23 }
24 }
25 
26 namespace fv{
27 
28 enum SaveOption{
29  PNG = 0,
30  PDF = 1,
31  ROOT = 2
32 };
33 
35  private:
36  std::string name;
37  std::string desc;
38  std::vector<Filter*> filters;
39  protected:
40  virtual void _fill() = 0;
41  public:
42  GenContainer(const std::string name, const std::string& desc)
43  :name(name),desc(desc) { }
44 
45  GenContainer(const std::string name)
46  :GenContainer(name,"N/A"){ }
47 
48  void add_filter(GenValue* filter){
49  filters.push_back(dynamic_cast<Filter*>(filter));
50  }
51 
52  void fill(){
53  for (auto filter : filters){
54  if (!filter->get_value()) return;
55  }
56  _fill();
57  }
58 
59  void set_description(const std::string& description){
60  desc = description;
61  }
62  const std::string& get_name(){
63  return name;
64  }
65  virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
66  virtual void save(const SaveOption& option=SaveOption::PNG) {
67  save_as(get_name(), option);
68  }
69 };
70 typedef std::map<std::string, GenContainer*> ContainerSet;
71 
72 template <typename H>
73 class Container : public GenContainer{
74  protected:
75  H* container;
76  public:
77  Container(const std::string& name, H* container)
78  :GenContainer(name),
79  container(container){ }
80  virtual H* get_container(){
81  return container;
82  }
83 
84 };
85 
86 template <typename T>
87 class ContainerVector : public Container<std::vector<T> >{
88  private:
89  Value<T>* value;
90 
91  void _fill(){
92  this->container->push_back(value->get_value());
93  }
94  public:
95  ContainerVector(const std::string& name, std::vector<T> *container, Value<T>* value)
96  :Container<std::vector<T> >(name, container),
97  value(value){ }
98  ContainerVector(const std::string& name, Value<T>* value)
99  :Container<std::vector<T> >(name, nullptr),
100  value(value){
101  this->container = new std::vector<T>();
102  }
103  void save_as(const std::string& fname) {
104  WARNING("Saving of ContainerVector objects not supported");
105  }
106 };
107 
108 template <typename T>
109 class ContainerMean : public Container<T>{
110  private:
111  Value<T>* value;
112  int count;
113  T sum;
114 
115  void _fill(){
116  count++;
117  sum += value->get_value();
118  }
119  public:
120  ContainerMean(const std::string& name, Value<T>* value)
121  :Container<std::vector<T> >(name, nullptr),
122  value(value){
123  this->container = new T();
124  }
125 
126  T* get_container(){
127  *(this->container) = sum/count;
128  return (this->container);
129  }
130  void save_as(const std::string& fname) {
131  WARNING("Saving of ContainerMean objects not supported");
132  }
133 };
134 
135 }
136 #endif // container_hpp
Definition: value.hpp:213
Definition: container.hpp:87
Definition: argparse.hpp:39
Definition: container.hpp:109
The namespace containing all filval classes and functions.
Definition: api.hpp:6
A generic value.
Definition: value.hpp:338
Definition: container.hpp:34
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:73