From 0502924d250f004a976c532d5bd8ad1ff132ea32 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 28 Jun 2019 19:08:48 +0900 Subject: [PATCH] feat: add nocomment and showcomment --- toml/serializer.hpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index 95df73e..e6d8c81 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -672,6 +672,34 @@ format(const basic_value& v, std::size_t w = 80u, return visit(serializer(w, fprec, force_inline), v); } +namespace detail +{ +template +int comment_index(std::basic_ostream&) +{ + static const int index = std::ios_base::xalloc(); + return index; +} +} // detail + +template +std::basic_ostream& +nocomment(std::basic_ostream& os) +{ + // by default, it is zero. and by defalut, it shows comments. + os.iword(detail::comment_index(os)) = 1; + return os; +} + +template +std::basic_ostream& +showcomment(std::basic_ostream& os) +{ + // by default, it is zero. and by defalut, it shows comments. + os.iword(detail::comment_index(os)) = 0; + return os; +} + template class M, template class V> std::basic_ostream& @@ -682,7 +710,11 @@ operator<<(std::basic_ostream& os, const basic_value& v) const int fprec = static_cast(os.precision()); os.width(0); - if(v.is_table() && !v.comments().empty()) + // by defualt, iword is initialized byl 0. And by default, toml11 outputs + // comments. So `0` means showcomment. 1 means nocommnet. + const bool show_comment = (0 == os.iword(detail::comment_index(os))); + + if(show_comment && v.is_table() && !v.comments().empty()) { os << v.comments(); os << '\n'; // to split the file comment from the first element @@ -707,7 +739,7 @@ operator<<(std::basic_ostream& os, const basic_value& v) // // In this case, it is impossible to put comments before key-value pair. // The only way to preserve comments is to put all of them after a value. - if(!v.is_table() && !v.comments().empty()) + if(show_comment && !v.is_table() && !v.comments().empty()) { os << " #"; for(const auto& c : v.comments()) {os << c;}