diff --git a/tests/test_from_toml.cpp b/tests/test_from_toml.cpp index 117bed7..b3953b3 100644 --- a/tests/test_from_toml.cpp +++ b/tests/test_from_toml.cpp @@ -170,3 +170,22 @@ BOOST_AUTO_TEST_CASE(test_from_toml_tie) BOOST_CHECK_EQUAL(ut["val4"].cast(), "piyo"); } +BOOST_AUTO_TEST_CASE(test_from_toml_tuple) +{ + toml::Array a; + a.emplace_back(2); + a.emplace_back(7); + a.emplace_back(1); + a.emplace_back(8); + a.emplace_back(2); + toml::value v(a); + + std::tuple t; + toml::from_toml(t, v); + + BOOST_CHECK_EQUAL(std::get<0>(t), 2); + BOOST_CHECK_EQUAL(std::get<1>(t), 7); + BOOST_CHECK_EQUAL(std::get<2>(t), 1); + BOOST_CHECK_EQUAL(std::get<3>(t), 8); + BOOST_CHECK_EQUAL(std::get<4>(t), 2); +} diff --git a/toml/from_toml.hpp b/toml/from_toml.hpp index 8014110..d9d8561 100644 --- a/toml/from_toml.hpp +++ b/toml/from_toml.hpp @@ -1,66 +1,14 @@ #ifndef TOML11_FROM_TOML #define TOML11_FROM_TOML -#include "value.hpp" +#include "get.hpp" namespace toml { -template(), - typename std::enable_if<(vT != toml::value_t::Unknown && - vT != value_t::Empty), std::nullptr_t>::type = nullptr> +template void from_toml(T& x, const toml::value& v) { - if(v.type() != vT) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not arguemnt type: ") + stringize(vT)); - x = v.cast(); - return; -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - (!toml::detail::is_map::value) && - toml::detail::is_container::value, std::nullptr_t>::type = nullptr> -void from_toml(T& x, const toml::value& v) -{ - // TODO the case of x is not dynamic container case - if(v.type() != value_t::Array) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Array")); - const auto& ar = v.cast(); - try - { - toml::resize(x, ar.size()); - } - catch(std::invalid_argument& iv) - { - throw toml::type_error("toml::from_toml: static array size is not enough"); - } - auto iter = x.begin(); - for(const auto& val : ar) - { - typename T::value_type v; - from_toml(v, val); - *iter = std::move(v); - ++iter; - } - return; -} - -template(), - typename std::enable_if<(vT == toml::value_t::Unknown) && - toml::detail::is_map::value, std::nullptr_t>::type = nullptr> -void from_toml(T& x, const toml::value& v) -{ - if(v.type() != value_t::Table) - throw type_error("from_toml: value type: " + stringize(v.type()) + - std::string(" is not argument type: Table")); - x.clear(); - const auto& tb = v.cast(); - for(const auto& kv : tb) - { - x.insert(kv); - } + x = toml::get(v); return; }