From 8f9959387b37921a6fb82a7f182ba916dc0dd801 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Dec 2016 12:18:56 +0000 Subject: Bit messy and fiddly, but removes the template string length limit of (just under) compile time template-depth --- libadhocutil/compileTimeFormatter.h | 123 +++++++++++++++++---- .../unittests/testCompileTimeFormatter.cpp | 4 +- 2 files changed, 103 insertions(+), 24 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 39254e0..1e82956 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -3,42 +3,74 @@ #include namespace AdHoc { - template struct Buffer { }; + constexpr int WRAP_AT = 120; - template + template struct Buffer { }; + + 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 &) + { + return Upto::stuff(s, Buffer()); + } + }; + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) + { + char buf[] = {sm...}; + s.write(buf, sizeof...(sm)); + return Buffer(); + } + }; + template + struct Upto { + template + static auto stuff(stream & s, const Buffer &) + { + char buf[] = {sm...}; + s.write(buf, sizeof...(sm)); + return Buffer(); + } + }; template - struct Upto { + struct Upto { template - static auto stuff(stream & s, const Buffer &) + static auto stuff(stream & s, const Buffer &) { - return Upto::stuff(s, Buffer()); + char buf[] = {sm...}; + s.write(buf, sizeof...(sm)); + return Buffer(); } }; template - struct Upto { + struct Upto { template - static auto stuff(stream & s, const Buffer &) + static auto stuff(stream & s, const Buffer &) { char buf[] = {sm...}; s.write(buf, sizeof...(sm)); - return Buffer(); + return Buffer(); } }; template - struct Upto { + struct Upto { template - static auto stuff(stream & s, const Buffer &) + static auto stuff(stream & s, const Buffer &) { char buf[] = {sm...}; s.write(buf, sizeof...(sm)); - return Buffer<0, sn...>(); + return Buffer(); } }; @@ -47,10 +79,10 @@ namespace AdHoc { template static void write(stream & s, const Pn & ... pn) { - next(s, Upto<'%', sn...>::stuff(s, Buffer<>()), pn...); + next(s, Upto<'%', 0, sn...>::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...); } @@ -90,19 +122,66 @@ namespace AdHoc { static int err; }; - template + template struct Parser { static auto parse() { - return Parser::parse(); + return append(innerparse()); + } + static auto innerparse() + { + return Parser::innerparse(); + } + template + static auto append(const Buffer & b) + { + return join(b, Parser::parse()); + } + template + static auto append(const Buffer & b) + { + return b; + } + template + static auto join(const Buffer &, const Buffer &) + { + return Buffer(); + } + }; + + template + struct Parser { + static auto parse() + { + return Buffer(); + } + static auto innerparse() + { + return Buffer(); } }; template - struct Parser { + struct Parser { static auto parse() { - return Buffer(); + return Buffer(); + } + static auto innerparse() + { + return Buffer(); + } + }; + + template + struct Parser { + static auto parse() + { + return Buffer(); + } + static auto innerparse() + { + return Buffer(); } }; @@ -111,11 +190,11 @@ namespace AdHoc { template static void write(stream & s, const Pn & ... pn) { - run(Parser::parse(), s, pn...); + 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...); } diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index f19ee0a..00f1e6f 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -136,11 +136,11 @@ BOOST_AUTO_TEST_CASE ( customMultiArgRightAlign ) BOOST_REQUIRE_EQUAL(buf3.str(), "value 123.45"); } -extern constexpr const char * formatStringLong = " "; +extern constexpr const char * formatStringLong = " "; BOOST_AUTO_TEST_CASE ( longFormatString ) { std::stringstream buf; Formatter::write(buf); - BOOST_REQUIRE_EQUAL(buf.str().length(), 124); + BOOST_REQUIRE_EQUAL(buf.str().length(), 246); } -- cgit v1.2.3