From 6c22729770d4f5dfb8ddc0ddf0e25f1d42c63bdc Mon Sep 17 00:00:00 2001 From: "Dmitry Yu. Naumov" <github@naumov.de> Date: Mon, 17 Sep 2018 18:43:41 +0200 Subject: [PATCH] [BL] Add getConfigAttribute with default value. Update documentation mismatches in other getConfigAttribute*(). --- BaseLib/ConfigTree-impl.h | 10 ++++++++++ BaseLib/ConfigTree.h | 24 +++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/BaseLib/ConfigTree-impl.h b/BaseLib/ConfigTree-impl.h index 4256d180a8d..ed4cc075f75 100644 --- a/BaseLib/ConfigTree-impl.h +++ b/BaseLib/ConfigTree-impl.h @@ -187,6 +187,16 @@ getConfigAttribute(std::string const& attr) const error("Did not find XML attribute with name \"" + attr + "\"."); } +template <typename T> +T ConfigTree::getConfigAttribute(std::string const& attr, + T const& default_value) const +{ + if (auto a = getConfigAttributeOptional<T>(attr)) + return *a; + + return default_value; +} + template<typename T> boost::optional<T> ConfigTree:: diff --git a/BaseLib/ConfigTree.h b/BaseLib/ConfigTree.h index 8f30c13bc53..654a9a3c858 100644 --- a/BaseLib/ConfigTree.h +++ b/BaseLib/ConfigTree.h @@ -364,18 +364,32 @@ public: /*! Get XML attribute \c attr of type \c T for the current parameter. * - * \return the requested attribute + * \return the requested attribute's value. * - * \pre \c param must not have been read before from this ConfigTree. + * \pre \c attr must not have been read before from the current parameter. */ template<typename T> T getConfigAttribute(std::string const& attr) const; - /*! Get XML attribute \c attr of type \c T for the current parameter if present. + /*! Get XML attribute \c attr of type \c T for the current parameter or the + * \c default_value. * - * \return the requested attribute + * This method has a similar behaviour as getConfigAttribute(std::string + * const&) except in case of errors the \c default_value is returned. * - * \pre \c param must not have been read before from this ConfigTree. + * \return the requested attribute's value. + * + * \pre \c attr must not have been read before from the current parameter. + */ + template <typename T> + T getConfigAttribute(std::string const& attr, T const& default_value) const; + + /*! Get XML attribute \c attr of type \c T for the current parameter if + * present. + * + * \return the requested attribute's value. + * + * \pre \c attr must not have been read before from the current parameter. */ template<typename T> boost::optional<T> getConfigAttributeOptional(std::string const& attr) const; -- GitLab