From 819a8df952b1ff5481e32a56e8b9d7b14d07e1d6 Mon Sep 17 00:00:00 2001 From: NicolasWinckler Date: Mon, 9 Nov 2015 12:18:34 +0100 Subject: [PATCH] add function helpers for FairProgOptions which simplify FairProgOptions.cxx --- fairmq/options/FairProgOptions.cxx | 151 +--------------- fairmq/options/FairProgOptions.h | 21 +-- fairmq/options/FairProgOptionsHelper.h | 232 +++++++++++++++++++++++++ 3 files changed, 237 insertions(+), 167 deletions(-) create mode 100644 fairmq/options/FairProgOptionsHelper.h diff --git a/fairmq/options/FairProgOptions.cxx b/fairmq/options/FairProgOptions.cxx index 355cd680..02fa0692 100644 --- a/fairmq/options/FairProgOptions.cxx +++ b/fairmq/options/FairProgOptions.cxx @@ -200,63 +200,7 @@ string FairProgOptions::GetStringValue(const string& key) { if (fVarMap.count(key)) { - auto& value = fVarMap[key].value(); - - // string albeit useless here - if (auto q = boost::any_cast(&value)) - { - valueStr = VariableValueToString(fVarMap[key]); - return valueStr; - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - valueStr = VariableValueToString>(fVarMap[key]); - return valueStr; - } - - // int - if (auto q = boost::any_cast(&value)) - { - valueStr = VariableValueToString(fVarMap[key]); - return valueStr; - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - valueStr = VariableValueToString>(fVarMap[key]); - return valueStr; - } - - // float - if (auto q = boost::any_cast(&value)) - { - valueStr = VariableValueToString(fVarMap[key]); - return valueStr; - } - - // vector float - if (auto q = boost::any_cast>(&value)) - { - valueStr = VariableValueToString>(fVarMap[key]); - return valueStr; - } - - // double - if (auto q = boost::any_cast(&value)) - { - valueStr = VariableValueToString(fVarMap[key]); - return valueStr; - } - - // vector double - if (auto q = boost::any_cast>(&value)) - { - valueStr = VariableValueToString>(fVarMap[key]); - return valueStr; - } + valueStr=fairmq::ConvertVariableValue().Run(fVarMap.at(key)); } } catch(exception& e) @@ -282,8 +226,8 @@ int FairProgOptions::PrintOptions() // Method to overload. // -> loop over variable map and print its content // -> In this example the following types are supported: - // string, int, float, double, boost::filesystem::path - // vector, vector, vector, vector + // string, int, float, double, short, boost::filesystem::path + // vector, vector, vector, vector, vector MapVarValInfo_t mapinfo; @@ -400,92 +344,5 @@ int FairProgOptions::NotifySwitchOption() FairProgOptions::VarValInfo_t FairProgOptions::GetVariableValueInfo(const po::variable_value& varValue) { - // tuple - auto& value = varValue.value(); - string defaultedValue; - string emptyValue; - - if (varValue.empty()) - { - emptyValue = " [empty]"; - } - else - { - if (varValue.defaulted()) - { - defaultedValue = " [default value]"; - } - else - { - defaultedValue = " [provided value]"; - } - } - - emptyValue += " *"; - // string - if (auto q = boost::any_cast(&value)) - { - string valueStr = *q; - return make_tuple(valueStr, string(" [Type=string]"), defaultedValue, emptyValue); - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - string valueStr = VariableValueToString>(varValue); - return make_tuple(valueStr, string(" [Type=vector]"), defaultedValue, emptyValue); - } - - // int - if (auto q = boost::any_cast(&value)) - { - string valueStr = VariableValueToString(varValue); - return make_tuple(valueStr, string(" [Type=int]"), defaultedValue, emptyValue); - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - string valueStr = VariableValueToString>(varValue); - return make_tuple(valueStr, string(" [Type=vector]"), defaultedValue, emptyValue); - } - - // float - if (auto q = boost::any_cast(&value)) - { - string valueStr = VariableValueToString(varValue); - return make_tuple(valueStr, string(" [Type=float]"), defaultedValue, emptyValue); - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - string valueStr = VariableValueToString>(varValue); - return make_tuple(valueStr, string(" [Type=vector]"), defaultedValue, emptyValue); - } - - // double - if (auto q = boost::any_cast(&value)) - { - string valueStr = VariableValueToString(varValue); - return make_tuple(valueStr, string(" [Type=double]"), defaultedValue, emptyValue); - } - - // vector - if (auto q = boost::any_cast>(&value)) - { - string valueStr = VariableValueToString>(varValue); - return make_tuple(valueStr, string(" [Type=vector]"), defaultedValue, emptyValue); - } - - // boost::filesystem::path - if (auto q = boost::any_cast(&value)) - { - string valueStr = (*q).string(); - //string valueStr = (*q).filename().generic_string(); - return make_tuple(valueStr, string(" [Type=boost::filesystem::path]"), defaultedValue, emptyValue); - } - - // if we get here, the type is not supported return unknown info - return make_tuple(string("Unknown value"), string(" [Type=Unknown]"), defaultedValue, emptyValue); + return fairmq::ConvertVariableValue().Run(varValue); } diff --git a/fairmq/options/FairProgOptions.h b/fairmq/options/FairProgOptions.h index 3be63721..adbc870f 100644 --- a/fairmq/options/FairProgOptions.h +++ b/fairmq/options/FairProgOptions.h @@ -18,7 +18,7 @@ #include "FairMQLogger.h" #include #include - +#include "FairProgOptionsHelper.h" #include #include #include @@ -55,12 +55,6 @@ * } */ -template -std::ostream& operator<<(std::ostream& os, const std::vector& v) -{ - std::copy(v.begin(), v.end(), std::ostream_iterator(os, " ")); - return os; -} namespace po = boost::program_options; namespace fs = boost::filesystem; @@ -165,19 +159,6 @@ class FairProgOptions VarValInfo_t GetVariableValueInfo(const po::variable_value& varValue); - template - std::string VariableValueToString(const po::variable_value& varValue) - { - auto& value = varValue.value(); - std::ostringstream ostr; - if (auto q = boost::any_cast(&value)) - { - ostr << *q; - } - std::string valStr = ostr.str(); - return valStr; - } - static void Max(int &val, const int &comp) { if (comp > val) diff --git a/fairmq/options/FairProgOptionsHelper.h b/fairmq/options/FairProgOptionsHelper.h new file mode 100644 index 00000000..4acd7a72 --- /dev/null +++ b/fairmq/options/FairProgOptionsHelper.h @@ -0,0 +1,232 @@ +/******************************************************************************** + * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence version 3 (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ +/* + * File: FairProgOptionsHelper.h + * Author: winckler + * + * Created on March 11, 2015, 5:38 PM + */ + +#ifndef FAIRPROGOPTIONSHELPER_H +#define FAIRPROGOPTIONSHELPER_H + +#include +#include + +#include +#include +#include +#include +#include +#include + + + +template +std::ostream& operator<<(std::ostream& os, const std::vector& v) +{ + std::copy(v.begin(), v.end(), std::ostream_iterator(os, " ")); + return os; +} + +namespace fairmq +{ + + namespace po = boost::program_options; + + //_____________________________________________________________________________________________________________________________ + + template + bool is_this_type(const po::variable_value& varValue) + { + auto& value = varValue.value(); + if (auto q = boost::any_cast(&value)) + return true; + else + return false; + } + +//_____________________________________________________________________________________________________________________________ + + template + std::string ConvertVariableValueToString(const po::variable_value& varValue) + { + auto& value = varValue.value(); + std::ostringstream ostr; + if (auto q = boost::any_cast(&value)) + { + ostr << *q; + } + std::string valueStr = ostr.str(); + return valueStr; + } + + // string specialization + template<> + inline std::string ConvertVariableValueToString(const po::variable_value& varValue) + { + auto& value = varValue.value(); + std::string valueStr; + if (auto q = boost::any_cast(&value)) + { + valueStr = *q; + } + return valueStr; + } + + // boost::filesystem::path specialization + template<> + inline std::string ConvertVariableValueToString(const po::variable_value& varValue) + { + auto& value = varValue.value(); + std::string valueStr; + if (auto q = boost::any_cast(&value)) + { + valueStr = (*q).string(); + } + return valueStr; + } + + //_____________________________________________________________________________________________________________________________ + // policy to convert boost variable value into string + struct ToString + { + typedef std::string returned_type; + template + std::string Value(const po::variable_value& varValue,const std::string& type, const std::string& defaulted, const std::string& empty) + { + return ConvertVariableValueToString(varValue); + } + + returned_type DefaultValue(const std::string& defaulted, const std::string& empty) + { + return std::string("empty value"); + } + }; +//_____________________________________________________________________________________________________________________________ + + // policy to convert variable value content into a tuple with value, type, defaulted, empty information + struct ToVarInfo + { + typedef std::tuple returned_type; + template + returned_type Value(const po::variable_value& varValue,const std::string& type, const std::string& defaulted, const std::string& empty) + { + std::string valueStr = ConvertVariableValueToString(varValue); + return make_tuple(valueStr, type, defaulted, empty); + } + + returned_type DefaultValue(const std::string& defaulted, const std::string& empty) + { + return make_tuple(std::string("Unknown value"), std::string(" [Type=Unknown]"), defaulted, empty); + } + }; + +//_____________________________________________________________________________________________________________________________ + + // host class that take one of the two policy defined above + template + struct ConvertVariableValue : T + { + //typename T::returned_type Run(const po::variable_value& varValue) //-> decltype(T::returned_type) + auto Run(const po::variable_value& varValue) -> typename T::returned_type + { + std::string defaultedValue; + std::string emptyValue; + + if (varValue.empty()) + { + emptyValue = " [empty]"; + } + else + { + if (varValue.defaulted()) + { + defaultedValue = " [default value]"; + } + else + { + defaultedValue = " [provided value]"; + } + } + + emptyValue += " *"; + + //////////////////////////////// std types + // std::string albeit useless here + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=string]"),defaultedValue,emptyValue); + + // std::vector + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // int + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=int]"),defaultedValue,emptyValue); + + // std::vector + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // float + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=float]"),defaultedValue,emptyValue); + + // std::vector float + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // double + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=double]"),defaultedValue,emptyValue); + + // std::vector double + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // short + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=short]"),defaultedValue,emptyValue); + + // std::vector short + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // long + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=long]"),defaultedValue,emptyValue); + + // std::vector short + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + // size_t + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=std::size_t]"),defaultedValue,emptyValue); + + // std::vector short + if (is_this_type>(varValue)) + return T::template Value>(varValue,std::string(" [Type=vector]"),defaultedValue,emptyValue); + + + //////////////////////////////// boost types + // boost::filesystem::path + if (is_this_type(varValue)) + return T::template Value(varValue,std::string(" [Type=boost::filesystem::path]"),defaultedValue,emptyValue); + + // if we get here, the type is not supported return unknown info + return T::DefaultValue(defaultedValue,emptyValue); + } + + }; + + + +}; + #endif /* FAIRPROGOPTIONSHELPER_H */ \ No newline at end of file