mirror of
https://github.com/ToruNiina/toml11.git
synced 2024-11-12 15:50:07 +00:00
simplify SFINAE expressions in toml::get
This commit is contained in:
parent
b6f53cae7a
commit
e54deacf1a
64
toml/get.hpp
64
toml/get.hpp
@ -6,51 +6,67 @@
|
||||
namespace toml
|
||||
{
|
||||
|
||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
||||
typename std::enable_if<(vT != toml::value_t::Unknown &&
|
||||
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
|
||||
inline T get(const toml::value& v)
|
||||
template<typename T, typename std::enable_if<
|
||||
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
||||
inline T& get(value& v)
|
||||
{
|
||||
return static_cast<T>(v.cast<vT>());
|
||||
constexpr value_t kind = detail::check_type<T>();
|
||||
return v.cast<kind>();
|
||||
}
|
||||
|
||||
template<typename T, typename std::enable_if<
|
||||
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
||||
inline T const& get(const value& v)
|
||||
{
|
||||
constexpr value_t kind = detail::check_type<T>();
|
||||
return v.cast<kind>();
|
||||
}
|
||||
|
||||
template<typename T, typename std::enable_if<detail::conjunction<
|
||||
detail::negation<detail::is_exact_toml_type<T>>,
|
||||
detail::negation<std::is_same<T, bool>>, std::is_integral<T>
|
||||
>::value, std::nullptr_t>::type = nullptr>
|
||||
inline T get(const value& v)
|
||||
{
|
||||
return static_cast<T>(v.cast<value_t::Integer>());
|
||||
}
|
||||
template<typename T, typename std::enable_if<detail::conjunction<
|
||||
detail::negation<detail::is_exact_toml_type<T>>, std::is_floating_point<T>
|
||||
>::value, std::nullptr_t>::type = nullptr>
|
||||
inline T get(const value& v)
|
||||
{
|
||||
return static_cast<T>(v.cast<value_t::Float>());
|
||||
}
|
||||
|
||||
// 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) &&
|
||||
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
|
||||
T get(const toml::value& v)
|
||||
template<typename T, typename std::enable_if<detail::conjunction<
|
||||
detail::negation<detail::is_exact_toml_type<T>>, detail::is_container<T>
|
||||
>::value, std::nullptr_t>::type = nullptr>
|
||||
T get(const 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
|
||||
{
|
||||
toml::resize(tmp, ar.size());
|
||||
::toml::resize(tmp, ar.size());
|
||||
}
|
||||
catch(std::invalid_argument& iv)
|
||||
{
|
||||
throw toml::type_error("toml::get: static array size is not enough");
|
||||
throw type_error("toml::get: static array: size is not enough");
|
||||
}
|
||||
std::transform(ar.cbegin(), ar.cend(), tmp.begin(),
|
||||
[](toml::value const& elem){return get<typename T::value_type>(elem);});
|
||||
[](value const& elem){return get<typename T::value_type>(elem);});
|
||||
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>
|
||||
template<typename T, typename std::enable_if<detail::conjunction<
|
||||
detail::negation<detail::is_exact_toml_type<T>>, detail::is_map<T>
|
||||
>::value, std::nullptr_t>::type = nullptr>
|
||||
T get(const toml::value& v)
|
||||
{
|
||||
if(v.type() != value_t::Table)
|
||||
throw type_error("get: value type: " + stringize(v.type()) +
|
||||
std::string(" is not argument type: Table"));
|
||||
T tmp;
|
||||
const auto& tb = v.cast<value_t::Table>();
|
||||
T tmp;
|
||||
for(const auto& kv : tb){tmp.insert(kv);}
|
||||
return tmp;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user