From be0641e2f79ee610d61549d694bc033aeddb5482 Mon Sep 17 00:00:00 2001 From: Pierre Gergondet Date: Tue, 12 Sep 2023 16:58:06 +0900 Subject: [PATCH] [mc_rtc/Configuration] Add multi-key find --- include/mc_rtc/Configuration.h | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/include/mc_rtc/Configuration.h b/include/mc_rtc/Configuration.h index c2ca2667a4..a63942ffb9 100644 --- a/include/mc_rtc/Configuration.h +++ b/include/mc_rtc/Configuration.h @@ -817,26 +817,43 @@ struct MC_RTC_UTILS_DLLAPI Configuration /*! \brief Returns the Configuration entry if it exists, std::nullopt otherwise * - * \param Key key used to store the value + * \param key key used to store the value * * \returns Configuration entry if it exists, std::nullopt otherwise */ std::optional find(const std::string & key) const; - /*! \brief Returns the value stored within the configuration if it exists, std::nullopt otherwise + /*! \brief Return the Configuration entry at (key, others...) if it exists, std::nullopt otherwise * - * \param key Key used to store the value + * This is equivalent if(auto a = cfg.find("a")) { if(auto b = cfg.find("b")) { ... } } * - * \returns Value if key is in the configuration, std::nullopt otherwise + * \param key key used to store the value + * + * \param others Keys searched one after the other + * + * \returns Configuration entry if it exists, std::nullopt otherwise + */ + template + std::optional find(const std::string & key, Args &&... others) const + { + auto out = find(key); + return out ? out->find(std::forward(others)...) : std::nullopt; + } + + /*! \brief Return the Configuration entry at (key, others...) if it exists, std::nullopt otherwise + * + * \param key key used to store the value + * + * \param others Keys searched one after the other * * \tparam T Type of the entry retrieved * - * \throws If the conversion to \tparam T fails + * \throws If the key exists but the conversion to \tparam T fails */ - template - std::optional find(const std::string & key) const + template + std::optional find(const std::string & key, Args &&... others) const { - auto maybe_cfg = find(key); + auto maybe_cfg = find(key, std::forward(others)...); if(maybe_cfg) { return maybe_cfg->operator T(); } return std::nullopt; }