From afed9cfd95077e09d2c07f36e5c8f77d55568866 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Tue, 25 Oct 2016 11:39:24 +0200 Subject: [PATCH] base: let string accept multiple arguments Issue #2064 --- repos/base/include/util/string.h | 31 +++++++++++++++++-------------- repos/base/run/log.run | 1 + repos/base/src/test/log/main.cc | 4 ++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/repos/base/include/util/string.h b/repos/base/include/util/string.h index 2c53934a88..ec8c04f3a2 100644 --- a/repos/base/include/util/string.h +++ b/repos/base/include/util/string.h @@ -595,18 +595,6 @@ class Genode::String } }; - template - void _init(T &&... args) - { - /* initialize string content */ - Local_output output(_buf); - Genode::print(output, args...); - - /* add terminating null */ - _buf[output.num_chars()] = 0; - _len = output.num_chars() + 1; - } - public: constexpr static size_t size() { return CAPACITY; } @@ -616,6 +604,12 @@ class Genode::String /** * Constructor * + * The constructor accepts a non-zero number of arguments, which + * are concatenated in the resulting 'String' object. In order to + * generate the textual representation of the arguments, the + * argument types must support the 'Output' interface, e.g., by + * providing 'print' method. + * * If the textual representation of the supplied arguments exceeds * 'CAPACITY', the resulting string gets truncated. The caller may * check for this condition by evaluating the 'length' of the @@ -623,8 +617,17 @@ class Genode::String * may fit perfectly into the buffer or may have been truncated. * In general, it would be safe to assume the latter. */ - template - String(T const &arg) { _init(arg); } + template + String(T const &arg, TAIL &&... args) + { + /* initialize string content */ + Local_output output(_buf); + Genode::print(output, arg, args...); + + /* add terminating null */ + _buf[output.num_chars()] = 0; + _len = output.num_chars() + 1; + } /** * Copy constructor diff --git a/repos/base/run/log.run b/repos/base/run/log.run index 611820ce8a..d7a77c61e8 100644 --- a/repos/base/run/log.run +++ b/repos/base/run/log.run @@ -32,5 +32,6 @@ compare_output_to { [init -> test-log] invalid hex range: [f8,08) (overflow!) [init -> test-log] negative hex char: 0xfe [init -> test-log] positive hex char: 0x02 +[init -> test-log] multiarg string: "parent -> child.7" [init -> test-log] Test done. } diff --git a/repos/base/src/test/log/main.cc b/repos/base/src/test/log/main.cc index 132a0284c5..45dd6fd5f0 100644 --- a/repos/base/src/test/log/main.cc +++ b/repos/base/src/test/log/main.cc @@ -26,5 +26,9 @@ void Component::construct(Genode::Env &env) log("invalid hex range: ", Hex_range(0xf8, 0x10)); log("negative hex char: ", Hex((char)-2LL, Hex::PREFIX, Hex::PAD)); log("positive hex char: ", Hex((char) 2LL, Hex::PREFIX, Hex::PAD)); + + typedef String<128> Label; + log("multiarg string: ", Label(Char('"'), "parent -> child.", 7, Char('"'))); + log("Test done."); }