diff --git a/README.md b/README.md index 93c0a13..222de08 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}}; } @@ -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 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) { { diff --git a/toml/get.hpp b/toml/get.hpp index 4cdbe81..d2c178a 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&); // ============================================================================