add expect<T>(toml::value)

This commit is contained in:
ToruNiina 2018-12-11 11:35:35 +09:00
parent d75a977066
commit 38135940e9

View File

@ -276,5 +276,48 @@ get_or(const toml::Table& tab, const toml::key& ky, T&& opt)
typename std::remove_reference<T>::type>::type>(tab.find(ky)->second);
}
// ============================================================================
// expect
template<typename T>
auto expect(const toml::value& v)
-> result<decltype(::toml::get<T>(v)), std::string>
{
try
{
return ok(get<T>(v));
}
catch(const type_error& te)
{
return err(te.what());
}
}
template<typename T>
auto expect(toml::value& v)
-> result<decltype(::toml::get<T>(v)), std::string>
{
try
{
return ok(get<T>(v));
}
catch(const type_error& te)
{
return err(te.what());
}
}
template<typename T>
auto expect(toml::value&& v)
-> result<decltype(::toml::get<T>(std::move(v))), std::string>
{
try
{
return ok(get<T>(std::move(v)));
}
catch(const type_error& te)
{
return err(te.what());
}
}
} // toml
#endif// TOML11_GET