mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
add get_or
This commit is contained in:
15
toml/get.hpp
15
toml/get.hpp
@@ -14,7 +14,7 @@ inline T get(const toml::value& v)
|
||||
return static_cast<T>(v.cast<vT>());
|
||||
}
|
||||
|
||||
// array case
|
||||
// array-like type
|
||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
||||
(!toml::detail::is_map<T>::value) &&
|
||||
@@ -24,6 +24,7 @@ T get(const toml::value& v)
|
||||
if(v.type() != value_t::Array)
|
||||
throw type_error("get: value type: " + stringize(v.type()) +
|
||||
std::string(" is not argument type: Array"));
|
||||
|
||||
const auto& ar = v.cast<value_t::Array>();
|
||||
T tmp;
|
||||
try
|
||||
@@ -39,6 +40,7 @@ T get(const toml::value& v)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// table-like case
|
||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
||||
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
|
||||
@@ -53,5 +55,16 @@ T get(const toml::value& v)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// get_or -----------------------------------------------------------------
|
||||
|
||||
template<typename T>
|
||||
inline typename std::remove_cv<typename std::remove_reference<T>::type>::type
|
||||
get_or(const toml::Table& tab, const toml::key& ky, T&& opt)
|
||||
{
|
||||
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
|
||||
return get<typename std::remove_cv<
|
||||
typename std::remove_reference<T>::type>::type>(tab.find(ky)->second);
|
||||
}
|
||||
|
||||
} // toml
|
||||
#endif// TOML11_GET
|
||||
|
Reference in New Issue
Block a user