container.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. #ifndef container_hpp
  32. #define container_hpp
  33. #include <typeindex>
  34. #include <vector>
  35. #include <map>
  36. #include "value.hpp"
  37. template
  38. class std::vector<std::vector<float> >;
  39. template
  40. class std::vector<std::vector<int> >;
  41. namespace fv::util {
  42. std::string get_type_name(const std::type_index &index) {
  43. std::map<std::type_index, std::string> _map;
  44. // Add to this list as needed :)
  45. _map[typeid(int)] = "int";
  46. _map[typeid(unsigned int)] = "unsigned int";
  47. _map[typeid(float)] = "float";
  48. _map[typeid(double)] = "double";
  49. _map[typeid(std::vector<int>)] = "std::vector<int>";
  50. _map[typeid(std::vector<float>)] = "std::vector<float>";
  51. _map[typeid(std::pair<std::vector<float>, std::vector<float>>)] = "std::pair<std::vector<float>,std::vector<float>>";
  52. _map[typeid(std::vector<std::pair<float, int>>)] = "std::vector<std::pair<float,int>>";
  53. _map[typeid(std::map<std::string, std::string>)] = "std::map<std::string,std::string>";
  54. if (_map[index] == "") {
  55. CRITICAL("Cannot lookup type name of \"" << index.name() << "\"", -1);
  56. }
  57. return _map[index];
  58. }
  59. }
  60. namespace fv {
  61. /**
  62. * Enumeration of different options that can be used to save Containers. Not
  63. * all options are allowed for all Containers.
  64. */
  65. enum SaveOption {
  66. PNG = 0,
  67. PDF = 1,
  68. ROOT = 2
  69. };
  70. /**
  71. * Generic, untyped parent class of Container. Used to allow for placing
  72. * Containers of disparate types in common data structures.
  73. */
  74. class GenContainer {
  75. private:
  76. std::string name;
  77. std::string desc;
  78. public:
  79. GenContainer(const std::string name, const std::string &desc)
  80. : name(name), desc(desc) {}
  81. GenContainer(const std::string name)
  82. : GenContainer(name, "N/A") {}
  83. void set_description(const std::string &description) {
  84. desc = description;
  85. }
  86. const std::string &get_name() {
  87. return name;
  88. }
  89. virtual const std::string get_value_name() {
  90. return "N/A";
  91. }
  92. virtual GenContainer *clone_as(const std::string &new_name) = 0;
  93. virtual void save_as(const std::string &fname, const SaveOption &option) = 0;
  94. virtual void save(const SaveOption &option = SaveOption::PNG) {
  95. save_as(get_name(), option);
  96. }
  97. };
  98. /**
  99. * A class that is used to "hold" values. When an event is loaded the
  100. * \c fill() method is called and the Container can access the stored Value
  101. * object to process it. For example, if the Container is a ROOT Histogram
  102. * object, it may call <tt>container->Fill(value->get_value())</tt>.
  103. */
  104. template<typename H, typename V>
  105. class Container : public GenContainer {
  106. protected:
  107. H *container;
  108. public:
  109. Container(const std::string &name)
  110. : GenContainer(name),
  111. container(nullptr) {}
  112. virtual ~Container() {
  113. if (container != nullptr) delete container;
  114. }
  115. virtual H *get_container() {
  116. return container;
  117. }
  118. };
  119. /**
  120. * Prints out the value registered to it
  121. */
  122. template<typename T>
  123. class Logger : public Container<std::ostream, T> {
  124. private:
  125. using to_str_t = std::function<void(T, std::ostream &)>;
  126. to_str_t value_to_string;
  127. std::string logger_name;
  128. static void default_to_string(T &t, std::ostream &os) {
  129. os << t;
  130. }
  131. public:
  132. void fill(const T& the_value) {
  133. auto &os = (*this->container);
  134. std::stringstream vs;
  135. os << this->get_name() << std::endl;
  136. if (value_to_string != nullptr) value_to_string(the_value, vs);
  137. else default_to_string(the_value, vs);
  138. for (const char &c : vs.str()) {
  139. if (c == '\n') os << std::endl << " ";
  140. else os << c;
  141. }
  142. os << std::endl;
  143. }
  144. Logger(std::ostream *out, const std::string& logger_name, to_str_t value_to_string = nullptr)
  145. : Container<std::ostream, T>("Logger:" + logger_name),
  146. value_to_string(value_to_string), logger_name(logger_name) {
  147. this->container = out;
  148. }
  149. std::ostream *get_container() {
  150. return this->container;
  151. }
  152. GenContainer *clone_as(const std::string &new_name) {
  153. return new Logger(this->container, logger_name, value_to_string);
  154. }
  155. void save_as(const std::string &, const SaveOption &) {}
  156. };
  157. }
  158. #endif // container_hpp