From 2fb8793f1af4a4c1727eca7780597ce5cafa6629 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 29 Dec 2020 18:52:07 +0900 Subject: [PATCH] doc: add document about basic_value and toml::into related to #146. --- README.md | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4bd139c..fcc4b0e 100644 --- a/README.md +++ b/README.md @@ -1303,9 +1303,9 @@ struct foo double b; std::string c; - toml::table into_toml() const // you need to mark it const. + toml::value into_toml() const // you need to mark it const. { - return toml::table{{"a", this->a}, {"b", this->b}, {"c", this->c}}; + return toml::value{{"a", this->a}, {"b", this->b}, {"c", this->c}}; } }; } // ext @@ -1332,9 +1332,9 @@ namespace toml template<> struct into { - static toml::table into_toml(const ext::foo& f) + static toml::value into_toml(const ext::foo& f) { - return toml::table{{"a", f.a}, {"b", f.b}, {"c", f.c}}; + return toml::value{{"a", f.a}, {"b", f.b}, {"c", f.c}}; } }; } // toml @@ -1346,6 +1346,27 @@ toml::value v(f); Any type that can be converted to `toml::value`, e.g. `int`, `toml::table` and `toml::array` are okay to return from `into_toml`. +You can also return a custom `toml::basic_value` from `toml::into`. + +```cpp +namespace toml +{ +template<> +struct into +{ + static toml::basic_value into_toml(const ext::foo& f) + { + toml::basic_value v{{"a", f.a}, {"b", f.b}, {"c", f.c}}; + v.comments().push_back(" comment"); + return v; + } +}; +} // toml +``` + +But note that, if this `basic_value` would be assigned into other `toml::value` +that discards `comments`, the comments would be dropped. + ## Formatting user-defined error messages When you encounter an error after you read the toml value, you may want to