From 772bd173742753c1cc7b21ca79e20fab7f36abd3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 30 Dec 2016 22:38:23 +0000 Subject: Refactor to reduce AdHoc namespace polution --- libadhocutil/compileTimeFormatter.h | 198 +++++++++++++++++++----------------- 1 file changed, 104 insertions(+), 94 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 7734f9c..4f980d2 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -7,51 +7,16 @@ namespace AdHoc { /// @cond constexpr int WRAP_AT = 120; - template struct Buffer { }; - - template - struct Upto { - template - static auto scan(stream &, const Buffer & f) - { - return f; - } - }; - template - struct Upto { - template - static auto scan(stream & s, const Buffer &) - { - return Upto::scan(s, Buffer()); - } - }; - template - struct UptoWrite { - template - static auto scan(stream & s, const Buffer &) - { - s.write(S + start, sizeof...(sm)); - return Buffer(); - } - }; - template - struct Upto : public UptoWrite { }; - template - struct Upto : public UptoWrite { }; - template - struct Upto : public UptoWrite { }; - template - struct Upto : public UptoWrite { }; - template - struct Upto : public UptoWrite { }; + namespace FormatterImpl { + template struct ParserBuffer { }; + template struct Buffer { }; + } template struct StreamWriter { template - static void write(stream & s, const Pn & ... pn) - { - next(s, Upto::scan(s, Buffer<0>()), pn...); - } + static void write(stream & s, const Pn & ... pn); + template class Buffer> static void next(stream & s, const Buffer&, const Pn & ... pn) { @@ -121,63 +86,99 @@ namespace AdHoc { static int err; }; - template struct ParserBuffer { }; - - template - struct Parser { - static auto parse() - { - return append(innerparse()); - } - static auto innerparse() - { - return Parser::innerparse(); - } - template - static auto append(const ParserBuffer & b) - { - return join(b, Parser::parse()); - } - template - static auto append(const ParserBuffer & b) - { - return b; - } - template - static auto join(const ParserBuffer &, const ParserBuffer &) - { - return ParserBuffer(); - } - }; - - template - struct ParserBase { - static auto parse() - { - return innerparse(); - } - static auto innerparse() - { - return ParserBuffer(); - } - }; - - template - struct Parser : public ParserBase { }; - - template - struct Parser : public ParserBase { }; - - template - struct Parser : public ParserBase{ }; - /// @endcond - /** * Compile time string formatter. * @param S the format string. */ template class Formatter { + private: + template friend struct StreamWriter; + + template + struct Upto { + template + static auto scan(stream &, const FormatterImpl::Buffer & f) + { + return f; + } + }; + template + struct Upto { + template + static auto scan(stream & s, const FormatterImpl::Buffer &) + { + return Upto::scan(s, FormatterImpl::Buffer()); + } + }; + template + struct UptoWrite { + template + static auto scan(stream & s, const FormatterImpl::Buffer &) + { + s.write(S + start, sizeof...(sm)); + return FormatterImpl::Buffer(); + } + }; + template + struct Upto : public UptoWrite { }; + template + struct Upto : public UptoWrite { }; + template + struct Upto : public UptoWrite { }; + template + struct Upto : public UptoWrite { }; + template + struct Upto : public UptoWrite { }; + + template + struct Parser { + static auto parse() + { + return append(innerparse()); + } + static auto innerparse() + { + return Parser::innerparse(); + } + template + static auto append(const FormatterImpl::ParserBuffer & b) + { + return join(b, Parser::parse()); + } + template + static auto append(const FormatterImpl::ParserBuffer & b) + { + return b; + } + template + static auto join(const FormatterImpl::ParserBuffer &, const FormatterImpl::ParserBuffer &) + { + return FormatterImpl::ParserBuffer(); + } + }; + + template + struct ParserBase { + static auto parse() + { + return innerparse(); + } + static auto innerparse() + { + return FormatterImpl::ParserBuffer(); + } + }; + + template + struct Parser : public ParserBase { }; + + template + struct Parser : public ParserBase { }; + + template + struct Parser : public ParserBase{ }; + public: /** * Get a string containing the result of formatting. @@ -188,7 +189,7 @@ namespace AdHoc { static std::string get(const Pn & ... pn) { std::stringstream s; - return run(Parser::parse(), s, pn...).str(); + return run(Parser<0, 0, *S>::parse(), s, pn...).str(); } /** @@ -200,7 +201,7 @@ namespace AdHoc { template static stream & write(stream & s, const Pn & ... pn) { - return run(Parser::parse(), s, pn...); + return run(Parser<0, 0, *S>::parse(), s, pn...); } private: @@ -211,6 +212,15 @@ namespace AdHoc { return s; } }; + + /// @cond + template + template + void StreamWriter::write(stream & s, const Pn & ... pn) + { + next(s, Formatter::template Upto::scan(s, FormatterImpl::Buffer<0>()), pn...); + } + /// @endcond } #define AdHocFormatterTypedef(name, str, id) \ -- cgit v1.2.3