Sierra Toolkit  Version of the Day
PrintTable.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_DIAG_PrintTable_hpp
10 #define STK_UTIL_DIAG_PrintTable_hpp
11 
12 #include <vector>
13 #include <string>
14 #include <sstream>
15 
16 #include <stk_util/diag/Writer_fwd.hpp>
17 
18 namespace stk_classic {
19 
20 class PrintTable
21 {
22  template<typename T>
23  friend PrintTable &operator<<(PrintTable &table, const T &t);
24 
25 public:
26  typedef std::string::size_type ColumnWidth;
27  typedef std::vector<ColumnWidth> ColumnWidthVector;
28 
29  struct Cell
30  {
31  enum Flags {
32  SPAN = 0x01
33  };
34 
35  enum Justification {
36  LEFT = 1,
37  RIGHT = 2,
38  CENTER = 3,
39  JUSTIFY_MASK = 0x0F,
40  TRUNC = 0x10,
41  ENDS = 0x20
42  };
43 
44  Cell()
45  : m_string(),
46  m_flags(0),
47  m_justification(RIGHT | TRUNC),
48  m_indent(0),
49  m_width(0)
50  {}
51 
52  Cell(const Cell &cell)
53  : m_string(cell.m_string),
54  m_flags(cell.m_flags),
55  m_justification(cell.m_justification),
56  m_indent(cell.m_indent),
57  m_width(cell.m_width)
58  {}
59 
60  Cell &operator=(const Cell &cell) {
61  m_string = cell.m_string;
62  m_flags = cell.m_flags;
63  m_justification = cell.m_justification;
64  m_indent = cell.m_indent;
65  m_width = cell.m_width;
66 
67  return *this;
68  }
69 
70  std::string m_string;
71  int m_flags;
72  int m_justification;
73  ColumnWidth m_indent;
74  ColumnWidth m_width;
75  };
76 
77  typedef std::vector<Cell> Row;
78  typedef std::vector<Row> Table;
79 
80  enum Flags {
81  AUTO_END_COL = 0x01,
82  COMMA_SEPARATED_VALUES = 0x02,
83  PRINT_TRANSPOSED = 0x04
84  };
85 
86  PrintTable()
87  : m_ostream(0),
88  m_flags(AUTO_END_COL),
89  m_tableWidth(0)
90  {
91  m_table.push_back(Row());
92  }
93 
94  explicit PrintTable(std::ostream &os)
95  : m_ostream(&os),
96  m_flags(AUTO_END_COL),
97  m_commentPrefix(),
98  m_tableWidth(0)
99  {
100  m_table.push_back(Row());
101  }
102 
103 private:
104  PrintTable(const PrintTable &);
105  PrintTable &operator=(const PrintTable &);
106 
107 public:
108  ~PrintTable()
109  {}
110 
111  Row::size_type headerSize() const {
112  return m_header.empty() ? 0 : m_header.begin()->size();
113  }
114 
115  inline Table::size_type size() const {
116  return m_table.size();
117  }
118 
119  inline std::ostringstream &getCurrentString() {
120  return m_currentString;
121  }
122 
123  inline bool autoEndCol() const {
124  return m_flags & AUTO_END_COL;
125  }
126 
127  inline PrintTable &setAutoEndCol(bool auto_end_col = true) {
128  if (auto_end_col)
129  m_flags |= AUTO_END_COL;
130  else
131  m_flags &= ~AUTO_END_COL;
132  return *this;
133  }
134 
135  inline bool commaSeparatedValues() const {
136  return m_flags & COMMA_SEPARATED_VALUES;
137  }
138 
139  inline PrintTable &setCommaSeparatedValues(bool comma_separated_values = true) {
140  if (comma_separated_values)
141  m_flags |= COMMA_SEPARATED_VALUES;
142  else
143  m_flags &= ~COMMA_SEPARATED_VALUES;
144  return *this;
145  }
146 
147  inline PrintTable &setCommentPrefix(const std::string &comment_prefix) {
148  m_commentPrefix = comment_prefix;
149  return *this;
150  }
151 
152  inline const std::string &getCommentPrefix() const {
153  return m_commentPrefix;
154  }
155 
160  inline PrintTable &setTitle(const std::string &title){
161  m_title = title;
162  return *this;
163  }
164 
165  inline const std::string &getTitle() const {
166  return m_title;
167  }
168 
175  inline PrintTable& operator<<(PrintTable& (*f)(PrintTable&)) {
176  f(*this);
177  return *this;
178 
179  }
180 
187  inline PrintTable& operator<<(std::ios_base& (*f)(std::ios_base&)) {
188  f(m_currentString);
189  return *this;
190  }
191 
192  inline PrintTable &push() {
193  ++m_currentCell.m_indent;
194  return *this;
195  }
196 
197  inline PrintTable &span() {
198  m_currentCell.m_flags |= Cell::SPAN;
199  return *this;
200  }
201 
202  inline PrintTable &pop() {
203  if (m_currentCell.m_indent != 0)
204  --m_currentCell.m_indent;
205 
206  return *this;
207  }
208 
209  inline PrintTable &cell_width(ColumnWidth my_width) {
210  m_currentCell.m_width = my_width;
211 
212  return *this;
213  }
214 
215  inline PrintTable &indent(ColumnWidth my_indent) {
216  m_currentCell.m_indent = my_indent;
217 
218  return *this;
219  }
220 
221  inline PrintTable &justify(int justification) {
222  m_currentCell.m_justification = justification;
223 
224  return *this;
225  }
226 
227  PrintTable &end_col();
228 
229  PrintTable &end_row();
230 
231  PrintTable &at(size_t row, size_t col);
232 
233  PrintTable &end_header() {
234  m_header.push_back(m_table.back());
235  m_table.pop_back();
236  m_table.push_back(Row());
237  return *this;
238  }
239 
240  PrintTable &end_format() {
241  m_format = m_table.back();
242  m_table.pop_back();
243  m_table.push_back(Row());
244  return *this;
245  }
246 
247  void calculate_column_widths() const;
248 
249  void transpose_table() const;
250 
254  std::ostream &print(std::ostream &os) const;
255 
259  std::ostream &printRow(std::ostream &os, const Row &row) const;
260 
264  std::ostream &printHeaderBar(std::ostream &os) const;
265 
269  std::ostream &csvPrint(std::ostream &os) const;
270 
274  diag::Writer & verbose_print(diag::Writer &dout) const;
275 
276 private:
284  void normalize_table(int row, int col);
285 
286 private:
287  std::ostream * m_ostream;
288  std::string m_title;
289  Table m_header;
290  Row m_format;
291  Table m_table;
292  Cell m_currentCell;
293  std::ostringstream m_currentString;
294  int m_flags;
295  std::string m_commentPrefix;
296  mutable ColumnWidthVector m_columnWidth;
297  mutable ColumnWidth m_tableWidth;
298 };
299 
300 
301 template<typename T>
302 inline PrintTable &operator<<(PrintTable &table, const T &t) {
303  table.m_currentString << t;
304  if (table.autoEndCol())
305  table.end_col();
306 
307  return table;
308 }
309 
310 
311 struct cell_width
312 {
313  cell_width(PrintTable::ColumnWidth width)
314  : m_width(width)
315  {}
316 
317  PrintTable::ColumnWidth m_width;
318 };
319 
320 
321 struct at
322 {
323  at(size_t row, size_t col)
324  : m_row(row),
325  m_col(col)
326  {}
327 
328  size_t m_row;
329  size_t m_col;
330 };
331 
332 
333 struct indent
334 {
335  indent(PrintTable::ColumnWidth my_indent)
336  : m_indent(my_indent)
337  {}
338 
339  PrintTable::ColumnWidth m_indent;
340 };
341 
342 
343 struct justify
344 {
345  justify(int my_justify)
346  : m_justify(my_justify)
347  {}
348 
349  int m_justify;
350 };
351 
352 
353 inline PrintTable &operator<<(PrintTable &tout, const at &m) {
354  tout.at(m.m_row, m.m_col);
355  return tout;
356 }
357 
358 inline PrintTable &operator<<(PrintTable &tout, const cell_width &m) {
359  tout.cell_width(m.m_width);
360  return tout;
361 }
362 
363 inline PrintTable &operator<<(PrintTable &tout, const indent &m) {
364  tout.indent(m.m_indent);
365  return tout;
366 }
367 
368 inline PrintTable &operator<<(PrintTable &tout, const justify &m) {
369  tout.justify(m.m_justify);
370  return tout;
371 }
372 
373 inline PrintTable &end_col(PrintTable &tout) {
374  return tout.end_col();
375 }
376 
377 inline PrintTable &end_row(PrintTable &tout) {
378  return tout.end_row();
379 }
380 
381 inline PrintTable &end_header(PrintTable &tout) {
382  return tout.end_header();
383 }
384 
385 inline PrintTable &end_format(PrintTable &tout) {
386  return tout.end_format();
387 }
388 
389 inline PrintTable &push(PrintTable &tout) {
390  return tout.push();
391 }
392 
393 inline PrintTable &pop(PrintTable &tout) {
394  return tout.pop();
395 }
396 
397 inline PrintTable &span(PrintTable &tout) {
398  return tout.span();
399 }
400 
401 inline std::ostream &operator<<(std::ostream &os, const PrintTable &table){
402  return table.print(os);
403 }
404 
405 inline diag::Writer &operator<<(diag::Writer &dout, const PrintTable &table){
406  return table.verbose_print(dout);
407 }
408 
409 } // namespace stk_classic
410 
411 //namespace sierra {
412 //
414 //using stk_classic::PrintTable;
415 //typedef stk_classic::cell_width cell_width;
416 //typedef stk_classic::at at;
417 //typedef stk_classic::justify justify;
418 //
419 //} // namespace sierra
420 
421 #endif // STK_UTIL_DIAG_PrintTable_hpp
std::ostream & print(std::ostream &os, const std::string &indent, const Bucket &bucket)
Print the parts and entities of this bucket.
Definition: Bucket.cpp:259
std::string title(const std::string &s)
Function title returns a first letter of each word capitalized of the string.
Definition: StringUtil.cpp:107
std::ostream & operator<<(std::ostream &s, const Bucket &k)
Print the part names for which this bucket is a subset.
Definition: Bucket.cpp:239
static const char LEFT
Meta-character to force left justification.
Sierra Toolkit.
std::ostream & tout()
Regression test textual output stream.
Definition: OutputLog.cpp:682