|
Sierra Toolkit
Version of the Day
|
00001 /*------------------------------------------------------------------------*/ 00002 /* Copyright 2010 Sandia Corporation. */ 00003 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */ 00004 /* license for use of this work by or on behalf of the U.S. Government. */ 00005 /* Export of this program may require a license from the */ 00006 /* United States Government. */ 00007 /*------------------------------------------------------------------------*/ 00008 00009 #ifndef STK_UTIL_DIAG_PrintTable_hpp 00010 #define STK_UTIL_DIAG_PrintTable_hpp 00011 00012 #include <vector> 00013 #include <string> 00014 #include <sstream> 00015 00016 #include <stk_util/diag/Writer_fwd.hpp> 00017 00018 namespace stk_classic { 00019 00020 class PrintTable 00021 { 00022 template<typename T> 00023 friend PrintTable &operator<<(PrintTable &table, const T &t); 00024 00025 public: 00026 typedef std::string::size_type ColumnWidth; 00027 typedef std::vector<ColumnWidth> ColumnWidthVector; 00028 00029 struct Cell 00030 { 00031 enum Flags { 00032 SPAN = 0x01 00033 }; 00034 00035 enum Justification { 00036 LEFT = 1, 00037 RIGHT = 2, 00038 CENTER = 3, 00039 JUSTIFY_MASK = 0x0F, 00040 TRUNC = 0x10, 00041 ENDS = 0x20 00042 }; 00043 00044 Cell() 00045 : m_string(), 00046 m_flags(0), 00047 m_justification(RIGHT | TRUNC), 00048 m_indent(0), 00049 m_width(0) 00050 {} 00051 00052 Cell(const Cell &cell) 00053 : m_string(cell.m_string), 00054 m_flags(cell.m_flags), 00055 m_justification(cell.m_justification), 00056 m_indent(cell.m_indent), 00057 m_width(cell.m_width) 00058 {} 00059 00060 Cell &operator=(const Cell &cell) { 00061 m_string = cell.m_string; 00062 m_flags = cell.m_flags; 00063 m_justification = cell.m_justification; 00064 m_indent = cell.m_indent; 00065 m_width = cell.m_width; 00066 00067 return *this; 00068 } 00069 00070 std::string m_string; 00071 int m_flags; 00072 int m_justification; 00073 ColumnWidth m_indent; 00074 ColumnWidth m_width; 00075 }; 00076 00077 typedef std::vector<Cell> Row; 00078 typedef std::vector<Row> Table; 00079 00080 enum Flags { 00081 AUTO_END_COL = 0x01, 00082 COMMA_SEPARATED_VALUES = 0x02, 00083 PRINT_TRANSPOSED = 0x04 00084 }; 00085 00086 PrintTable() 00087 : m_ostream(0), 00088 m_flags(AUTO_END_COL), 00089 m_tableWidth(0) 00090 { 00091 m_table.push_back(Row()); 00092 } 00093 00094 explicit PrintTable(std::ostream &os) 00095 : m_ostream(&os), 00096 m_flags(AUTO_END_COL), 00097 m_commentPrefix(), 00098 m_tableWidth(0) 00099 { 00100 m_table.push_back(Row()); 00101 } 00102 00103 private: 00104 PrintTable(const PrintTable &); 00105 PrintTable &operator=(const PrintTable &); 00106 00107 public: 00108 ~PrintTable() 00109 {} 00110 00111 Row::size_type headerSize() const { 00112 return m_header.empty() ? 0 : m_header.begin()->size(); 00113 } 00114 00115 inline Table::size_type size() const { 00116 return m_table.size(); 00117 } 00118 00119 inline std::ostringstream &getCurrentString() { 00120 return m_currentString; 00121 } 00122 00123 inline bool autoEndCol() const { 00124 return m_flags & AUTO_END_COL; 00125 } 00126 00127 inline PrintTable &setAutoEndCol(bool auto_end_col = true) { 00128 if (auto_end_col) 00129 m_flags |= AUTO_END_COL; 00130 else 00131 m_flags &= ~AUTO_END_COL; 00132 return *this; 00133 } 00134 00135 inline bool commaSeparatedValues() const { 00136 return m_flags & COMMA_SEPARATED_VALUES; 00137 } 00138 00139 inline PrintTable &setCommaSeparatedValues(bool comma_separated_values = true) { 00140 if (comma_separated_values) 00141 m_flags |= COMMA_SEPARATED_VALUES; 00142 else 00143 m_flags &= ~COMMA_SEPARATED_VALUES; 00144 return *this; 00145 } 00146 00147 inline PrintTable &setCommentPrefix(const std::string &comment_prefix) { 00148 m_commentPrefix = comment_prefix; 00149 return *this; 00150 } 00151 00152 inline const std::string &getCommentPrefix() const { 00153 return m_commentPrefix; 00154 } 00155 00160 inline PrintTable &setTitle(const std::string &title){ 00161 m_title = title; 00162 return *this; 00163 } 00164 00165 inline const std::string &getTitle() const { 00166 return m_title; 00167 } 00168 00175 inline PrintTable& operator<<(PrintTable& (*f)(PrintTable&)) { 00176 f(*this); 00177 return *this; 00178 00179 } 00180 00187 inline PrintTable& operator<<(std::ios_base& (*f)(std::ios_base&)) { 00188 f(m_currentString); 00189 return *this; 00190 } 00191 00192 inline PrintTable &push() { 00193 ++m_currentCell.m_indent; 00194 return *this; 00195 } 00196 00197 inline PrintTable &span() { 00198 m_currentCell.m_flags |= Cell::SPAN; 00199 return *this; 00200 } 00201 00202 inline PrintTable &pop() { 00203 if (m_currentCell.m_indent != 0) 00204 --m_currentCell.m_indent; 00205 00206 return *this; 00207 } 00208 00209 inline PrintTable &cell_width(ColumnWidth my_width) { 00210 m_currentCell.m_width = my_width; 00211 00212 return *this; 00213 } 00214 00215 inline PrintTable &indent(ColumnWidth my_indent) { 00216 m_currentCell.m_indent = my_indent; 00217 00218 return *this; 00219 } 00220 00221 inline PrintTable &justify(int justification) { 00222 m_currentCell.m_justification = justification; 00223 00224 return *this; 00225 } 00226 00227 PrintTable &end_col(); 00228 00229 PrintTable &end_row(); 00230 00231 PrintTable &at(size_t row, size_t col); 00232 00233 PrintTable &end_header() { 00234 m_header.push_back(m_table.back()); 00235 m_table.pop_back(); 00236 m_table.push_back(Row()); 00237 return *this; 00238 } 00239 00240 PrintTable &end_format() { 00241 m_format = m_table.back(); 00242 m_table.pop_back(); 00243 m_table.push_back(Row()); 00244 return *this; 00245 } 00246 00247 void calculate_column_widths() const; 00248 00249 void transpose_table() const; 00250 00254 std::ostream &print(std::ostream &os) const; 00255 00259 std::ostream &printRow(std::ostream &os, const Row &row) const; 00260 00264 std::ostream &printHeaderBar(std::ostream &os) const; 00265 00269 std::ostream &csvPrint(std::ostream &os) const; 00270 00274 diag::Writer & verbose_print(diag::Writer &dout) const; 00275 00276 private: 00284 void normalize_table(int row, int col); 00285 00286 private: 00287 std::ostream * m_ostream; 00288 std::string m_title; 00289 Table m_header; 00290 Row m_format; 00291 Table m_table; 00292 Cell m_currentCell; 00293 std::ostringstream m_currentString; 00294 int m_flags; 00295 std::string m_commentPrefix; 00296 mutable ColumnWidthVector m_columnWidth; 00297 mutable ColumnWidth m_tableWidth; 00298 }; 00299 00300 00301 template<typename T> 00302 inline PrintTable &operator<<(PrintTable &table, const T &t) { 00303 table.m_currentString << t; 00304 if (table.autoEndCol()) 00305 table.end_col(); 00306 00307 return table; 00308 } 00309 00310 00311 struct cell_width 00312 { 00313 cell_width(PrintTable::ColumnWidth width) 00314 : m_width(width) 00315 {} 00316 00317 PrintTable::ColumnWidth m_width; 00318 }; 00319 00320 00321 struct at 00322 { 00323 at(size_t row, size_t col) 00324 : m_row(row), 00325 m_col(col) 00326 {} 00327 00328 size_t m_row; 00329 size_t m_col; 00330 }; 00331 00332 00333 struct indent 00334 { 00335 indent(PrintTable::ColumnWidth my_indent) 00336 : m_indent(my_indent) 00337 {} 00338 00339 PrintTable::ColumnWidth m_indent; 00340 }; 00341 00342 00343 struct justify 00344 { 00345 justify(int my_justify) 00346 : m_justify(my_justify) 00347 {} 00348 00349 int m_justify; 00350 }; 00351 00352 00353 inline PrintTable &operator<<(PrintTable &tout, const at &m) { 00354 tout.at(m.m_row, m.m_col); 00355 return tout; 00356 } 00357 00358 inline PrintTable &operator<<(PrintTable &tout, const cell_width &m) { 00359 tout.cell_width(m.m_width); 00360 return tout; 00361 } 00362 00363 inline PrintTable &operator<<(PrintTable &tout, const indent &m) { 00364 tout.indent(m.m_indent); 00365 return tout; 00366 } 00367 00368 inline PrintTable &operator<<(PrintTable &tout, const justify &m) { 00369 tout.justify(m.m_justify); 00370 return tout; 00371 } 00372 00373 inline PrintTable &end_col(PrintTable &tout) { 00374 return tout.end_col(); 00375 } 00376 00377 inline PrintTable &end_row(PrintTable &tout) { 00378 return tout.end_row(); 00379 } 00380 00381 inline PrintTable &end_header(PrintTable &tout) { 00382 return tout.end_header(); 00383 } 00384 00385 inline PrintTable &end_format(PrintTable &tout) { 00386 return tout.end_format(); 00387 } 00388 00389 inline PrintTable &push(PrintTable &tout) { 00390 return tout.push(); 00391 } 00392 00393 inline PrintTable &pop(PrintTable &tout) { 00394 return tout.pop(); 00395 } 00396 00397 inline PrintTable &span(PrintTable &tout) { 00398 return tout.span(); 00399 } 00400 00401 inline std::ostream &operator<<(std::ostream &os, const PrintTable &table){ 00402 return table.print(os); 00403 } 00404 00405 inline diag::Writer &operator<<(diag::Writer &dout, const PrintTable &table){ 00406 return table.verbose_print(dout); 00407 } 00408 00409 } // namespace stk_classic 00410 00411 //namespace sierra { 00412 // 00414 //using stk_classic::PrintTable; 00415 //typedef stk_classic::cell_width cell_width; 00416 //typedef stk_classic::at at; 00417 //typedef stk_classic::justify justify; 00418 // 00419 //} // namespace sierra 00420 00421 #endif // STK_UTIL_DIAG_PrintTable_hpp