Files
toml11/toml/to_toml.hpp

82 lines
2.0 KiB
C++
Raw Permalink Normal View History

2017-04-21 13:14:53 +09:00
#ifndef TOML11_TO_TOML
#define TOML11_TO_TOML
#include "value.hpp"
namespace toml
{
2018-05-05 13:09:40 +09:00
template<typename T, typename std::enable_if<
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
inline value to_toml(const T& x)
2017-04-21 13:14:53 +09:00
{
2018-05-05 13:09:40 +09:00
return value(x);
2017-04-21 13:14:53 +09:00
}
2018-05-05 13:09:40 +09:00
template<typename T, typename std::enable_if<detail::conjunction<
detail::negation<detail::is_exact_toml_type<T>>, std::is_integral<T>
>::value, std::nullptr_t>::type = nullptr>
inline value to_toml(const T& x)
2017-04-21 13:14:53 +09:00
{
2018-05-05 13:09:40 +09:00
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)));
2017-04-21 13:14:53 +09:00
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
2018-05-05 13:09:40 +09:00
{
2017-04-21 13:14:53 +09:00
tmp.emplace_back(*iter);
2018-05-05 13:09:40 +09:00
}
return value(std::move(tmp));
2017-04-21 13:14:53 +09:00
}
2018-05-05 13:09:40 +09:00
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>
value to_toml(const T& x)
2017-04-21 13:14:53 +09:00
{
2018-05-05 13:09:40 +09:00
Table tmp;
2017-04-21 13:14:53 +09:00
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
2018-05-05 13:09:40 +09:00
{
2017-04-21 13:14:53 +09:00
tmp.emplace(iter->first, to_toml(iter->second));
2018-05-05 13:09:40 +09:00
}
return value(std::move(tmp));
2017-04-21 13:14:53 +09:00
}
template<typename T>
2018-05-05 13:09:40 +09:00
inline value to_toml(std::initializer_list<T> init)
{
return value(std::move(init));
}
inline value to_toml(std::initializer_list<std::pair<std::string, value>> init)
2017-04-21 13:14:53 +09:00
{
2018-05-05 13:09:40 +09:00
return value(std::move(init));
2017-04-21 13:14:53 +09:00
}
2018-05-05 13:09:40 +09:00
template<typename T>
inline value to_toml(const value& x)
2017-04-21 13:14:53 +09:00
{
2018-05-05 13:09:40 +09:00
return x;
2017-04-21 13:14:53 +09:00
}
} // toml
#endif // TOML11_TO_TOML