base: let string accept multiple arguments

Issue #2064
This commit is contained in:
Norman Feske 2016-10-25 11:39:24 +02:00 committed by Christian Helmuth
parent 0d4f48ca0b
commit afed9cfd95
3 changed files with 22 additions and 14 deletions

View File

@ -595,18 +595,6 @@ class Genode::String
}
};
template <typename... T>
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 <typename T>
String(T const &arg) { _init(arg); }
template <typename T, typename... TAIL>
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

View File

@ -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.
}

View File

@ -26,5 +26,9 @@ void Component::construct(Genode::Env &env)
log("invalid hex range: ", Hex_range<uint8_t>(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.");
}