From b853860a8f6f555f1084a06a624e136b945f4e5b Mon Sep 17 00:00:00 2001 From: xep Date: Sat, 20 Jul 2024 10:44:45 +0800 Subject: [PATCH 1/3] Add: template-version into_toml (Github issue #255) --- include/toml11/traits.hpp | 11 +++++++++++ include/toml11/value.hpp | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/toml11/traits.hpp b/include/toml11/traits.hpp index 0a0fa79..f201140 100644 --- a/include/toml11/traits.hpp +++ b/include/toml11/traits.hpp @@ -83,6 +83,14 @@ struct has_into_toml_method_impl static std::false_type check(...); }; +struct has_template_into_toml_method_impl +{ + template + static std::true_type check(decltype(std::declval().template into_toml())*); + template + static std::false_type check(...); +}; + struct has_specialized_from_impl { template @@ -126,6 +134,9 @@ struct has_from_toml_method: decltype(has_from_toml_method_impl::check(nu template struct has_into_toml_method: decltype(has_into_toml_method_impl::check(nullptr)){}; +template +struct has_template_into_toml_method: decltype(has_template_into_toml_method_impl::check(nullptr)){}; + template struct has_specialized_from: decltype(has_specialized_from_impl::check(nullptr)){}; template diff --git a/include/toml11/value.hpp b/include/toml11/value.hpp index 6ac4319..e635135 100644 --- a/include/toml11/value.hpp +++ b/include/toml11/value.hpp @@ -1085,6 +1085,29 @@ class basic_value *this = ud.into_toml(); return *this; } + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud): basic_value(ud.template into_toml()) {} + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud, std::vector com) + : basic_value(ud.template into_toml(), std::move(com)) + {} + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& ud) + { + *this = ud.template into_toml(); + return *this; + } // }}} // empty value with region info ======================================= {{{ From ce42e78e3cc75d49116cf3f3fb7d963027c2fab7 Mon Sep 17 00:00:00 2001 From: xep Date: Sat, 20 Jul 2024 11:15:30 +0800 Subject: [PATCH 2/3] add test for template into_toml --- tests/test_user_defined_conversion.cpp | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_user_defined_conversion.cpp b/tests/test_user_defined_conversion.cpp index e8941ae..e7f8520 100644 --- a/tests/test_user_defined_conversion.cpp +++ b/tests/test_user_defined_conversion.cpp @@ -49,6 +49,25 @@ struct foobar int a; std::string b; }; + +struct corge +{ + int a; + std::string b; + + void from_toml(const toml::value& v) + { + this->a = toml::find(v, "a"); + this->b = toml::find(v, "b"); + return ; + } + + template + toml::basic_value into_toml() const + { + return toml::basic_value(typename toml::basic_value::table_type{{"a", this->a}, {"b", this->b}}); + } +}; } // extlib namespace toml @@ -215,6 +234,19 @@ TEST_CASE("test_conversion_by_member_methods") CHECK(v == v2); } + + { + const toml::value v(toml::table{{"a", 42}, {"b", "baz"}}); + + const auto corge = toml::get(v); + CHECK_EQ(corge.a, 42); + CHECK_EQ(corge.b, "baz"); + + const toml::value v2(corge); + + CHECK_EQ(v, v2); + } + { const toml::ordered_value v(toml::ordered_table{{"a", 42}, {"b", "baz"}}); From f7deb1e89e49f0e84b45988ccd0973ebbb5b95b0 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 20 Jul 2024 03:15:58 +0000 Subject: [PATCH 3/3] feat [skip ci]: update single_include --- single_include/toml.hpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/single_include/toml.hpp b/single_include/toml.hpp index f43cfd2..78e8603 100644 --- a/single_include/toml.hpp +++ b/single_include/toml.hpp @@ -3577,6 +3577,14 @@ struct has_into_toml_method_impl static std::false_type check(...); }; +struct has_template_into_toml_method_impl +{ + template + static std::true_type check(decltype(std::declval().template into_toml())*); + template + static std::false_type check(...); +}; + struct has_specialized_from_impl { template @@ -3620,6 +3628,9 @@ struct has_from_toml_method: decltype(has_from_toml_method_impl::check(nu template struct has_into_toml_method: decltype(has_into_toml_method_impl::check(nullptr)){}; +template +struct has_template_into_toml_method: decltype(has_template_into_toml_method_impl::check(nullptr)){}; + template struct has_specialized_from: decltype(has_specialized_from_impl::check(nullptr)){}; template @@ -6660,6 +6671,29 @@ class basic_value *this = ud.into_toml(); return *this; } + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud): basic_value(ud.template into_toml()) {} + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud, std::vector com) + : basic_value(ud.template into_toml(), std::move(com)) + {} + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& ud) + { + *this = ud.template into_toml(); + return *this; + } // }}} // empty value with region info ======================================= {{{