config.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. */
  32. #ifndef config_hpp
  33. #define config_hpp
  34. #include <string>
  35. #include "yaml-cpp/yaml.h"
  36. namespace fv_util {
  37. struct DataFileDescriptor{
  38. std::string filename;
  39. std::string label;
  40. std::string category;
  41. int file_number;
  42. int n_events;
  43. DataFileDescriptor():filename(""), label(""),category(""),file_number(-1), n_events(-1){ }
  44. DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int n_events)
  45. :filename(filename), label(label),category(category),file_number(-1), n_events(n_events) { }
  46. DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int file_number, const int n_events)
  47. :filename(filename), label(label),category(category),file_number(file_number), n_events(n_events) { }
  48. };
  49. }
  50. namespace YAML {
  51. template<>
  52. struct convert<fv_util::DataFileDescriptor> {
  53. static Node encode(const fv_util::DataFileDescriptor &rhs) {
  54. Node node;
  55. node["filename"] = rhs.filename;
  56. node["label"] = rhs.label;
  57. node["category"] = rhs.category;
  58. node["n_events"] = rhs.n_events;
  59. return node;
  60. }
  61. static bool decode(const Node &node, fv_util::DataFileDescriptor &rhs) {
  62. if (!node.IsMap()) {
  63. return false;
  64. }
  65. if (!node["filename"]) return false;
  66. rhs.filename = node["filename"].as<std::string>();
  67. if (node["label"]) rhs.label = node["label"].as<std::string>();
  68. if (node["category"]) rhs.category = node["category"].as<std::string>();
  69. if (node["n_events"]) rhs.n_events = node["n_events"].as<int>();
  70. return true;
  71. }
  72. };
  73. }
  74. namespace fv_util {
  75. class Config{
  76. private:
  77. std::string input_filename;
  78. YAML::Node root;
  79. public:
  80. Config ()
  81. :root(), input_filename("") { }
  82. Config (const Config& c)
  83. :root(c.root), input_filename(c.input_filename) { }
  84. Config (const std::string& input_filename)
  85. :root(YAML::LoadFile(input_filename)),
  86. input_filename(input_filename) { }
  87. template <typename KEY_TYPE>
  88. YAML::Node get(const KEY_TYPE& key) {
  89. auto val = root[key];
  90. return val;
  91. }
  92. size_t get_max_events() {
  93. YAML::Node max_events = root["max-events"];
  94. if(!max_events) {
  95. return 0;
  96. }
  97. return max_events.as<size_t>();
  98. }
  99. bool get_debug() {
  100. YAML::Node debug_node = root["debug"];
  101. if(!debug_node) {
  102. return false;
  103. }
  104. return debug_node.as<bool>();
  105. }
  106. std::string get_output_filename() {
  107. YAML::Node output_file = root["output-file"];
  108. if(output_file) {
  109. return output_file.as<std::string>();
  110. } else {
  111. std::cout << "Must specify output-file in config file." << std::endl;
  112. return "";
  113. }
  114. }
  115. std::vector<DataFileDescriptor> get_source_files() {
  116. std::vector<DataFileDescriptor> source_files;
  117. YAML::Node source_files_nd = root["source-files"];
  118. if(source_files_nd.IsSequence()) {
  119. for (const auto& f : source_files_nd) {
  120. source_files.push_back(f.as<DataFileDescriptor>());
  121. }
  122. } else {
  123. std::cout << "source-files missing or not a sequence" << std::endl;
  124. }
  125. return source_files;
  126. }
  127. };
  128. Config* the_config = nullptr;
  129. bool debug_on = false;
  130. void init_config(const std::string& input_filename){
  131. if(the_config) delete the_config;
  132. the_config = new Config(input_filename);
  133. debug_on = the_config->get_debug();
  134. }
  135. }
  136. #endif // config_hpp