DP3
StreamUtil.h
Go to the documentation of this file.
1 // StreamUtil.h: useful stream manipulation methods.
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #ifndef LOFAR_COMMON_STREAMUTILX_H
7 #define LOFAR_COMMON_STREAMUTILX_H
8 
9 #include <map>
10 #include <ostream>
11 #include <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 
16 namespace dp3 {
17 namespace common {
18 
20 
27 class Indent {
28  public:
30  Indent() { lvl++; }
32  ~Indent() { lvl--; }
34  static unsigned int level() { return lvl; }
36  static const std::string& token() { return tok; }
37 
38  private:
40  static unsigned int lvl;
42  static const std::string tok;
43 };
44 
47 inline std::ostream& indent(std::ostream& os) {
48  for (unsigned int i = 0; i < Indent::level(); ++i) {
49  os << Indent::token();
50  }
51  return os;
52 }
53 
56 template <typename T, typename U>
57 std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p);
58 
60 template <typename ITER>
61 void print(std::ostream& os, ITER begin, ITER end, const char* separator = ",",
62  const char* prefix = "[", const char* postfix = "]");
63 
67 template <typename T>
68 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
69 
73 template <typename T>
74 std::ostream& operator<<(std::ostream& os, const std::set<T>& v);
75 
79 template <typename T, typename U>
80 std::ostream& operator<<(std::ostream& os, const std::map<T, U>& m);
81 
83 template <typename T, typename U>
84 inline std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
85  os << '<' << p.first << ',' << p.second << '>';
86  return os;
87 }
88 
90 template <typename ITER>
91 inline void print(std::ostream& os, ITER begin, ITER end, const char* separator,
92  const char* prefix, const char* postfix) {
93  os << prefix;
94  if (begin != end) {
95  os << *begin;
96  ++begin;
97  }
98  for (; begin != end; ++begin) {
99  os << separator << *begin;
100  }
101  os << postfix;
102 }
103 
107 template <typename T>
108 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
109  print(os, v.begin(), v.end(), ",", "[", "]");
110  return os;
111 }
112 
116 template <typename T>
117 inline std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
118  print(os, s.begin(), s.end(), ",", "[", "]");
119  return os;
120 }
121 
125 template <typename T, typename U>
126 inline std::ostream& operator<<(std::ostream& os, const std::map<T, U>& m) {
127  print(os, m.begin(), m.end(), ", ", "{", "}");
128  return os;
129 }
130 
131 } // namespace common
132 } // namespace dp3
133 
134 #endif
Useful stream manipulation methods.
Definition: StreamUtil.h:27
static unsigned int level()
Return the amount of indentation.
Definition: StreamUtil.h:34
Indent()
Constructor. Increments indentation level.
Definition: StreamUtil.h:30
~Indent()
Destructor. Decrements indentation level.
Definition: StreamUtil.h:32
static const std::string & token()
Return the token to be printed per indentation level.
Definition: StreamUtil.h:36
std::ostream & indent(std::ostream &os)
Definition: StreamUtil.h:47
std::ostream & operator<<(std::ostream &os, const KVpair &kv)
void print(std::ostream &os, ITER begin, ITER end, const char *separator=",", const char *prefix="[", const char *postfix="]")
Write any container to the given std::ostream.
Definition: StreamUtil.h:91
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53