summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2018-08-20 21:47:45 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2018-08-20 21:51:32 +0100
commitbc77811cca8fb7634f45e1432d7eff571964c47f (patch)
tree9dd008c399cedc91ff68654774a727476f4b6d53
parentOnly GCC supports -flto=N (diff)
downloadlibadhocutil-bc77811cca8fb7634f45e1432d7eff571964c47f.tar.bz2
libadhocutil-bc77811cca8fb7634f45e1432d7eff571964c47f.tar.xz
libadhocutil-bc77811cca8fb7634f45e1432d7eff571964c47f.zip
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)
-rw-r--r--libadhocutil/compileTimeFormatter.h10
-rw-r--r--libadhocutil/unittests/testCompileTimeFormatter.cpp11
2 files changed, 21 insertions, 0 deletions
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
+