From bc77811cca8fb7634f45e1432d7eff571964c47f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 20 Aug 2018 21:47:45 +0100 Subject: Add the scprintf macro Clang/LLVM only macro which allows a more pure use of CTF with everything inline. e.g. scprintf(str, "Number = %d", n) --- libadhocutil/compileTimeFormatter.h | 10 ++++++++++ libadhocutil/unittests/testCompileTimeFormatter.cpp | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 0551f62..4fcb1cb 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -176,5 +176,15 @@ namespace AdHoc { #define AdHocFormatter(name, str) \ AdHocFormatterTypedef(name, str, MAKE_UNIQUE(name)) +// As far as I know, only clang/llvm version 5+ can compile this +// so long as std=c++17 +#if __clang_major__ >= 5 && __cplusplus >= 201703 +#define scprintf(strm, fmt, ...) \ + ([&strm]() -> decltype(strm) & { \ + static constexpr const char * const __FMT = fmt; \ + return ::AdHoc::Formatter<__FMT>::write(strm, __VA_ARGS__); \ + }()) +#endif + #endif diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index 2870993..2b9a796 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -413,3 +413,14 @@ BOOST_AUTO_TEST_CASE(smartptr) smartptr_fmt::get(shrd); } +// This tests scprintf macro, which in turn requires compiler support +#ifdef scprintf +BOOST_AUTO_TEST_CASE(scprintf) +{ + std::stringstream str; + auto & strret = scprintf(str, "Some literal format string (%d, %c).", 0, 'f'); + BOOST_CHECK_EQUAL(&str, &strret); // We got back our original stream + BOOST_CHECK_EQUAL(str.str(), "Some literal format string (0, f)."); +} +#endif + -- cgit v1.2.3