TabularStream.h
Go to the documentation of this file.
1 #ifndef _utl_TabularStream_h_
2 #define _utl_TabularStream_h_
3 
4 #include <string>
5 #include <iostream>
6 #include <vector>
7 #include <deque>
8 #include <sstream>
9 
10 #include <utl/ErrorLogger.h>
11 
12 
13 namespace utl {
14 
15 
17 
18  class TableCell {
19  public:
20  TableCell() { }
21  TableCell(const TableCell& c) : fEntry(c.fEntry.str()) { }
23  { fEntry.str(c.fEntry.str()); return *this; }
24 
25  private:
31  };
32 
33  template<typename T>
34  TableCell& operator<<(const T& item) { fEntry << item; return *this; }
35 
36  std::string GetEntry(const int left = 0, const int right = 0) const
37  { return std::string(left, ' ') + fEntry.str() + std::string(right, ' '); }
38 
39  std::string GetEntry(const Justification just, const int totalSize,
40  const int leftSize = 0, const char mid = '\0') const;
41 
42  int GetSize() const { return fEntry.str().size(); }
43 
44  std::pair<int, int> GetSize(const char mid) const;
45 
46  std::ostringstream fEntry;
47 
48  friend class TableColumn;
49  friend class TabularStream;
50  };
51 
52 
53  // Holds one column of TableCells in the TabularStream class
54 
55  class TableColumn {
56  public:
57  TableColumn() : fWidth(-1) { }
58 
59  private:
60  template<typename T>
61  TableColumn& operator<<(const T& junk)
62  { Front() << junk; return *this; }
63 
64  int GetLength() const { return fCells.size(); }
65 
66  void SetMargins(const std::string& prefix,
67  const std::string& postfix = std::string())
68  { fPrefix = prefix; fPostfix = postfix; }
69 
70  void SetJustification(const TableCell::Justification just, const char mid = '\0')
71  { fJustification = just; fMidChar = mid; }
72 
73  std::string Pop();
74 
75  void PopFront() { if (!fCells.empty()) fCells.pop_front(); }
76 
77  TableCell& Front() { return fCells.front(); }
78 
79  void Push() { fCells.push_front(TableCell()); }
80 
81  bool Empty() { return fCells.empty(); }
82 
83  void CalculateWidths();
84 
85  std::string fPrefix;
86  std::string fPostfix;
88  char fMidChar;
89  std::deque<TableCell> fCells;
90  int fWidth;
92 
93  friend class TabularStream;
94  };
95 
96 
98 
99  class EndColumn {
100  };
101 
103 
104 
106 
107  class EndRow {
108  };
109 
110  const EndRow endr = EndRow();
111 
112 
118  class DeleteRow {
119  };
120 
122 
124 
125  class HLine {
126  public:
127  HLine(const char sign) : fChar(sign) { }
128  char GetChar() const { return fChar; }
129  private:
130  char fChar;
131  };
132 
133  const HLine hline('-');
134 
135 
193  public:
194  TabularStream(const std::string& format)
195  { Clear(format); }
196 
197  void Clear() { fColumns.clear(); fMakeNewRow = true; }
198 
199  void Clear(const std::string& format)
200  { Clear(); MakeFormat(format); }
201 
202  template<typename T>
204  operator<<(const T& junk)
205  {
206  CheckFirst();
207  *fCurrentColumn << junk;
208  return *this;
209  }
210 
212  { CheckFirst(); NextColumn(); return *this; }
213 
215  { CheckFirst(); NextRow(); return *this; }
216 
218  operator<<(const HLine hl)
219  {
220  CheckFirst();
221  if (fCurrentColumn != fColumns.begin())
222  *this << "ERROR: hline can be used only at the begining of a new line";
223  else {
224  for (Columns::iterator cIt = fColumns.begin();
225  cIt != fColumns.end(); ++cIt)
226  *cIt << ((std::string("\r") + hl.GetChar()));
227  }
228  fMakeNewRow = true;
229  return *this;
230  }
231 
234  {
235  for (Columns::iterator cIt = fColumns.begin();
236  cIt != fColumns.end(); ++cIt)
237  cIt->PopFront();
238  fMakeNewRow = true;
239  return *this;
240  }
241 
242  std::string Str();
243 
244  private:
245  void MakeFormat(const std::string& format);
246 
247  void
249  {
250  if (fMakeNewRow) {
251  NextRow();
252  fMakeNewRow = false;
253  }
254  }
255 
256  void
258  {
259  ++fCurrentColumn;
260  if (fCurrentColumn == fColumns.end()) {
261  ERROR("Running out of columns, will fill the last one!");
262  --fCurrentColumn;
263  }
264  }
265 
266  void
268  {
269  for (Columns::iterator cIt = fColumns.begin();
270  cIt != fColumns.end(); ++cIt)
271  cIt->Push();
272  fCurrentColumn = fColumns.begin();
273  }
274 
275  typedef std::vector<TableColumn> Columns;
276  Columns::iterator fCurrentColumn;
279 
280  };
281 
282 
283  inline
284  std::ostream&
285  operator<<(std::ostream& os, TabularStream& ts)
286  {
287  return os << ts.Str();
288  }
289 
290 }
291 
292 #endif
std::string fPostfix
Definition: TabularStream.h:86
Holds TabularStream cell entry.
Definition: TabularStream.h:18
TabularStream & operator<<(const EndColumn)
TabularStream(const std::string &format)
TableCell & operator=(const TableCell &c)
Definition: TabularStream.h:22
void SetMargins(const std::string &prefix, const std::string &postfix=std::string())
Definition: TabularStream.h:66
std::vector< TableColumn > Columns
std::string Pop()
std::deque< TableCell > fCells
Definition: TabularStream.h:89
type-only supplying class that triggers end-of-column in the TabularStream
Definition: TabularStream.h:99
Stream & operator<<(Stream &s, MessageLoggerConfig &mlc)
Applies the configuration to the given stream.
int GetLength() const
Definition: TabularStream.h:64
int GetSize() const
Definition: TabularStream.h:42
std::string fPrefix
Definition: TabularStream.h:85
char GetChar() const
const EndRow endr
Columns::iterator fCurrentColumn
TableColumn & operator<<(const T &junk)
Definition: TabularStream.h:61
class that triggers insertion of the line row in the TabularStream
TabularStream & operator<<(const EndRow)
HLine(const char sign)
std::ostringstream fEntry
Definition: TabularStream.h:46
class to format data in tabular form
std::string Str()
void Clear(const std::string &format)
const DeleteRow delr
std::string GetEntry(const int left=0, const int right=0) const
Definition: TabularStream.h:36
void SetJustification(const TableCell::Justification just, const char mid= '\0')
Definition: TabularStream.h:70
TableCell & operator<<(const T &item)
Definition: TabularStream.h:34
TabularStream & operator<<(const T &junk)
void MakeFormat(const std::string &format)
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
type-only supplying class that triggers end-of-row in the TabularStream
const HLine hline('-')
const EndColumn endc
TableCell::Justification fJustification
Definition: TabularStream.h:87
TableCell & Front()
Definition: TabularStream.h:77
TableCell(const TableCell &c)
Definition: TabularStream.h:21

, generated on Tue Sep 26 2023.