123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #ifndef container_hpp
- #define container_hpp
- #include <typeindex>
- #include <vector>
- #include <map>
- #include "value.hpp"
- #include "filter.hpp"
- template class std::vector<std::vector<float> >;
- template class std::vector<std::vector<int> >;
- namespace fv::util{
- std::string get_type_name(const std::type_index& index){
- std::map<std::type_index, std::string> _map;
-
- _map[typeid(int)]="int";
- _map[typeid(unsigned int)]="unsigned int";
- _map[typeid(float)]="float";
- _map[typeid(double)]="double";
- _map[typeid(std::vector<int>)]="std::vector<int>";
- _map[typeid(std::vector<float>)]="std::vector<float>";
- _map[typeid(std::map<std::string,std::string>)] = "std::map<std::string,std::string>";
- if (_map[index] == ""){
- CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
- }
- return _map[index];
- }
- }
- namespace fv{
- enum SaveOption{
- PNG = 0,
- PDF = 1,
- ROOT = 2
- };
- class GenContainer{
- private:
- std::string name;
- std::string desc;
- std::vector<ObsFilter*> filters;
- protected:
- virtual void _fill() = 0;
- public:
- GenContainer(const std::string name, const std::string& desc)
- :name(name),desc(desc) { }
- GenContainer(const std::string name)
- :GenContainer(name,"N/A"){ }
- GenContainer* add_filter(GenValue* filter){
- filters.push_back(dynamic_cast<ObsFilter*>(filter));
- return this;
- }
- void fill(){
- for (auto filter : filters){
- if (!filter->get_value()) return;
- }
- _fill();
- }
- void set_description(const std::string& description){
- desc = description;
- }
- const std::string& get_name(){
- return name;
- }
- virtual const std::string get_value_name(){
- return "N/A";
- }
- virtual GenContainer* clone_as(const std::string& new_name) = 0;
- virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
- virtual void save(const SaveOption& option=SaveOption::PNG) {
- save_as(get_name(), option);
- }
- };
- template <typename H, typename V>
- class Container : public GenContainer{
- protected:
- H* container;
- Value<V> *value;
- public:
- Container(const std::string& name, Value<V>* value)
- :GenContainer(name),
- container(nullptr),
- value(value){ }
- virtual H* get_container(){
- return container;
- }
- virtual const std::string get_value_name(){
- return value->get_name();
- }
- };
- template <typename T>
- class ContainerMean : public Container<T,T>{
- private:
- int count;
- T sum;
- void _fill(){
- count++;
- sum += this->value->get_value();
- }
- public:
- ContainerMean(const std::string& name, Value<T>* value)
- :Container<std::vector<T>,T>(name, value){
- this->container = new T();
- }
- T* get_container(){
- *(this->container) = sum/count;
- return (this->container);
- }
- GenContainer* clone_as(const std::string& new_name){
- return new ContainerMean(new_name, this->value);
- }
- void save_as(const std::string&) {
- WARNING("Saving of ContainerMean objects not supported");
- }
- };
- }
- #endif
|