From 891f68eab04a0382df77c6a3698c0e1d5855c0f3 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 15 May 2021 20:41:11 +0900 Subject: [PATCH] feat: support all &/const&/&& variants --- toml/get.hpp | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/toml/get.hpp b/toml/get.hpp index f462368..6669e34 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -1038,25 +1038,48 @@ find_or(const basic_value& v, const toml::key& ky, T&& opt) return get_or(tab.at(ky), std::forward(opt)); } -template class M, template class V, - typename ... Ks, +// --------------------------------------------------------------------------- +// recursive find-or with type deduction (find_or(value, keys, opt)) + +template 1), std::nullptr_t> = nullptr> // here we need to add SFINAE in the template parameter to avoid // infinite recursion in type deduction on gcc -auto find_or(const basic_value& v, const toml::key& ky, Ks&& ... keys) - -> decltype(find_or(v, ky, detail::last_one(std::forward(keys)...))) +auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys) + -> decltype(find_or(std::forward(v), ky, detail::last_one(std::forward(keys)...))) { if(!v.is_table()) { return detail::last_one(std::forward(keys)...); } - const auto& tab = v.as_table(); + auto&& tab = std::forward(v).as_table(); if(tab.count(ky) == 0) { return detail::last_one(std::forward(keys)...); } - return find_or(tab.at(ky), std::forward(keys)...); + return find_or(std::forward(tab).at(ky), std::forward(keys)...); +} + +// --------------------------------------------------------------------------- +// recursive find_or with explicit type specialization, find_or(value, keys...) + +template 1), std::nullptr_t> = nullptr> + // here we need to add SFINAE in the template parameter to avoid + // infinite recursion in type deduction on gcc +auto find_or(Value&& v, const toml::key& ky, Ks&& ... keys) + -> decltype(find_or(std::forward(v), ky, detail::last_one(std::forward(keys)...))) +{ + if(!v.is_table()) + { + return detail::last_one(std::forward(keys)...); + } + auto&& tab = std::forward(v).as_table(); + if(tab.count(ky) == 0) + { + return detail::last_one(std::forward(keys)...); + } + return find_or(std::forward(tab).at(ky), std::forward(keys)...); } // ============================================================================