From 5a3da898536b9132dbd313eefb4007c864726bd2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 8 Dec 2016 10:43:51 +0000 Subject: Refactor to avoid hitting the template depth limit so early --- libadhocutil/compileTimeFormatter.h | 35 +++++++++++++++------- .../unittests/testCompileTimeFormatter.cpp | 8 +++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index aa30e20..39254e0 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -45,10 +45,10 @@ namespace AdHoc { template struct StreamWriter { template - static void write(stream & s, const Pn & ... pn) - { - next(s, Upto<'%', sn...>::stuff(s, Buffer<>()), pn...); - } + static void write(stream & s, const Pn & ... pn) + { + next(s, Upto<'%', sn...>::stuff(s, Buffer<>()), pn...); + } template class Buffer> static void next(stream & s, const Buffer&, const Pn & ... pn) { @@ -90,21 +90,34 @@ namespace AdHoc { static int err; }; - template - struct Formatter { - template - static void write(stream & s, const Pn & ... pn) + template + struct Parser { + static auto parse() { - Formatter::write(s, pn...); + return Parser::parse(); } }; template - struct Formatter { + struct Parser { + static auto parse() + { + return Buffer(); + } + }; + + template + struct Formatter { template static void write(stream & s, const Pn & ... pn) { - StreamWriter::write(s, pn...); + run(Parser::parse(), s, 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 4248cb6..f19ee0a 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -136,3 +136,11 @@ BOOST_AUTO_TEST_CASE ( customMultiArgRightAlign ) BOOST_REQUIRE_EQUAL(buf3.str(), "value 123.45"); } +extern constexpr const char * formatStringLong = " "; +BOOST_AUTO_TEST_CASE ( longFormatString ) +{ + std::stringstream buf; + Formatter::write(buf); + BOOST_REQUIRE_EQUAL(buf.str().length(), 124); +} + -- cgit v1.2.3