simplify SFINAE in to_toml

This commit is contained in:
ToruNiina
2018-05-05 13:09:40 +09:00
parent 170b0d6b3f
commit b1a55b1331

View File

@@ -5,47 +5,76 @@
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 value to_toml(const T& x)
inline toml::value to_toml(T&& x)
{ {
return toml::value(std::forward<T>(x)); return value(x);
} }
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>>, std::is_integral<T>
(!toml::detail::is_map<T>::value) && >::value, std::nullptr_t>::type = nullptr>
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr> inline value to_toml(const T& x)
toml::value to_toml(T&& x)
{ {
toml::Array tmp; tmp.reserve(std::distance(std::begin(x), std::end(x))); return value(::toml::Integer(x));
}
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 value to_toml(const T& x)
{
return value(::toml::Float(x));
}
inline value to_toml(const char* str)
{
return value(::toml::String(str));
}
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>
value to_toml(const T& x)
{
Array tmp;
tmp.reserve(std::distance(std::begin(x), std::end(x)));
for(auto iter = std::begin(x); iter != std::end(x); ++iter) for(auto iter = std::begin(x); iter != std::end(x); ++iter)
{
tmp.emplace_back(*iter); tmp.emplace_back(*iter);
return toml::value(std::move(tmp)); }
return value(std::move(tmp));
} }
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>
toml::value to_toml(T&& x) value to_toml(const T& x)
{ {
toml::Table tmp; Table tmp;
for(auto iter = std::begin(x); iter != std::end(x); ++iter) for(auto iter = std::begin(x); iter != std::end(x); ++iter)
{
tmp.emplace(iter->first, to_toml(iter->second)); tmp.emplace(iter->first, to_toml(iter->second));
return toml::value(std::move(tmp)); }
return value(std::move(tmp));
} }
template<typename T> template<typename T>
inline toml::value to_toml(std::initializer_list<T> init) inline value to_toml(std::initializer_list<T> init)
{ {
return toml::value(std::move(init)); return value(std::move(init));
} }
inline toml::value inline value to_toml(std::initializer_list<std::pair<std::string, value>> init)
to_toml(std::initializer_list<std::pair<std::string, toml::value>> init)
{ {
return toml::value(std::move(init)); return value(std::move(init));
}
template<typename T>
inline value to_toml(const value& x)
{
return x;
} }
} // toml } // toml