diff --git a/tests/test_get.cpp b/tests/test_get.cpp index b8d8477..728efab 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -10,7 +10,7 @@ #include #include #include - +#include BOOST_AUTO_TEST_CASE(test_get_exact) @@ -86,33 +86,27 @@ BOOST_AUTO_TEST_CASE(test_get_cast) toml::value v6(a); toml::value v7(t); - std::size_t u2 = toml::get(v2); - float u3 = toml::get(v3); - std::deque u4 = toml::get>(v6); - std::list u5 = toml::get >(v6); - std::map u6 = toml::get>(v7); + const auto u2 = toml::get(v2); + const auto u3 = toml::get(v3); + const auto u4 = toml::get>(v6); + const auto u5 = toml::get >(v6); + const auto u6 = toml::get>(v6); + std::map u7 = toml::get>(v7); - std::deque r4; - r4.push_back(2); - r4.push_back(7); - r4.push_back(1); - r4.push_back(8); - r4.push_back(2); - std::list r5; - r5.push_back(2); - r5.push_back(7); - r5.push_back(1); - r5.push_back(8); - r5.push_back(2); + std::deque r4{2,7,1,8,2}; + std::list r5{2,7,1,8,2}; + std::array r6{{2,7,1,8,2}}; BOOST_CHECK_EQUAL(u2, 42ul); BOOST_CHECK_CLOSE_FRACTION(u3, 3.14, 1e-3); const bool dq = r4 == u4; const bool ls = r5 == u5; + const bool ar = r6 == u6; BOOST_CHECK(dq); BOOST_CHECK(ls); - BOOST_CHECK_EQUAL(u6.at("val1").cast(), true); - BOOST_CHECK_EQUAL(u6.at("val2").cast(), 42); - BOOST_CHECK_CLOSE_FRACTION(u6.at("val3").cast(),3.14, 1e-3); - BOOST_CHECK_EQUAL(u6.at("val4").cast(), "piyo"); + BOOST_CHECK(ar); + BOOST_CHECK_EQUAL(u7.at("val1").cast(), true); + BOOST_CHECK_EQUAL(u7.at("val2").cast(), 42); + BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast(),3.14, 1e-3); + BOOST_CHECK_EQUAL(u7.at("val4").cast(), "piyo"); } diff --git a/toml/get.hpp b/toml/get.hpp index f93a3fc..ea32216 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -1,7 +1,7 @@ #ifndef TOML11_GET #define TOML11_GET #include "value.hpp" -#include "from_toml.hpp" +#include namespace toml { @@ -14,6 +14,7 @@ inline T get(const toml::value& v) return static_cast(v.cast()); } +// array case template(), typename std::enable_if<(vT == toml::value_t::Unknown) && (!toml::detail::is_map::value) && @@ -21,10 +22,20 @@ template(), T get(const toml::value& v) { if(v.type() != value_t::Array) - throw type_error("from_toml: value type: " + stringize(v.type()) + + throw type_error("get: value type: " + stringize(v.type()) + std::string(" is not argument type: Array")); + const auto& ar = v.cast(); T tmp; - from_toml(tmp, v); + try + { + toml::resize(tmp, ar.size()); + } + catch(std::invalid_argument& iv) + { + throw toml::type_error("toml::get: static array size is not enough"); + } + std::transform(ar.cbegin(), ar.cend(), tmp.begin(), + [](toml::value const& elem){return get(elem);}); return tmp; } @@ -34,10 +45,11 @@ template(), T get(const toml::value& v) { if(v.type() != value_t::Table) - throw type_error("from_toml: value type: " + stringize(v.type()) + + throw type_error("get: value type: " + stringize(v.type()) + std::string(" is not argument type: Table")); T tmp; - from_toml(tmp, v); + const auto& tb = v.cast(); + for(const auto& kv : tb){tmp.insert(kv);} return tmp; }