From 89ca9c9b246beb89148c80a8aa65d48ee6dd0563 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 18 Feb 2018 14:15:24 -0800 Subject: [PATCH] ircd::json: Allow multiple attributes to the printer; simplify printer stack. --- ircd/json.cc | 75 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/ircd/json.cc b/ircd/json.cc index 97fecb142..5ac0dc786 100644 --- a/ircd/json.cc +++ b/ircd/json.cc @@ -39,7 +39,6 @@ namespace ircd::json using karma::long_; using karma::double_; using karma::bool_; - using karma::maxwidth; using karma::eps; using karma::attr_cast; @@ -282,18 +281,12 @@ const ircd::json::parser; struct ircd::json::printer :output { - template - bool operator()(char *&out, char *const &stop, generator&& gen, attribute&& a) const; + template + bool operator()(mutable_buffer &out, gen&&, attr&&...) const; - template - bool operator()(char *&out, char *const &stop, generator&& gen) const; - - template - bool operator()(mutable_buffer &out, args&&... a) const - { - return operator()(begin(out), end(out), std::forward(a)...); - } + template + bool operator()(mutable_buffer &out, gen&&) const; template + class... attr> bool -ircd::json::printer::operator()(char *&out, - char *const &stop, +ircd::json::printer::operator()(mutable_buffer &out, gen&& g, - attr&& a) + attr&&... a) const { - const auto throws{[&out, &stop] + const auto maxwidth { - throw print_error - { - "Failed to print attribute '%s' generator '%s' (%zd bytes in buffer)", - demangle(), - demangle(), - size_t(stop - out) - }; - }}; + karma::maxwidth(size(out)) + }; const auto gg { - maxwidth(size_t(stop - out))[std::forward(g)] | eps[throws] + maxwidth[std::forward(g)] }; - return karma::generate(out, gg, std::forward(a)); + const auto throws{[&out] + { + throw print_error + { + "Failed to print attributes '%s' generator '%s' (%zd bytes in buffer)", + demangle(), + demangle(), + size(out) + }; + }}; + + return karma::generate(begin(out), gg | eps[throws], std::forward(a)...); } template bool -ircd::json::printer::operator()(char *&out, - char *const &stop, +ircd::json::printer::operator()(mutable_buffer &out, gen&& g) const { - const auto throws{[&out, &stop] + const auto maxwidth + { + karma::maxwidth(size(out)) + }; + + const auto gg + { + maxwidth[std::forward(g)] + }; + + const auto throws{[&out] { throw print_error { "Failed to print generator '%s' (%zd bytes in buffer)", demangle(), - size_t(stop - out) + size(out) }; }}; - const auto gg - { - maxwidth(size_t(stop - out))[std::forward(g)] | eps[throws] - }; - - return karma::generate(out, gg); + return karma::generate(begin(out), gg | eps[throws]); } template