123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * @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 config_hpp
- #define config_hpp
- #include <string>
- #include "yaml-cpp/yaml.h"
- namespace fv_util {
- struct DataFileDescriptor{
- std::string filename;
- std::string label;
- std::string category;
- int file_number;
- int n_events;
- DataFileDescriptor():filename(""), label(""),category(""),file_number(-1), n_events(-1){ }
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int n_events)
- :filename(filename), label(label),category(category),file_number(-1), n_events(n_events) { }
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category, const int file_number, const int n_events)
- :filename(filename), label(label),category(category),file_number(file_number), n_events(n_events) { }
- };
- }
- namespace YAML {
- template<>
- struct convert<fv_util::DataFileDescriptor> {
- static Node encode(const fv_util::DataFileDescriptor &rhs) {
- Node node;
- node["filename"] = rhs.filename;
- node["label"] = rhs.label;
- node["category"] = rhs.category;
- node["n_events"] = rhs.n_events;
- return node;
- }
- static bool decode(const Node &node, fv_util::DataFileDescriptor &rhs) {
- if (!node.IsMap()) {
- return false;
- }
- if (!node["filename"]) return false;
- rhs.filename = node["filename"].as<std::string>();
- if (node["label"]) rhs.label = node["label"].as<std::string>();
- if (node["category"]) rhs.category = node["category"].as<std::string>();
- if (node["n_events"]) rhs.n_events = node["n_events"].as<int>();
- return true;
- }
- };
- }
- namespace fv_util {
- class Config{
- private:
- std::string input_filename;
- YAML::Node root;
- public:
- Config ()
- :root(), input_filename("") { }
- Config (const Config& c)
- :root(c.root), input_filename(c.input_filename) { }
- Config (const std::string& input_filename)
- :root(YAML::LoadFile(input_filename)),
- input_filename(input_filename) { }
- template <typename KEY_TYPE>
- YAML::Node get(const KEY_TYPE& key) {
- auto val = root[key];
- return val;
- }
- size_t get_max_events() {
- YAML::Node max_events = root["max-events"];
- if(!max_events) {
- return 0;
- }
- return max_events.as<size_t>();
- }
- bool get_debug() {
- YAML::Node debug_node = root["debug"];
- if(!debug_node) {
- return false;
- }
- return debug_node.as<bool>();
- }
- std::string get_output_filename() {
- YAML::Node output_file = root["output-file"];
- if(output_file) {
- return output_file.as<std::string>();
- } else {
- std::cout << "Must specify output-file in config file." << std::endl;
- return "";
- }
- }
- std::vector<DataFileDescriptor> get_source_files() {
- std::vector<DataFileDescriptor> source_files;
- YAML::Node source_files_nd = root["source-files"];
- if(source_files_nd.IsSequence()) {
- for (const auto& f : source_files_nd) {
- source_files.push_back(f.as<DataFileDescriptor>());
- }
- } else {
- std::cout << "source-files missing or not a sequence" << std::endl;
- }
- return source_files;
- }
- };
- Config* the_config = nullptr;
- bool debug_on = false;
- void init_config(const std::string& input_filename){
- if(the_config) delete the_config;
- the_config = new Config(input_filename);
- debug_on = the_config->get_debug();
- }
- }
- #endif // config_hpp
|