From 4a7e3121e15dede49c8ae7704c579bc979464da7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Dec 2016 18:49:24 +0000 Subject: Macro helper for easily adding new formatters that take a single character parameter from the format string --- libadhocutil/compileTimeFormatter.h | 5 ++++ .../unittests/testCompileTimeFormatter.cpp | 31 ++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 501e0a7..034a65d 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -84,6 +84,11 @@ namespace AdHoc { struct StreamWriter : \ public StreamWriterBase +#define StreamWriterTP(P, C...) \ + template \ + struct StreamWriter : \ + public StreamWriterBase + // Default stream writer formatter StreamWriterT('?') { template diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index 7ecd50e..9b678ec 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -12,11 +12,12 @@ extern constexpr const char * formatEdgeCaseSingle = "1"; extern constexpr const char * formatEdgeCaseFormatStart = "%? after"; extern constexpr const char * formatEdgeCaseFormatEnd = "before %?"; extern constexpr const char * formatEdgeCaseFormatLonely = "%?"; - extern constexpr const char * formatStringLiteral = "literal"; extern constexpr const char * formatStringSingle = "single %?."; extern constexpr const char * formatStringMulti = "First %?, then %?."; extern constexpr const char * formatStringCustom = "custom %()"; +extern constexpr const char * formatStringCustomParam1 = "custom %(\x3)"; +extern constexpr const char * formatStringCustomParam2 = "custom %(\x9)"; extern constexpr const char * formatStringCustomLong = "custom %(longname)"; extern constexpr const char * formatStringLong = " "; extern constexpr const char * formatStringMultiArg = "value%ra"; @@ -26,7 +27,8 @@ extern constexpr const char * formatStringEscape3 = "literal %%%? percentage."; extern constexpr const char * formatStringEscape4 = "literal %%%?%% percentage."; namespace AdHoc { - // Custom stream writer formatter, formats as (bracketed expression) + // Custom stream writer formatter, formats as + // -( bracketed expression )- StreamWriterT('(', ')') { template static void write(stream & s, const P & p, const Pn & ... pn) @@ -35,6 +37,8 @@ namespace AdHoc { StreamWriter::next(s, pn...); } }; + // Custom stream writer formatter with a long identifier, formats as + // ---( bracketed expression )--- StreamWriterT('(', 'l', 'o', 'n', 'g', 'n', 'a', 'm', 'e', ')') { template static void write(stream & s, const P & p, const Pn & ... pn) @@ -43,6 +47,17 @@ namespace AdHoc { StreamWriter::next(s, pn...); } }; + // Custom stream writer formatter that has parameter in the format string, formats as + // dashes*-( bracketed expression )dashes*- + StreamWriterTP(dashes, '(', dashes, ')') { + template + static void write(stream & s, const P & p, const Pn & ... pn) + { + std::string d(dashes, '-'); + s << d << "( " << p << " )" << d; + StreamWriter::next(s, pn...); + } + }; // Custom stream writer formatter, formats // right-aligned by given width StreamWriterT('r', 'a') { @@ -157,6 +172,18 @@ BOOST_AUTO_TEST_CASE ( customLongName ) BOOST_REQUIRE_EQUAL(this->str(), "custom ---( some text here )---"); } +BOOST_AUTO_TEST_CASE ( customParam1 ) +{ + Formatter::write(*this, "some text here"); + BOOST_REQUIRE_EQUAL(this->str(), "custom ---( some text here )---"); +} + +BOOST_AUTO_TEST_CASE ( customParam2 ) +{ + Formatter::write(*this, "some text here"); + BOOST_REQUIRE_EQUAL(this->str(), "custom ---------( some text here )---------"); +} + typedef Formatter TestFormat; BOOST_AUTO_TEST_CASE ( typedefFormat ) { -- cgit v1.2.3