diff --git a/toml/get.hpp b/toml/get.hpp index bbe1679..61e2d07 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -575,6 +575,28 @@ find(basic_value&& v, const ::toml::key& ky, Ts&& ... keys) // ============================================================================ // get_or(value, fallback) +template class M, template class V> +basic_value const& +get_or(const basic_value& v, const basic_value&) +{ + return v; +} +template class M, template class V> +basic_value& +get_or(basic_value& v, basic_value&) +{ + return v; +} +template class M, template class V> +basic_value +get_or(basic_value&& v, basic_value&&) +{ + return v; +} + // ---------------------------------------------------------------------------- // specialization for the exact toml types (return type becomes lvalue ref) @@ -726,6 +748,40 @@ get_or(const basic_value& v, T&& opt) // =========================================================================== // find_or(value, key, fallback) +template class M, template class V> +basic_value const& +find_or(const basic_value& v, const key& ky, + const basic_value& opt) +{ + if(!v.is_table()) {return opt;} + const auto& tab = v.as_table(); + if(tab.count(ky) == 0) {return opt;} + return tab.at(ky); +} + +template class M, template class V> +basic_value& +find_or(basic_value& v, const toml::key& ky, basic_value& opt) +{ + if(!v.is_table()) {return opt;} + auto& tab = v.as_table(); + if(tab.count(ky) == 0) {return opt;} + return tab[ky]; +} + +template class M, template class V> +basic_value +find_or(basic_value&& v, const toml::key& ky, basic_value&& opt) +{ + if(!v.is_table()) {return opt;} + auto tab = std::move(v).as_table(); + if(tab.count(ky) == 0) {return opt;} + return std::move(tab[ky]); +} + // --------------------------------------------------------------------------- // exact types (return type can be a reference) template