diff options
Diffstat (limited to 'upstream/utility/Utility.h')
| -rw-r--r-- | upstream/utility/Utility.h | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/upstream/utility/Utility.h b/upstream/utility/Utility.h index c32095a..0f2aec3 100644 --- a/upstream/utility/Utility.h +++ b/upstream/utility/Utility.h @@ -35,6 +35,7 @@ #include <map> #include <sstream> #include <numeric> +#include <type_traits> namespace utility { @@ -62,15 +63,24 @@ T join(InputIt first, InputIt last, BinaryOperation op, T empty = T{}) } /** -* Format the items of a map into a string as a list of key-value pairs. The map must be -* composed of pairs of strings. +* Format the items of a sequence container of strings into a string. * +* @tparam Sequence the string sequence container (e.g. list or vector) * @param[in] lstr A list of strings * @param[in] separator The separator to use between each item * * @return the concatenated elements. */ -std::string asString(const std::list<std::string> &lstr, const std::string &separator = "\n"); +template <class Sequence> +std::string asString(const Sequence &lstr, const std::string &separator = "\n") +{ + static_assert(std::is_same<typename Sequence::value_type, std::string>::value, + "asString called on a sequence container that does not contains strings"); + + return join<std::string>( + begin(lstr), end(lstr), + [separator](std::string acc, std::string right) { return acc + separator + right; }); +} /** * Format the items of a map into a string as a list of key-value pairs. The map must be @@ -99,4 +109,4 @@ void appendTitle(std::string &strTo, const std::string &strTitle); */ bool isHexadecimal(const std::string &strValue); -} // utility +} // namespace utility |
