KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
table_stream.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ \.
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Vicente Mataix Ferrandiz
11 // Inspired in bprinter, work of Dat Chu: https://github.com/dattanchu/bprinter
12 // Removing all the dependencies of boost::karma and adding bold fonts and additional functionalities needed
13 //
14 
15 #ifndef KRATOS_TABLE_STREAM_H_INCLUDED
16 #define KRATOS_TABLE_STREAM_H_INCLUDED
17 
18 // System includes
19 #include <iostream>
20 #include <iomanip>
21 #include <vector>
22 #include <string>
23 #include <sstream>
24 
25 // External includes
26 
27 // Project includes
28 #include "includes/serializer.h"
29 #include "includes/exception.h"
30 
31 namespace Kratos
32 {
35 
38 
42 
46 
50 
54 
55 class endl{};
56 
64 {
65 public:
66 
69 
72 
76 
79  std::ostream * Output,
80  const std::string& Separator = "|",
81  const bool UseBoldFont = true
82  ) : mOutStream(Output),
83  mSeparator(Separator),
84  mBoldFont(UseBoldFont)
85  {
86  // Initialize values
87  mIndexRow = 0;
88  mIndexColumn = 0;
89  mTableWidth = 0;
90  mFlushLeft = false;
91  }
92 
94  virtual ~TableStream() = default;
95 
99 
105  template<typename TClass>
106  TableStream& operator<<(TClass Input)
107  {
108  if (typeid(TClass) == typeid(endl)) {
109  while (mIndexColumn != 0) {
110  *this << "";
111  }
112  } else {
113  if (mIndexColumn == 0) {
114  *mOutStream << "|";
115  }
116 
117  if(mFlushLeft) {
118  *mOutStream << std::left;
119  } else {
120  *mOutStream << std::right;
121  }
122 
123  // Leave 3 extra space: One for negative sign, one for zero, one for decimal
124  *mOutStream << std::setw(mColumnWidths.at(mIndexColumn)) << Input;
125 
126  if (mIndexColumn == GetNumColumns()-1) {
127  *mOutStream << "|\n";
128  mIndexRow = mIndexRow + 1;
129  mIndexColumn = 0;
130  } else {
131  *mOutStream << mSeparator;
132  mIndexColumn = mIndexColumn + 1;
133  }
134  }
135 
136  return *this;
137  }
138 
144  TableStream& operator<<(float Input)
145  {
146  OutputDecimalNumber<float>(Input);
147  return *this;
148  }
149 
155  TableStream& operator<<(double Input)
156  {
157  OutputDecimalNumber<double>(Input);
158  return *this;
159  }
160 
164 
169  unsigned int GetNumColumns() const
170  {
171  return mColumnHeaders.size();
172  }
173 
178  unsigned int GetTableWidth() const
179  {
180  return mTableWidth;
181  }
182 
186  void SetSeparator(const std::string& Separator)
187  {
188  mSeparator = Separator;
189  }
190 
194  void SetBold(const bool& UseBoldFont)
195  {
196  mBoldFont = UseBoldFont;
197  }
198 
203  {
204  mFlushLeft = true;
205  }
206 
211  {
212  mFlushLeft = false;
213  }
214 
220  void AddColumn(
221  const std::string& HeaderName,
222  const int ColumnWidth
223  )
224  {
225  KRATOS_ERROR_IF(ColumnWidth < 4) << "Column size has to be >= 4" << std::endl;
226 
227  mColumnHeaders.push_back(HeaderName);
228  mColumnWidths.push_back(ColumnWidth);
229  mTableWidth += ColumnWidth + mSeparator.size(); // for the separator
230  }
231 
235  void PrintHeader()
236  {
237  PrintHorizontalLine();
238 
239  if (mBoldFont == true) {
240  #if !defined(_WIN32)
241  *mOutStream << "\x1B[1m";
242  #endif
243  }
244 
245  *mOutStream << "|";
246 
247  for (unsigned int i = 0; i < GetNumColumns(); ++i) {
248  if(mFlushLeft) {
249  *mOutStream << std::left;
250  } else {
251  *mOutStream << std::right;
252  }
253 
254  *mOutStream << std::setw(mColumnWidths.at(i)) << mColumnHeaders.at(i).substr(0, mColumnWidths.at(i));
255 
256  if (i != GetNumColumns()-1) {
257  *mOutStream << mSeparator;
258  }
259  }
260 
261  *mOutStream << "|";
262 
263  if (mBoldFont == true) {
264  #if !defined(_WIN32)
265  *mOutStream << "\x1B[0m";
266  #endif
267  }
268 
269  *mOutStream << "\n";
270 
271  PrintHorizontalLine();
272  }
273 
277  void PrintFooter()
278  {
279  PrintHorizontalLine();
280  }
281 
285 
289 
290 
294 
298 
299 
301 
302 protected:
305 
306 
310 
311 
315 
316 
320 
321 
325 
326 
330 
331 
335 
337 
338 private:
341 
342 
346 
347  // Stream related variables
348  std::ostream* mOutStream; // The stream considered
349  std::vector<std::string> mColumnHeaders; // This vector contains the header to print in each column
350  std::vector<int> mColumnWidths; // This vector contains the spaces of each column
351  std::string mSeparator; // The separator considered in each column
352 
353  // The indexes currently used
354  unsigned int mIndexRow; // Index of current row
355  unsigned int mIndexColumn; // Index of current column
356 
357  // Other variables related with the table output
358  unsigned int mTableWidth; // The table width
359  bool mFlushLeft; // If the flush is aligned to the left or the right
360  bool mBoldFont; // If the bold fonts are considered to use in the
361 
365 
366 
367 
371 
375  void PrintHorizontalLine()
376  {
377  *mOutStream << "+"; // the left bar
378 
379  for (unsigned int i = 0; i< mTableWidth-1; ++i) {
380  *mOutStream << "-";
381  }
382 
383  *mOutStream << "+"; // the right bar
384  *mOutStream << "\n";
385  }
386 
391  template<typename TClass>
392  void OutputDecimalNumber(TClass Input)
393  {
394  // If we cannot handle this number, indicate so
395  if (Input < 10*(mColumnWidths.at(mIndexColumn)-1) || Input > 10*mColumnWidths.at(mIndexColumn)) {
396  std::stringstream string_out;
397  string_out
398  << std::setiosflags(std::ios::scientific)
399  << std::setprecision(3)
400  << std::uppercase
401  << std::setw(mColumnWidths.at(mIndexColumn))
402  << Input;
403 
404  std::string string_to_print = string_out.str();
405 
406  *mOutStream << string_to_print;
407  } else {
408  *mOutStream
409  << std::setiosflags(std::ios::scientific)
410  << std::setprecision(3)
411  << std::uppercase
412  << std::setw(mColumnWidths.at(mIndexColumn))
413  << Input;
414  }
415 
416  if (mIndexColumn == GetNumColumns()-1) {
417  *mOutStream << "|\n";
418  mIndexRow = mIndexRow + 1;
419  mIndexColumn = 0;
420  } else {
421  *mOutStream << mSeparator;
422  mIndexColumn = mIndexColumn + 1;
423  }
424  }
425 
429 
433 
434  friend class Serializer;
435 
436  void save(Serializer& rSerializer) const
437  {
438 // rSerializer.save("OutStream", mOutStream);
439  rSerializer.save("ColumnHeaders", mColumnHeaders);
440  rSerializer.save("ColumnWidths", mColumnWidths);
441  rSerializer.save("Separator", mSeparator);
442  rSerializer.save("IndexRow", mIndexRow);
443  rSerializer.save("IndexColumn", mIndexColumn);
444  rSerializer.save("TableWidth", mTableWidth);
445  rSerializer.save("FlushLeft", mFlushLeft);
446  rSerializer.save("BoldFont", mBoldFont);
447  }
448 
449  void load(Serializer& rSerializer)
450  {
451 // rSerializer.load("OutStream", mOutStream);
452  rSerializer.load("ColumnHeaders", mColumnHeaders);
453  rSerializer.load("ColumnWidths", mColumnWidths);
454  rSerializer.load("Separator", mSeparator);
455  rSerializer.load("IndexRow", mIndexRow);
456  rSerializer.load("IndexColumn", mIndexColumn);
457  rSerializer.load("TableWidth", mTableWidth);
458  rSerializer.load("FlushLeft", mFlushLeft);
459  rSerializer.load("BoldFont", mBoldFont);
460  }
461 
465 
466 
470 }; // Class TableStream
471 
472 } // namespace Kratos.
473 #endif
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void load(std::string const &rTag, TDataType &rObject)
Definition: serializer.h:207
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
This is a fancy table to stream data in a fancy way.
Definition: table_stream.h:64
Definition: table_stream.h:55
void AddColumn(const std::string &HeaderName, const int ColumnWidth)
Add a column to our table.
Definition: table_stream.h:220
TableStream & operator<<(TClass Input)
This is the operator << for any kind of input.
Definition: table_stream.h:106
void SetFlushLeft()
Set the flush orientation to the left.
Definition: table_stream.h:202
TableStream(std::ostream *Output, const std::string &Separator="|", const bool UseBoldFont=true)
Default constructor.
Definition: table_stream.h:78
void SetFlushRight()
Set the flush orientation to the right.
Definition: table_stream.h:210
void PrintHeader()
This function prints the header of the stream.
Definition: table_stream.h:235
void PrintFooter()
This function prints the footer of the stream.
Definition: table_stream.h:277
void SetSeparator(const std::string &Separator)
Set the separator used for the table.
Definition: table_stream.h:186
TableStream & operator<<(float Input)
This is the operator << just for floats.
Definition: table_stream.h:144
unsigned int GetNumColumns() const
It returns the number of columns.
Definition: table_stream.h:169
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
unsigned int GetTableWidth() const
It returns the table width.
Definition: table_stream.h:178
TableStream & operator<<(double Input)
This is the operator << just for doubles.
Definition: table_stream.h:155
virtual ~TableStream()=default
Destructor.
KRATOS_CLASS_POINTER_DEFINITION(TableStream)
Pointer definition of TableStream.
void SetBold(const bool &UseBoldFont)
Set if the bold fonts are used for the table.
Definition: table_stream.h:194
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
left
Definition: exact_hinsberg_test.py:140
def load(f)
Definition: ode_solve.py:307
integer i
Definition: TensorModule.f:17