From a5cb9866e5e3aa05a6cfc11849e0e1e305f84696 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 30 Dec 2016 18:01:31 +0000 Subject: Add helper for simply returning a formatted string (sprintf style) --- libadhocutil/compileTimeFormatter.h | 10 +++++++++- libadhocutil/unittests/testCompileTimeFormatter.cpp | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 9f6acfe..8a92094 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -172,6 +172,14 @@ namespace AdHoc { template struct Formatter { + template + static std::string get(const Pn & ... pn) + { + std::stringstream s; + run(Parser::parse(), s, pn...); + return s.str(); + } + template static void write(stream & s, const Pn & ... pn) { diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index 9b678ec..0f021e5 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -211,3 +211,9 @@ BOOST_AUTO_TEST_CASE ( customMultiArgRightAlign ) BOOST_REQUIRE_EQUAL(buf3.str(), "value 123.45"); } +BOOST_AUTO_TEST_CASE ( get ) +{ + auto s = Formatter::get(20, "something else"); + BOOST_REQUIRE_EQUAL(s, "value something else"); +} + -- cgit v1.2.3