diff --git a/LCS/Common/include/Common/StreamUtil.h b/LCS/Common/include/Common/StreamUtil.h index 6c088dafa764efceb3998eb726219f273c8d5db3..00a545e0170b6c000e32dc4d459372afd08c1a43 100644 --- a/LCS/Common/include/Common/StreamUtil.h +++ b/LCS/Common/include/Common/StreamUtil.h @@ -30,6 +30,7 @@ #include <Common/LofarTypes.h> #include <Common/lofar_string.h> #include <Common/lofar_vector.h> +#include <Common/lofar_map.h> #include <Common/lofar_iosfwd.h> namespace LOFAR @@ -57,30 +58,50 @@ namespace LOFAR static const string tok; }; + // Print an indentation that depends on the number of Indent objects + // currently in existence. + inline ostream& indent(ostream& os) + { + for (uint i = 0; i < Indent::level(); ++i) { + os << Indent::token(); + } + return os; + } - // Write a vector to an ostream with a given separator, prefix and postfix. - template<class T> - void writeVector (std::ostream& os, const std::vector<T>& vec, - const char* separator=",", - const char* prefix="[", const char* postfix="]") + + // Write a std::pair. + template <typename T, typename U> + inline ostream& operator<< (ostream& os, const std::pair<T,U>& p) + { + os << p.first << ':' << p.second; + return os; + } + + // Write any container to the given ostream. + template<typename ITER> + inline void print (std::ostream& os, ITER begin, ITER end, + const char* separator=",", + const char* prefix="[", const char* postfix="]") { os << prefix; - for (uint i = 0; i < vec.size(); i++) { - if (i > 0) os << separator; - os << vec[i]; + if (begin != end) { + os << *begin; + ++begin; + } + for (; begin!=end; ++begin) { + os << separator << *begin; } os << postfix; } - // Print an indentation that depends on the number of Indent objects - // currently in existence. - inline ostream& indent(ostream& os) + // Write a vector to an ostream with a given separator, prefix and postfix. + template<class T> + void writeVector (std::ostream& os, const std::vector<T>& vec, + const char* separator=",", + const char* prefix="[", const char* postfix="]") { - for (uint i = 0; i < Indent::level(); ++i) { - os << Indent::token(); - } - return os; + print (os, vec.begin(), vec.end(), separator, prefix, postfix); } @@ -95,6 +116,17 @@ namespace LOFAR } + // Print the contents of a map enclosed in braces, using a comma + // as separator. + // \note operator<<() must be defined for type \c T. + template<typename T, typename U> + inline ostream& operator<<(ostream& os, const map<T,U>& m) + { + print (os, m.begin(), m.end(), ", ", "{", "}"); + return os; + } + + } // namespace LOFAR #endif diff --git a/LCS/Common/test/tStreamUtil.cc b/LCS/Common/test/tStreamUtil.cc index 94821b42c004990e14b8af7f2b6b0d242ff2566e..f6d43867b4e1541cd03453185f6b518ed8d30fc0 100644 --- a/LCS/Common/test/tStreamUtil.cc +++ b/LCS/Common/test/tStreamUtil.cc @@ -26,6 +26,7 @@ #include <Common/StreamUtil.h> #include <Common/LofarLogger.h> #include <Common/lofar_vector.h> +#include <Common/lofar_map.h> #include <Common/lofar_string.h> #include <cstring> @@ -64,5 +65,12 @@ int main() cout << "A vector of strings:" << endl << vs << endl << endl; + map<string,int> m; + m["abc"] = 1; + m["ef"] = -3; + print (cout, m.begin(), m.end(), "...", "(", ")"); + cout << endl; + cout << m << endl; + return 0; } diff --git a/LCS/Common/test/tStreamUtil.stdout b/LCS/Common/test/tStreamUtil.stdout index 85a45d49c9d5e12736d4c810930dadad38262e13..25a419c89599805f41c2c87d60516426488e0bfd 100644 --- a/LCS/Common/test/tStreamUtil.stdout +++ b/LCS/Common/test/tStreamUtil.stdout @@ -12,3 +12,5 @@ A vector of integers: A vector of strings: [aap,noot,mies,wim,zus,jet,teun,vuur,gijs,lam,kees,bok,weide,does,hok,duif,schapen] +(abc:1...ef:-3) +{abc:1, ef:-3}