diff options
-rw-r--r-- | libadhocutil/compileTimeFormatter.h | 5 | ||||
-rw-r--r-- | libadhocutil/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<S, start, stream, '%', C, sn...> : \ public StreamWriterBase<S, start, BOOST_PP_VARIADIC_SIZE(C) + 1, stream, sn...> +#define StreamWriterTP(P, C...) \ + template<const char * const & S, int start, typename stream, char P, char ... sn> \ + struct StreamWriter<S, start, stream, '%', C, sn...> : \ + public StreamWriterBase<S, start, BOOST_PP_VARIADIC_SIZE(C) + 1, stream, sn...> + // Default stream writer formatter StreamWriterT('?') { template<typename P, typename ... Pn> 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<typename P, typename ... Pn> 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<typename P, typename ... Pn> 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<typename P, typename ... Pn> + 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<formatStringCustomParam1>::write(*this, "some text here"); + BOOST_REQUIRE_EQUAL(this->str(), "custom ---( some text here )---"); +} + +BOOST_AUTO_TEST_CASE ( customParam2 ) +{ + Formatter<formatStringCustomParam2>::write(*this, "some text here"); + BOOST_REQUIRE_EQUAL(this->str(), "custom ---------( some text here )---------"); +} + typedef Formatter<formatStringCustom> TestFormat; BOOST_AUTO_TEST_CASE ( typedefFormat ) { |