From affa159c826b69aea0aff901b8ce745fe81b1a78 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 13 Dec 2018 01:29:23 +0900 Subject: [PATCH] add get_or(value, key, opt) --- toml/get.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/toml/get.hpp b/toml/get.hpp index c2aece5..69c7c0f 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -398,6 +398,7 @@ get_or(toml::value&& v, T&& opt) } } + template auto get_or(const toml::table& tab, const toml::key& ky, T&& opt) -> decltype(get_or(std::declval(), std::forward(opt))) @@ -420,6 +421,33 @@ auto get_or(toml::table&& tab, const toml::key& ky, T&& opt) return ::toml::get_or(std::move(tab[ky]), std::forward(opt)); } +template +auto get_or(const toml::value& v, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) +{ + if(v.type() != toml::value_t::Table){return std::forward(opt);} + const auto& tab = toml::get(v); + if(tab.count(ky) == 0) {return std::forward(opt);} + return ::toml::get_or(tab.at(ky), std::forward(opt)); +} +template +auto get_or(toml::value& v, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) +{ + if(v.type() != toml::value_t::Table){return std::forward(opt);} + auto& tab = toml::get(v); + if(tab.count(ky) == 0) {return std::forward(opt);} + return ::toml::get_or(tab[ky], std::forward(opt)); +} +template +auto get_or(toml::value&& v, const toml::key& ky, T&& opt) + -> decltype(get_or(std::declval(), std::forward(opt))) +{ + if(v.type() != toml::value_t::Table){return std::forward(opt);} + auto tab = toml::get(std::move(v)); + if(tab.count(ky) == 0) {return std::forward(opt);} + return ::toml::get_or(std::move(tab[ky]), std::forward(opt)); +} // ============================================================================ // expect