/******************************************************************************** * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ /* * File: FairProgOptions.h * Author: winckler * * Created on March 11, 2015, 5:38 PM */ #ifndef FAIRPROGOPTIONS_H #define FAIRPROGOPTIONS_H #include "FairMQLogger.h" #include "FairProgOptionsHelper.h" #include #include #include #include #include #include #include #include #include namespace po = boost::program_options; namespace fs = boost::filesystem; class FairProgOptions { public: FairProgOptions(); virtual ~FairProgOptions(); auto GetPropertyKeys() const -> std::vector { std::lock_guard lock{fConfigMutex}; std::vector result; for (const auto& it : fVarMap) { result.push_back(it.first.c_str()); } return result; } // add options_description int AddToCmdLineOptions(const po::options_description optDesc, bool visible = true); po::options_description& GetCmdLineOptions(); // get value corresponding to the key template T GetValue(const std::string& key) const { std::unique_lock lock(fConfigMutex); T val = T(); if (fVarMap.count(key)) { val = fVarMap[key].as(); } else { LOG(warn) << "Config has no key: " << key << ". Returning default constructed object."; } return val; } // Given a key, convert the variable value to string std::string GetStringValue(const std::string& key) { std::unique_lock lock(fConfigMutex); std::string valueStr; try { if (fVarMap.count(key)) { valueStr = fair::mq::ConvertVariableValue()(fVarMap.at(key)); } } catch (std::exception& e) { LOG(error) << "Exception thrown for the key '" << key << "'"; LOG(error) << e.what(); } return valueStr; } int Count(const std::string& key) const { std::unique_lock lock(fConfigMutex); return fVarMap.count(key); } //restrict conversion to fundamental types template T ConvertTo(const std::string& strValue) { if (std::is_arithmetic::value) { std::istringstream iss(strValue); T val; iss >> val; return val; } else { LOG(error) << "the provided string " << strValue << " cannot be converted in the requested type. The target types must be arithmetic types"; } } const po::variables_map& GetVarMap() const { return fVarMap; } int ParseCmdLine(const int argc, char const* const* argv, bool allowUnregistered = false); void ParseDefaults(); virtual int ParseAll(const int argc, char const* const* argv, bool allowUnregistered = false) = 0; virtual int PrintOptions(); virtual int PrintOptionsRaw(); protected: // options container po::variables_map fVarMap; // options descriptions po::options_description fGeneralOptions; po::options_description fAllOptions; mutable std::mutex fConfigMutex; virtual int ImmediateOptions() = 0; // UpdateVarMap() and Replace() --> helper functions to modify the value of variable map after calling po::store template void UpdateVarMap(const std::string& key, const T& val) { Replace(fVarMap, key, val); } template void Replace(std::map& vm, const std::string& key, const T& val) { vm[key].value() = boost::any(val); } private: fair::mq::VarValInfo GetVariableValueInfo(const po::variable_value& varValue); static void Max(int& val, const int& comp) { if (comp > val) { val = comp; } } }; #endif /* FAIRPROGOPTIONS_H */