add to_toml(initializer_list)

This commit is contained in:
ToruNiina
2017-04-20 12:05:10 +09:00
parent d3578aec8e
commit fd8753612a
2 changed files with 43 additions and 2 deletions

View File

@@ -74,3 +74,32 @@ BOOST_AUTO_TEST_CASE(test_to_toml_castable)
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String >(), "hoge");
}
BOOST_AUTO_TEST_CASE(test_to_toml_initializer_list)
{
toml::value v1 = toml::to_toml({3,1,4,1,5});
toml::value v2 = toml::to_toml({{"hoge", 1}, {"piyo", 3.14}, {"fuga", "string"}});
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Table);
const auto& ar = v1.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), 5);
const auto& tb = v2.cast<toml::value_t::Table>();
BOOST_CHECK_EQUAL(tb.at("hoge").type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(tb.at("piyo").type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(tb.at("fuga").type(), toml::value_t::String);
BOOST_CHECK_EQUAL(tb.at("hoge").cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_CLOSE_FRACTION(tb.at("piyo").cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(tb.at("fuga").cast<toml::value_t::String>(), "string");
}

View File

@@ -763,7 +763,7 @@ 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>
inline toml::value to_toml(T&& x)
toml::value to_toml(T&& x)
{
toml::Array tmp; tmp.reserve(std::distance(std::begin(x), std::end(x)));
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
@@ -774,7 +774,7 @@ inline toml::value to_toml(T&& x)
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>
inline toml::value to_toml(T&& x)
toml::value to_toml(T&& x)
{
toml::Table tmp;
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
@@ -782,5 +782,17 @@ inline toml::value to_toml(T&& x)
return toml::value(std::move(tmp));
}
template<typename T>
inline toml::value to_toml(std::initializer_list<T> init)
{
return toml::value(std::move(init));
}
inline toml::value
to_toml(std::initializer_list<std::pair<std::string, toml::value>> init)
{
return toml::value(std::move(init));
}
}// toml
#endif// TOML_FOR_MODERN_CPP