mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 00:31:14 +00:00
add function helpers for FairProgOptions which simplify FairProgOptions.cxx
This commit is contained in:
parent
13d3729fec
commit
819a8df952
|
@ -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<string>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<string>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// vector<string>
|
||||
if (auto q = boost::any_cast<vector<string>>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<vector<string>>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// int
|
||||
if (auto q = boost::any_cast<int>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<int>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// vector<int>
|
||||
if (auto q = boost::any_cast<vector<int>>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<vector<int>>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// float
|
||||
if (auto q = boost::any_cast<float>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<float>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// vector float
|
||||
if (auto q = boost::any_cast<vector<float>>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<vector<float>>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// double
|
||||
if (auto q = boost::any_cast<double>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<double>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// vector double
|
||||
if (auto q = boost::any_cast<vector<double>>(&value))
|
||||
{
|
||||
valueStr = VariableValueToString<vector<double>>(fVarMap[key]);
|
||||
return valueStr;
|
||||
}
|
||||
valueStr=fairmq::ConvertVariableValue<fairmq::ToString>().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<string>, vector<int>, vector<float>, vector<double>
|
||||
// string, int, float, double, short, boost::filesystem::path
|
||||
// vector<string>, vector<int>, vector<float>, vector<double>, vector<short>
|
||||
|
||||
MapVarValInfo_t mapinfo;
|
||||
|
||||
|
@ -400,92 +344,5 @@ int FairProgOptions::NotifySwitchOption()
|
|||
|
||||
FairProgOptions::VarValInfo_t FairProgOptions::GetVariableValueInfo(const po::variable_value& varValue)
|
||||
{
|
||||
// tuple<valueStr, type_info_str, defaultStr, empty>
|
||||
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<string>(&value))
|
||||
{
|
||||
string valueStr = *q;
|
||||
return make_tuple(valueStr, string(" [Type=string]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// vector<string>
|
||||
if (auto q = boost::any_cast<vector<string>>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<vector<string>>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=vector<string>]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// int
|
||||
if (auto q = boost::any_cast<int>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<int>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=int]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// vector<int>
|
||||
if (auto q = boost::any_cast<vector<int>>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<vector<int>>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=vector<int>]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// float
|
||||
if (auto q = boost::any_cast<float>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<float>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=float]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// vector<float>
|
||||
if (auto q = boost::any_cast<vector<float>>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<vector<float>>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=vector<float>]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// double
|
||||
if (auto q = boost::any_cast<double>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<double>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=double]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// vector<double>
|
||||
if (auto q = boost::any_cast<vector<double>>(&value))
|
||||
{
|
||||
string valueStr = VariableValueToString<vector<double>>(varValue);
|
||||
return make_tuple(valueStr, string(" [Type=vector<double>]"), defaultedValue, emptyValue);
|
||||
}
|
||||
|
||||
// boost::filesystem::path
|
||||
if (auto q = boost::any_cast<boost::filesystem::path>(&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<fairmq::ToVarInfo>().Run(varValue);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include "FairMQLogger.h"
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include "FairProgOptionsHelper.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
@ -55,12 +55,6 @@
|
|||
* }
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(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<typename T>
|
||||
std::string VariableValueToString(const po::variable_value& varValue)
|
||||
{
|
||||
auto& value = varValue.value();
|
||||
std::ostringstream ostr;
|
||||
if (auto q = boost::any_cast<T>(&value))
|
||||
{
|
||||
ostr << *q;
|
||||
}
|
||||
std::string valStr = ostr.str();
|
||||
return valStr;
|
||||
}
|
||||
|
||||
static void Max(int &val, const int &comp)
|
||||
{
|
||||
if (comp > val)
|
||||
|
|
232
fairmq/options/FairProgOptionsHelper.h
Normal file
232
fairmq/options/FairProgOptionsHelper.h
Normal file
|
@ -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 <boost/program_options.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <tuple>
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(os, " "));
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace fairmq
|
||||
{
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
//_____________________________________________________________________________________________________________________________
|
||||
|
||||
template<typename T>
|
||||
bool is_this_type(const po::variable_value& varValue)
|
||||
{
|
||||
auto& value = varValue.value();
|
||||
if (auto q = boost::any_cast<T>(&value))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________________________________________________________
|
||||
|
||||
template<typename T>
|
||||
std::string ConvertVariableValueToString(const po::variable_value& varValue)
|
||||
{
|
||||
auto& value = varValue.value();
|
||||
std::ostringstream ostr;
|
||||
if (auto q = boost::any_cast<T>(&value))
|
||||
{
|
||||
ostr << *q;
|
||||
}
|
||||
std::string valueStr = ostr.str();
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// string specialization
|
||||
template<>
|
||||
inline std::string ConvertVariableValueToString<std::string>(const po::variable_value& varValue)
|
||||
{
|
||||
auto& value = varValue.value();
|
||||
std::string valueStr;
|
||||
if (auto q = boost::any_cast<std::string>(&value))
|
||||
{
|
||||
valueStr = *q;
|
||||
}
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
// boost::filesystem::path specialization
|
||||
template<>
|
||||
inline std::string ConvertVariableValueToString<boost::filesystem::path>(const po::variable_value& varValue)
|
||||
{
|
||||
auto& value = varValue.value();
|
||||
std::string valueStr;
|
||||
if (auto q = boost::any_cast<boost::filesystem::path>(&value))
|
||||
{
|
||||
valueStr = (*q).string();
|
||||
}
|
||||
return valueStr;
|
||||
}
|
||||
|
||||
//_____________________________________________________________________________________________________________________________
|
||||
// policy to convert boost variable value into string
|
||||
struct ToString
|
||||
{
|
||||
typedef std::string returned_type;
|
||||
template<typename T>
|
||||
std::string Value(const po::variable_value& varValue,const std::string& type, const std::string& defaulted, const std::string& empty)
|
||||
{
|
||||
return ConvertVariableValueToString<T>(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<std::string, std::string,std::string, std::string> returned_type;
|
||||
template<typename T>
|
||||
returned_type Value(const po::variable_value& varValue,const std::string& type, const std::string& defaulted, const std::string& empty)
|
||||
{
|
||||
std::string valueStr = ConvertVariableValueToString<T>(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<typename T>
|
||||
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<std::string>(varValue))
|
||||
return T::template Value<std::string>(varValue,std::string(" [Type=string]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector<std::string>
|
||||
if (is_this_type<std::vector<std::string>>(varValue))
|
||||
return T::template Value<std::vector<std::string>>(varValue,std::string(" [Type=vector<string>]"),defaultedValue,emptyValue);
|
||||
|
||||
// int
|
||||
if (is_this_type<int>(varValue))
|
||||
return T::template Value<int>(varValue,std::string(" [Type=int]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector<int>
|
||||
if (is_this_type<std::vector<int>>(varValue))
|
||||
return T::template Value<std::vector<int>>(varValue,std::string(" [Type=vector<int>]"),defaultedValue,emptyValue);
|
||||
|
||||
// float
|
||||
if (is_this_type<float>(varValue))
|
||||
return T::template Value<float>(varValue,std::string(" [Type=float]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector float
|
||||
if (is_this_type<std::vector<float>>(varValue))
|
||||
return T::template Value<std::vector<float>>(varValue,std::string(" [Type=vector<float>]"),defaultedValue,emptyValue);
|
||||
|
||||
// double
|
||||
if (is_this_type<double>(varValue))
|
||||
return T::template Value<double>(varValue,std::string(" [Type=double]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector double
|
||||
if (is_this_type<std::vector<double>>(varValue))
|
||||
return T::template Value<std::vector<double>>(varValue,std::string(" [Type=vector<double>]"),defaultedValue,emptyValue);
|
||||
|
||||
// short
|
||||
if (is_this_type<short>(varValue))
|
||||
return T::template Value<short>(varValue,std::string(" [Type=short]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector short
|
||||
if (is_this_type<std::vector<short>>(varValue))
|
||||
return T::template Value<std::vector<short>>(varValue,std::string(" [Type=vector<short>]"),defaultedValue,emptyValue);
|
||||
|
||||
// long
|
||||
if (is_this_type<long>(varValue))
|
||||
return T::template Value<long>(varValue,std::string(" [Type=long]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector short
|
||||
if (is_this_type<std::vector<long>>(varValue))
|
||||
return T::template Value<std::vector<long>>(varValue,std::string(" [Type=vector<long>]"),defaultedValue,emptyValue);
|
||||
|
||||
// size_t
|
||||
if (is_this_type<std::size_t>(varValue))
|
||||
return T::template Value<std::size_t>(varValue,std::string(" [Type=std::size_t]"),defaultedValue,emptyValue);
|
||||
|
||||
// std::vector short
|
||||
if (is_this_type<std::vector<std::size_t>>(varValue))
|
||||
return T::template Value<std::vector<std::size_t>>(varValue,std::string(" [Type=vector<std::size_t>]"),defaultedValue,emptyValue);
|
||||
|
||||
|
||||
//////////////////////////////// boost types
|
||||
// boost::filesystem::path
|
||||
if (is_this_type<boost::filesystem::path>(varValue))
|
||||
return T::template Value<boost::filesystem::path>(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 */
|
Loading…
Reference in New Issue
Block a user