summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <daniel.goodliffe@pressassociation.com>2016-12-08 10:43:51 +0000
committerDan Goodliffe <daniel.goodliffe@pressassociation.com>2016-12-08 10:43:51 +0000
commit5a3da898536b9132dbd313eefb4007c864726bd2 (patch)
tree135c38e064cd06983ba2857f7d941d5506308f16
parentAdds support for the compile time formatter (diff)
downloadlibadhocutil-5a3da898536b9132dbd313eefb4007c864726bd2.tar.bz2
libadhocutil-5a3da898536b9132dbd313eefb4007c864726bd2.tar.xz
libadhocutil-5a3da898536b9132dbd313eefb4007c864726bd2.zip
Refactor to avoid hitting the template depth limit so early
-rw-r--r--libadhocutil/compileTimeFormatter.h35
-rw-r--r--libadhocutil/unittests/testCompileTimeFormatter.cpp8
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 <typename stream, char ... sn>
struct StreamWriter {
template<typename ... Pn>
- 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<typename ... Pn, char... ssn, template <char...> class Buffer>
static void next(stream & s, const Buffer<ssn...>&, const Pn & ... pn)
{
@@ -90,21 +90,34 @@ namespace AdHoc {
static int err;
};
- template <const char * const & S, char s0 = *S, int offset = 0, char ... sn>
- struct Formatter {
- template<typename stream, typename ... Pn>
- static void write(stream & s, const Pn & ... pn)
+ template <const char * const & S, int offset, char s0, char ... sn>
+ struct Parser {
+ static auto parse()
{
- Formatter<S, S[offset], offset + 1, sn..., S[offset]>::write(s, pn...);
+ return Parser<S, offset + 1, S[offset + 1], sn..., s0>::parse();
}
};
template <const char * const & S, int offset, char ... sn>
- struct Formatter<S, 0, offset, sn...> {
+ struct Parser<S, offset, 0, sn...> {
+ static auto parse()
+ {
+ return Buffer<sn..., 0>();
+ }
+ };
+
+ template <const char * const & S>
+ struct Formatter {
template<typename stream, typename ... Pn>
static void write(stream & s, const Pn & ... pn)
{
- StreamWriter<stream, sn...>::write(s, pn...);
+ run(Parser<S, 0, *S>::parse(), s, pn...);
+ }
+
+ template<typename stream, char...ssn, template<char...> class Buffer, typename ... Pn>
+ static void run(const Buffer<ssn...> &, stream & s, const Pn & ... pn)
+ {
+ StreamWriter<stream, ssn...>::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<formatStringLong>::write(buf);
+ BOOST_REQUIRE_EQUAL(buf.str().length(), 124);
+}
+