#include "PndConfigParameters.h" #include "boost/lexical_cast.hpp" using boost::lexical_cast; using boost::bad_lexical_cast; PndConfigParameters::PndConfigParameters() { // TODO Auto-generated constructor stub } PndConfigParameters::~PndConfigParameters() { // TODO Auto-generated destructor stub } void PndConfigParameters::insertParameters(const std::string &name_base, const boost::property_tree::ptree &config_parameters_) { boost::property_tree::ptree::const_iterator iter; for (iter = config_parameters_.begin(); iter != config_parameters_.end(); iter++) { if (iter->second.empty()) { config_parameters[name_base + iter->first] = iter->second.data(); } else { insertParameters(name_base + "." + iter->first, iter->second); } } } void PndConfigParameters::setParameters( const boost::property_tree::ptree &config_parameters_) { boost::property_tree::ptree::const_iterator iter; // convert the ptree to simple format... for (iter = config_parameters_.begin(); iter != config_parameters_.end(); iter++) { if (iter->second.empty()) { config_parameters[iter->first] = iter->second.data(); } else { insertParameters(iter->first, iter->second); } } } boost::property_tree::ptree PndConfigParameters::getConfigParametersPropertyTree() const { boost::property_tree::ptree config_params_ptree; std::map::const_iterator config_parameter; for (config_parameter = config_parameters.begin(); config_parameter != config_parameters.end(); ++config_parameter) { bool success(false); // check if its a bool try { bool value = lexical_cast(config_parameter->second); config_params_ptree.put(config_parameter->first, value); success = true; } catch (const bad_lexical_cast &) { } // check if its an int try { if (!success) { int value = lexical_cast(config_parameter->second); config_params_ptree.put(config_parameter->first, value); success = true; } } catch (const bad_lexical_cast &) { } // check if its a double try { if (!success) { double value = lexical_cast(config_parameter->second); config_params_ptree.put(config_parameter->first, value); success = true; } } catch (const bad_lexical_cast &) { } // otherwise just take it as a string config_params_ptree.put(config_parameter->first, config_parameter->second); } return config_params_ptree; } template T PndConfigParameters::getParameterValueByName( const std::string ¶m_name) const { T value; std::map::const_iterator search_result = config_parameters.find(param_name); if (search_result != config_parameters.end()) { // check if its a double try { value = lexical_cast(search_result->second); } catch (const bad_lexical_cast &) { std::cout << "ERROR: parameter with name " << search_result->first << " exists, but has value " << search_result->second << " which seems not to be the type you requested." << " Returning a default instanciated variable of this type!" << std::endl; } } return value; } template int PndConfigParameters::getParameterValueByName( const std::string ¶m_name) const; template bool PndConfigParameters::getParameterValueByName( const std::string ¶m_name) const; template double PndConfigParameters::getParameterValueByName( const std::string ¶m_name) const; template std::string PndConfigParameters::getParameterValueByName( const std::string ¶m_name) const;