From 12d4648dbc72f0261e8b515e85d9ad59ce6f61d4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Dec 2016 13:45:32 +0000 Subject: Pass format string as template argument into writer to avoid copying out bytes from stream write --- libadhocutil/compileTimeFormatter.h | 135 ++++++++++----------- .../unittests/testCompileTimeFormatter.cpp | 12 +- 2 files changed, 71 insertions(+), 76 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 1e82956..1a09bb1 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -5,115 +5,110 @@ namespace AdHoc { constexpr int WRAP_AT = 120; - template struct Buffer { }; + template struct Buffer { }; - template + template struct Upto { template - static auto stuff(stream &, const Buffer & f) + static auto stuff(stream &, const Buffer & f) { return f; } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - return Upto::stuff(s, Buffer()); + return Upto::stuff(s, Buffer()); } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - char buf[] = {sm...}; - s.write(buf, sizeof...(sm)); - return Buffer(); + s.write(S + start, sizeof...(sm)); + return Buffer(); } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - char buf[] = {sm...}; - s.write(buf, sizeof...(sm)); - return Buffer(); + s.write(S + start, sizeof...(sm)); + return Buffer(); } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - char buf[] = {sm...}; - s.write(buf, sizeof...(sm)); - return Buffer(); + s.write(S + start, sizeof...(sm)); + return Buffer(); } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - char buf[] = {sm...}; - s.write(buf, sizeof...(sm)); - return Buffer(); + s.write(S + start, sizeof...(sm)); + return Buffer(); } }; - template - struct Upto { - template - static auto stuff(stream & s, const Buffer &) + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) { - char buf[] = {sm...}; - s.write(buf, sizeof...(sm)); - return Buffer(); + s.write(S + start, sizeof...(sm)); + return Buffer(); } }; - template + template struct StreamWriter { template static void write(stream & s, const Pn & ... pn) { - next(s, Upto<'%', 0, sn...>::stuff(s, Buffer()), pn...); + next(s, Upto::stuff(s, Buffer()), pn...); } - template class Buffer> - static void next(stream & s, const Buffer&, const Pn & ... pn) + template class Buffer> + static void next(stream & s, const Buffer&, const Pn & ... pn) { - StreamWriter::write(s, pn...); + StreamWriter::write(s, pn...); } }; - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream &, const Pn & ...) { } }; - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream &, const Pn & ...) { } }; // Default stream writer formatter - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream & s, const P & p, const Pn & ... pn) { s << p; - StreamWriter::write(s, pn...); + StreamWriter::write(s, pn...); } }; // Unknown stream writer formatter - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream &, const Pn & ...) { @@ -133,19 +128,19 @@ namespace AdHoc { return Parser::innerparse(); } template - static auto append(const Buffer & b) + static auto append(const Buffer & b) { return join(b, Parser::parse()); } template - static auto append(const Buffer & b) + static auto append(const Buffer & b) { return b; } template - static auto join(const Buffer &, const Buffer &) + static auto join(const Buffer &, const Buffer &) { - return Buffer(); + return Buffer(); } }; @@ -153,11 +148,11 @@ namespace AdHoc { struct Parser { static auto parse() { - return Buffer(); + return Buffer(); } static auto innerparse() { - return Buffer(); + return Buffer(); } }; @@ -165,11 +160,11 @@ namespace AdHoc { struct Parser { static auto parse() { - return Buffer(); + return Buffer(); } static auto innerparse() { - return Buffer(); + return Buffer(); } }; @@ -177,11 +172,11 @@ namespace AdHoc { struct Parser { static auto parse() { - return Buffer(); + return Buffer(); } static auto innerparse() { - return Buffer(); + return Buffer(); } }; @@ -193,10 +188,10 @@ namespace AdHoc { run(Parser::parse(), s, pn...); } - template class Buffer, typename ... Pn> - static void run(const Buffer &, stream & s, const Pn & ... pn) + template class Buffer, typename ... Pn> + static void run(const Buffer &, stream & s, const Pn & ... pn) { - StreamWriter::write(s, pn...); + StreamWriter::write(s, pn...); } }; } diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index 00f1e6f..ffb528b 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -90,19 +90,19 @@ BOOST_AUTO_TEST_CASE ( multi ) namespace AdHoc { // Custom stream writer formatter, formats as (bracketed expression) - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream & s, const P & p, const Pn & ... pn) { s << "-( " << p << " )-"; - StreamWriter::write(s, pn...); + StreamWriter::write(s, pn...); } }; // Custom stream writer formatter, formats // right-aligned by given width - template - struct StreamWriter { + template + struct StreamWriter { template static void write(stream & s, int width, const P & p, const Pn & ... pn) { @@ -110,7 +110,7 @@ namespace AdHoc { buf << p; std::string spaces(width - buf.str().length(), ' '); s << spaces << buf.str(); - StreamWriter::write(s, pn...); + StreamWriter::write(s, pn...); } }; } -- cgit v1.2.3