From 6a7dbb787570460675b50af9bd60765752d4bd6b Mon Sep 17 00:00:00 2001 From: "J.C. Moyer" Date: Thu, 3 Oct 2019 21:25:24 -0400 Subject: [PATCH 1/4] Update documentation for toml::from and toml::into --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 93c0a13..8cd611d 100644 --- a/README.md +++ b/README.md @@ -1091,7 +1091,7 @@ namespace toml template<> struct from { - ext::foo from_toml(const value& v) + static ext::foo from_toml(const value& v) { ext::foo f; f.a = find(v, "a"); @@ -1131,7 +1131,7 @@ template<> struct from { template class M, template class A> - ext::foo from_toml(const basic_value& v) + static ext::foo from_toml(const basic_value& v) { ext::foo f; f.a = find(v, "a"); @@ -1187,7 +1187,7 @@ namespace toml template<> struct into { - toml::table into_toml(const ext::foo& f) + static toml::table into_toml(const ext::foo& f) { return toml::table{{"a", f.a}, {"b", f.b}, {"c", f.c}}; } From 488015df492daa295ca54ef814b0bccd19bf5e93 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 4 Oct 2019 12:58:32 +0900 Subject: [PATCH 2/4] fix: toml -> T is required; related to #83 --- toml/get.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toml/get.hpp b/toml/get.hpp index 16516c1..7445ff0 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -256,7 +256,7 @@ get(const basic_value&); // toml::from::from_toml(v) template class M, template class V, - std::size_t S = sizeof(::toml::into)> + std::size_t S = sizeof(::toml::from)> T get(const basic_value&); // ============================================================================ From 36af02cb3a4a2997a7ee3ef95a275d919b1858eb Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 4 Oct 2019 13:01:15 +0900 Subject: [PATCH 3/4] test: add test cases for one-way conversion --- tests/test_extended_conversions.cpp | 96 +++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/tests/test_extended_conversions.cpp b/tests/test_extended_conversions.cpp index 99adeea..77a3600 100644 --- a/tests/test_extended_conversions.cpp +++ b/tests/test_extended_conversions.cpp @@ -33,6 +33,17 @@ struct bar return toml::table{{"a", this->a}, {"b", this->b}}; } }; + +struct baz +{ + int a; + std::string b; +}; +struct qux +{ + int a; + std::string b; +}; } // extlib namespace toml @@ -54,6 +65,24 @@ struct into return toml::table{{"a", f.a}, {"b", f.b}}; } }; + +template<> +struct from +{ + static extlib::baz from_toml(const toml::value& v) + { + return extlib::baz{toml::find(v, "a"), toml::find(v, "b")}; + } +}; + +template<> +struct into +{ + static toml::table into_toml(const extlib::qux& f) + { + return toml::table{{"a", f.a}, {"b", f.b}}; + } +}; } // toml // --------------------------------------------------------------------------- @@ -83,6 +112,16 @@ struct bar return toml::table{{"a", this->a}, {"b", this->b}}; } }; +struct baz +{ + int a; + std::string b; +}; +struct qux +{ + int a; + std::string b; +}; } // extlib2 namespace toml @@ -105,6 +144,28 @@ struct into return toml::table{{"a", f.a}, {"b", f.b}}; } }; + +template<> +struct from +{ + template class M, template class A> + static extlib2::baz from_toml(const toml::basic_value& v) + { + return extlib2::baz{toml::find(v, "a"), toml::find(v, "b")}; + } +}; + +template<> +struct into +{ + static toml::basic_value + into_toml(const extlib2::qux& f) + { + return toml::basic_value{ + {"a", f.a}, {"b", f.b} + }; + } +}; } // toml // --------------------------------------------------------------------------- @@ -188,6 +249,41 @@ BOOST_AUTO_TEST_CASE(test_conversion_by_specialization) } } +BOOST_AUTO_TEST_CASE(test_conversion_one_way) +{ + { + const toml::value v{{"a", 42}, {"b", "baz"}}; + + const auto baz = toml::get(v); + BOOST_TEST(baz.a == 42); + BOOST_TEST(baz.b == "baz"); + } + { + const extlib::qux q{42, "qux"}; + const toml::value v(q); + + BOOST_TEST(toml::find(v, "a") == 42); + BOOST_TEST(toml::find(v, "b") == "qux"); + } + + { + const toml::basic_value v{ + {"a", 42}, {"b", "baz"} + }; + + const auto baz = toml::get(v); + BOOST_TEST(baz.a == 42); + BOOST_TEST(baz.b == "baz"); + } + { + const extlib::qux q{42, "qux"}; + const toml::basic_value v(q); + + BOOST_TEST(toml::find(v, "a") == 42); + BOOST_TEST(toml::find(v, "b") == "qux"); + } +} + BOOST_AUTO_TEST_CASE(test_recursive_conversion) { { From e12fd4d944955dc432ea4854c7df47dfc176276a Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 4 Oct 2019 14:28:43 +0900 Subject: [PATCH 4/4] doc: add contributors --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8cd611d..222de08 100644 --- a/README.md +++ b/README.md @@ -1492,6 +1492,8 @@ I appreciate the help of the contributors who introduced the great feature to th - Fixed warnings while type conversion - @KerstinKeller - Added installation script to CMake +- J.C. Moyer (@jcmoyer) + - Fixed an example code in the documentation ## Licensing terms