From 190636b79193910aa119d508aae709474a91b7b9 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 16 Mar 2019 15:52:22 +0900 Subject: [PATCH] fix: support getting a container of external types --- tests/test_extended_conversions.cpp | 34 +++++++++++++++++++++++++++++ toml/get.hpp | 18 +++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index dd20758..0d4ff25 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -80,3 +80,37 @@ BOOST_AUTO_TEST_CASE(test_conversion_by_specialization) BOOST_TEST(v == v2); } + +BOOST_AUTO_TEST_CASE(test_recursive_conversion) +{ + const toml::value v{ + toml::table{{"a", 42}, {"b", "baz"}}, + toml::table{{"a", 43}, {"b", "qux"}}, + toml::table{{"a", 44}, {"b", "quux"}}, + toml::table{{"a", 45}, {"b", "foobar"}}, + }; + + const auto foos = toml::get>(v); + BOOST_TEST(foos.size() == 4ul); + BOOST_TEST(foos.at(0).a == 42); + BOOST_TEST(foos.at(1).a == 43); + BOOST_TEST(foos.at(2).a == 44); + BOOST_TEST(foos.at(3).a == 45); + + BOOST_TEST(foos.at(0).b == "baz"); + BOOST_TEST(foos.at(1).b == "qux"); + BOOST_TEST(foos.at(2).b == "quux"); + BOOST_TEST(foos.at(3).b == "foobar"); + + const auto bars = toml::get>(v); + BOOST_TEST(bars.size() == 4ul); + BOOST_TEST(bars.at(0).a == 42); + BOOST_TEST(bars.at(1).a == 43); + BOOST_TEST(bars.at(2).a == 44); + BOOST_TEST(bars.at(3).a == 45); + + BOOST_TEST(bars.at(0).b == "baz"); + BOOST_TEST(bars.at(1).b == "qux"); + BOOST_TEST(bars.at(2).b == "quux"); + BOOST_TEST(bars.at(3).b == "foobar"); +} diff --git a/toml/get.hpp b/toml/get.hpp index dc1b495..9d14e8c 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -174,6 +174,20 @@ template::value, std::nullptr_t>::type = nullptr> T get(const toml::value& v); +template>, // not a toml::value + detail::has_from_toml_method, // but has from_toml(toml::value) memfn + std::is_default_constructible // and default constructible + >::value, std::nullptr_t>::type = nullptr> +T get(const toml::value& v); + +template> // not a toml::value + >::value, std::nullptr_t>::type = nullptr, + std::size_t = sizeof(::toml::from) // and has from specialization + > +T get(const toml::value& v); + // ============================================================================ // array-like types; most likely STL container, like std::vector, etc. @@ -306,7 +320,7 @@ template>, // not a toml::value detail::has_from_toml_method, // but has from_toml(toml::value) memfn std::is_default_constructible // and default constructible - >::value, std::nullptr_t>::type = nullptr> + >::value, std::nullptr_t>::type> T get(const toml::value& v) { T ud; @@ -315,7 +329,7 @@ T get(const toml::value& v) } template> // not a toml::value - >::value, std::nullptr_t>::type = nullptr, + >::value, std::nullptr_t>::type, std::size_t = sizeof(::toml::from) // and has from specialization > T get(const toml::value& v)