diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-12-30 18:01:31 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-12-30 18:01:31 +0000 |
commit | a5cb9866e5e3aa05a6cfc11849e0e1e305f84696 (patch) | |
tree | d4a5901251a2534142aa703e9955230f8ed44fd5 | |
parent | Rename hacked in 'stuff' to 'scan' (diff) | |
download | libadhocutil-a5cb9866e5e3aa05a6cfc11849e0e1e305f84696.tar.bz2 libadhocutil-a5cb9866e5e3aa05a6cfc11849e0e1e305f84696.tar.xz libadhocutil-a5cb9866e5e3aa05a6cfc11849e0e1e305f84696.zip |
Add helper for simply returning a formatted string (sprintf style)
-rw-r--r-- | libadhocutil/compileTimeFormatter.h | 10 | ||||
-rw-r--r-- | libadhocutil/unittests/testCompileTimeFormatter.cpp | 6 |
2 files changed, 15 insertions, 1 deletions
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 <iostream> +#include <sstream> #include <string.h> #include <boost/static_assert.hpp> #include <boost/preprocessor/variadic/size.hpp> @@ -172,6 +172,14 @@ namespace AdHoc { template <const char * const & S> struct Formatter { + template<typename ... Pn> + static std::string get(const Pn & ... pn) + { + std::stringstream s; + run(Parser<S, 0, 0, *S>::parse(), s, pn...); + return s.str(); + } + template<typename stream, typename ... Pn> 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<formatStringMultiArg>::get(20, "something else"); + BOOST_REQUIRE_EQUAL(s, "value something else"); +} + |