Generic operator<< for std::vector, std::pair and custom types providing "streamify()"

This commit is contained in:
Moritz Bunkus 2012-04-17 22:14:46 +02:00
parent 50620f4988
commit cfdf2a93f2

View File

@ -0,0 +1,54 @@
/*
mkvmerge -- utility for splicing together matroska files
from component media subtypes
Distributed under the GPL
see the file COPYING for details
or visit http://www.gnu.org/copyleft/gpl.html
generic "operator <<()" for standard types
Written by Moritz Bunkus <moritz@bunkus.org>.
*/
#ifndef MTX_COMMON_OSTREAM_OPERATORS_H
#define MTX_COMMON_OSTREAM_OPERATORS_H
namespace std {
template<typename CharT, typename TraitsT, typename ContT>
std::basic_ostream<CharT, TraitsT> &
operator <<(std::basic_ostream<CharT, TraitsT> &out,
std::vector<ContT> const &vec) {
out << "<";
bool first = true;
for (auto element : vec) {
if (first)
first = false;
else
out << ",";
out << element;
}
out << ">";
return out;
}
template<typename CharT, typename TraitsT, typename PairT1, typename PairT2>
std::basic_ostream<CharT, TraitsT> &
operator <<(std::basic_ostream<CharT, TraitsT> &out,
std::pair<PairT1, PairT2> const &p) {
out << "(" << p.first << "/" << p.second << ")";
return out;
}
template<typename CharT, typename TraitsT, typename ElementT>
std::basic_ostream<CharT, TraitsT> &
operator <<(std::basic_ostream<CharT, TraitsT> &out,
ElementT const &e) {
return e.streamify(out);
}
}
#endif // MTX_COMMON_OSTREAM_OPERATORS_H