123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /**
- * @file
- * @author Caleb Fangmeier <caleb@fangmeier.tech>
- * @version 0.1
- *
- * @section LICENSE
- *
- *
- * MIT License
- *
- * Copyright (c) 2017 Caleb Fangmeier
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- #ifndef container_hpp
- #define container_hpp
- #include <typeindex>
- #include <vector>
- #include <map>
- #include "value.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;
- // Add to this list as needed :)
- _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::pair<std::vector<float>, std::vector<float>>)] = "std::pair<std::vector<float>,std::vector<float>>";
- _map[typeid(std::vector<std::pair<float, int>>)] = "std::vector<std::pair<float,int>>";
- _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 {
- /**
- * Enumeration of different options that can be used to save Containers. Not
- * all options are allowed for all Containers.
- */
- enum SaveOption {
- PNG = 0,
- PDF = 1,
- ROOT = 2
- };
- /**
- * Generic, untyped parent class of Container. Used to allow for placing
- * Containers of disparate types in common data structures.
- */
- class GenContainer {
- private:
- std::string name;
- std::string desc;
- public:
- GenContainer(const std::string name, const std::string &desc)
- : name(name), desc(desc) {}
- GenContainer(const std::string name)
- : GenContainer(name, "N/A") {}
- 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);
- }
- };
- /**
- * A class that is used to "hold" values. When an event is loaded the
- * \c fill() method is called and the Container can access the stored Value
- * object to process it. For example, if the Container is a ROOT Histogram
- * object, it may call <tt>container->Fill(value->get_value())</tt>.
- */
- template<typename H, typename V>
- class Container : public GenContainer {
- protected:
- H *container;
- public:
- Container(const std::string &name)
- : GenContainer(name),
- container(nullptr) {}
- virtual ~Container() {
- if (container != nullptr) delete container;
- }
- virtual H *get_container() {
- return container;
- }
- virtual void fill(const V&) = 0;
- };
- /**
- * Prints out the value registered to it
- */
- template<typename T>
- class Logger : public Container<std::ostream, T> {
- private:
- using to_str_t = std::function<void(T, std::ostream &)>;
- to_str_t value_to_string;
- std::string logger_name;
- static void default_to_string(T &t, std::ostream &os) {
- os << t;
- }
- public:
- void fill(const T& the_value) {
- auto &os = (*this->container);
- std::stringstream vs;
- os << this->get_name() << std::endl;
- if (value_to_string != nullptr) value_to_string(the_value, vs);
- else default_to_string(the_value, vs);
- for (const char &c : vs.str()) {
- if (c == '\n') os << std::endl << " ";
- else os << c;
- }
- os << std::endl;
- }
- Logger(std::ostream *out, const std::string& logger_name, to_str_t value_to_string = nullptr)
- : Container<std::ostream, T>("Logger:" + logger_name),
- value_to_string(value_to_string), logger_name(logger_name) {
- this->container = out;
- }
- std::ostream *get_container() {
- return this->container;
- }
- GenContainer *clone_as(const std::string &new_name) {
- return new Logger(this->container, logger_name, value_to_string);
- }
- void save_as(const std::string &, const SaveOption &) {}
- };
- }
- #endif // container_hpp
|