mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 10:28:09 +08:00
simplify SFINAE expressions in toml::get
This commit is contained in:
64
toml/get.hpp
64
toml/get.hpp
@@ -6,51 +6,67 @@
|
|||||||
namespace toml
|
namespace toml
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
template<typename T, typename std::enable_if<
|
||||||
typename std::enable_if<(vT != toml::value_t::Unknown &&
|
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
|
||||||
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
|
inline T& get(value& v)
|
||||||
inline T get(const toml::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
|
// array-like type
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
template<typename T, typename std::enable_if<detail::conjunction<
|
||||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
detail::negation<detail::is_exact_toml_type<T>>, detail::is_container<T>
|
||||||
(!toml::detail::is_map<T>::value) &&
|
>::value, std::nullptr_t>::type = nullptr>
|
||||||
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
|
T get(const value& v)
|
||||||
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>();
|
const auto& ar = v.cast<value_t::Array>();
|
||||||
T tmp;
|
T tmp;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
toml::resize(tmp, ar.size());
|
::toml::resize(tmp, ar.size());
|
||||||
}
|
}
|
||||||
catch(std::invalid_argument& iv)
|
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(),
|
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;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// table-like case
|
// table-like case
|
||||||
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
|
template<typename T, typename std::enable_if<detail::conjunction<
|
||||||
typename std::enable_if<(vT == toml::value_t::Unknown) &&
|
detail::negation<detail::is_exact_toml_type<T>>, detail::is_map<T>
|
||||||
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
|
>::value, std::nullptr_t>::type = nullptr>
|
||||||
T get(const toml::value& v)
|
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>();
|
const auto& tb = v.cast<value_t::Table>();
|
||||||
|
T tmp;
|
||||||
for(const auto& kv : tb){tmp.insert(kv);}
|
for(const auto& kv : tb){tmp.insert(kv);}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user