to_string: Add operator<< overload for std::tuple.

Message-Id: <20190829100902.GN21540@scylladb.com>
This commit is contained in:
Gleb Natapov
2019-08-29 13:09:02 +03:00
committed by Avi Kivity
parent 036f51927c
commit e61a86bbb2

View File

@@ -51,6 +51,20 @@ sstring join(sstring delimiter, const PrintableRange& items) {
return join(delimiter, items.begin(), items.end());
}
template<bool NeedsComma, typename Printable>
struct print_with_comma {
const Printable& v;
};
template<bool NeedsComma, typename Printable>
std::ostream& operator<<(std::ostream& os, const print_with_comma<NeedsComma, Printable>& x) {
os << x.v;
if (NeedsComma) {
os << ", ";
}
return os;
}
namespace std {
template<typename Printable>
@@ -87,6 +101,16 @@ std::ostream& operator<<(std::ostream& os, const std::pair<K, V>& p) {
return os;
}
template<typename... T, size_t... I>
std::ostream& print_tuple(std::ostream& os, const std::tuple<T...>& p, std::index_sequence<I...>) {
return ((os << "{" ) << ... << print_with_comma<I < sizeof...(I) - 1, T>{std::get<I>(p)}) << "}";
}
template <typename... T>
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& p) {
return print_tuple(os, p, std::make_index_sequence<sizeof...(T)>());
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& items) {
os << "{" << join(", ", items) << "}";