From ffd9feb344bb9d3f543d41cf5d40cd0baf256e8b Mon Sep 17 00:00:00 2001 From: Dmitri Naumov <dmitri.naumov@ufz.de> Date: Wed, 30 Mar 2016 20:05:23 +0000 Subject: [PATCH] [BL] Add ConfigTree::getConfParam* for vector<T>. --- BaseLib/ConfigTree-impl.h | 34 +++++++++++++++++++++++++++++----- BaseLib/ConfigTree.h | 10 ++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/BaseLib/ConfigTree-impl.h b/BaseLib/ConfigTree-impl.h index 5e83580f3a9..6ea8d4a5fac 100644 --- a/BaseLib/ConfigTree-impl.h +++ b/BaseLib/ConfigTree-impl.h @@ -9,6 +9,8 @@ #include "ConfigTree.h" +#include <sstream> + namespace BaseLib { @@ -50,15 +52,37 @@ getConfParam(std::string const& param, T const& default_value) const return default_value; } -template<typename T> -boost::optional<T> -ConfigTree:: -getConfParamOptional(std::string const& param) const +template <typename T> +boost::optional<T> ConfigTree::getConfParamOptional( + std::string const& param) const { checkUnique(param); + return getConfParamOptionalImpl(param, static_cast<T*>(nullptr)); +} + +template <typename T> +boost::optional<T> ConfigTree::getConfParamOptionalImpl( + std::string const& param, T*) const +{ + if (auto p = getConfSubtreeOptional(param)) return p->getValue<T>(); + + return boost::none; +} + +template <typename T> +boost::optional<std::vector<T>> ConfigTree::getConfParamOptionalImpl( + std::string const& param, std::vector<T>*) const +{ if (auto p = getConfSubtreeOptional(param)) - return p->getValue<T>(); + { + std::istringstream sstr{p->getValue<std::string>()}; + std::vector<T> result; + T value; + while (sstr >> value) + result.push_back(value); + return boost::make_optional(result); + } return boost::none; } diff --git a/BaseLib/ConfigTree.h b/BaseLib/ConfigTree.h index 90a60270b40..e4041229fe1 100644 --- a/BaseLib/ConfigTree.h +++ b/BaseLib/ConfigTree.h @@ -15,6 +15,7 @@ #include <functional> #include <memory> +#include <vector> #include <boost/property_tree/ptree.hpp> @@ -497,6 +498,15 @@ public: static void onwarning(std::string const& filename, std::string const& path, std::string const& message); +private: + //! Default implementation of reading a value of type T. + template<typename T> boost::optional<T> + getConfParamOptionalImpl(std::string const& param, T*) const; + + //! Implementation of reading a vector of values of type T. + template<typename T> boost::optional<std::vector<T>> + getConfParamOptionalImpl(std::string const& param, std::vector<T>*) const; + private: struct CountType { -- GitLab