GEOS
TableLayout.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_FORMAT_TABLE_TABLELAYOUT_HPP
21 #define GEOS_COMMON_FORMAT_TABLE_TABLELAYOUT_HPP
22 
23 #include "common/DataTypes.hpp"
24 #include "TableTypes.hpp"
25 #include <variant>
26 #include "common/logger/Logger.hpp"
27 
28 
29 namespace geos
30 {
31 
36 {
37 
38 public:
39 
41  enum Alignment { right, left, center };
42 
45  {
46  tiny = 0,
47  small = 1,
48  medium = 2,
49  large = 3
50  };
51 
55  enum Section { header, values };
56 
61  {
63  Alignment headerAlignment = Alignment::center;
65  Alignment valueAlignment = Alignment::right;
66  };
67 
72  struct CellLayout
73  {
75  std::vector< string > m_lines;
81  size_t m_cellWidth;
82 
87 
94  CellLayout( CellType cellType, string const & value, TableLayout::Alignment alignment );
95  };
96 
101  class Column
102  {
103 public:
107  std::vector< Column > m_subColumn;
110 
116 
123 
129  { return m_parent; }
130 
135  void setParent( Column * parent )
136  { m_parent = parent; }
137 
143  { return m_next; }
144 
149  void setNextCell( Column * nextCell )
150  { m_next = nextCell; }
151 
158 
165 
171  TableLayout::Column & addSubColumns( std::initializer_list< TableLayout::Column > subCol );
172 
178  TableLayout::Column & addSubColumns( std::initializer_list< string > subColName );
179 
185  TableLayout::Column & addSubColumns( string const & subColName );
186 
193 
200 
205  { return m_headerMergeCount; }
206 
211  void incrementMergeHeaderCount( size_t value )
212  { m_headerMergeCount+= value;}
213 
218  { m_headerMergeCount--; }
219 
224  bool hasChild() const
225  { return !this->m_subColumn.empty(); }
226 
231  bool hasParent() const
232  { return this->m_parent != nullptr; }
233 
234 private:
236  Column * m_parent;
238  Column * m_next;
240  size_t m_headerMergeCount = 0;
241  };
242 
248  {
249 public:
252 
258  DeepFirstIterator( ColumnType * columnPtr, size_t idxLayer ):
259  m_currentColumn( columnPtr ), m_currentLayer( idxLayer )
260  {}
261 
268  {
269  this->m_currentColumn= columnPtr;
270  return *this;
271  }
272 
278 
284 
290  { return *m_currentColumn; }
291 
297  { return m_currentColumn; }
298 
305  friend bool operator== ( DeepFirstIterator const & a, DeepFirstIterator const & b )
306  { return a.m_currentColumn == b.m_currentColumn; };
313  friend bool operator!= ( DeepFirstIterator const & a, DeepFirstIterator const & b )
314  { return a.m_currentColumn != b.m_currentColumn; };
315 
320  size_t getCurrentLayer() const
321  { return m_currentLayer; }
322 
328  bool isLastColumn();
329 
330 private:
332  ColumnType * m_currentColumn;
334  size_t m_currentLayer;
335  };
336 
343 
350  {
351  return DeepFirstIterator( nullptr, 0 );
352  }
353 
355  using TableLayoutArgs = std::initializer_list< std::variant< string_view, TableLayout::Column > >;
356 
357  TableLayout() = default;
358 
365  std::vector< TableLayout::Column > const & columns )
366  {
367  setMargin( MarginValue::medium );
368  setTitle( title );
369  for( auto const & column :columns )
370  {
371  addToColumns( column );
372  }
373  }
374 
381  TableLayoutArgs args )
382  {
383  setMargin( MarginValue::medium );
384  setTitle( title );
385  processArguments( args );
386  }
387 
394  {
395  setMargin( MarginValue::medium );
396  processArguments( args );
397  }
398 
405  std::vector< string > const & args )
406  {
407  setMargin( MarginValue::medium );
408  setTitle( title );
409  addToColumns( args );
410  }
411 
416  size_t getMaxDepth() const;
417 
421  std::vector< Column > & getColumns()
422  { return m_tableColumnsData; }
423 
427  std::vector< Column > const & getColumns() const
428  { return m_tableColumnsData; }
429 
434  { return m_tableTitle; }
435 
441 
447  TableLayout & enableLineBreak( bool value );
448 
455 
459  bool isLineBreakEnabled() const;
460 
461 
466  integer const & getBorderMargin() const
467  { return m_borderMargin; }
468 
473  integer const & getColumnMargin() const
474  { return m_columnMargin; }
475 
479  integer const & getMarginValue() const
480  { return m_marginValue; }
481 
485  integer const & getMarginTitle() const
486  { return m_titleMargin; }
487 
492  std::vector< size_t > & getSublineInHeaderCounts()
493  { return m_sublineHeaderCounts; }
494 
499  std::vector< size_t > & getNbSubDataLines()
500  { return m_sublineDataCounts; }
501 
506  void addToColumns( string_view m_header );
507 
508 private:
509 
514  void processArguments( TableLayoutArgs args )
515  {
516  for( auto const & arg : args )
517  {
518  std::visit( [this]( auto const & value ) {
519  addToColumns( value );
520  }, arg );
521  }
522  }
523 
530  template< typename ... Ts >
531  void processArguments( Ts &... args )
532  {
533  addToColumns( args ... );
534  }
535 
540  void addToColumns( std::vector< string > const & columnNames );
541 
547  void addToColumns( TableLayout::Column const & column );
548 
550  std::vector< Column > m_tableColumnsData;
552  std::vector< size_t > m_sublineHeaderCounts;
554  std::vector< size_t > m_sublineDataCounts;
555  bool m_wrapLine = true;
556 
557  string m_tableTitle;
558 
559  integer m_borderMargin;
560  integer m_columnMargin;
561  integer m_marginValue;
562  integer m_titleMargin = 2;
563 
564 };
565 }
566 
567 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLELAYOUT_HPP */
Class representing a column in a table layout.
TableLayout::Column & addSubColumns(string const &subColName)
Adds a single sub-column to the column.
std::vector< Column > m_subColumn
A vector containing all sub-columns in the column.
Column * getParent()
Get the parent column.
Column * getNextCell()
GGet the next column in the layout.
Column()
Default constructor. Initializes a column with default values.
Column(TableLayout::CellLayout cellLayout)
Constructor to initialize a column with a specific CellLayout.
ColumnAlignement m_alignment
struct containing m_alignment for the column (header and values)
void decrementMergeHeaderCount()
Decremente number of times we will divide the current cell.
TableLayout::Column & addSubColumns(std::initializer_list< string > subColName)
Adds multiple sub-columns to the column.
TableLayout::Column & setValuesAlignment(Alignment valueAlignment)
Sets the values alignment for the column.
CellLayout m_header
The header cell layout.
void setParent(Column *parent)
Set the parent column.
void incrementMergeHeaderCount(size_t value)
Increment number of times we will divide the current cell.
TableLayout::Column & setHeaderAlignment(Alignment headerAlignment)
Sets the header alignment for the column.
Column & setVisibility(CellType celltype)
Set the column visibility.
Column & setName(string_view name)
Sets the name of the column.
bool hasChild() const
Checks if the column has any child columns.
TableLayout::Column & addSubColumns(std::initializer_list< TableLayout::Column > subCol)
Adds multiple sub-columns to the column.
bool hasParent() const
Checks if the column has a parent column.
void setNextCell(Column *nextCell)
Set the next column in the layout.
Iterator to loop over all columns, starting by the deepest sub columns, then to their parents,...
DeepFirstIterator(ColumnType *columnPtr, size_t idxLayer)
Construct a new Leaf Iterator object.
friend bool operator==(DeepFirstIterator const &a, DeepFirstIterator const &b)
Equality comparison operator.
ColumnType * operator->()
Arrow operator.
DeepFirstIterator operator++(int)
Postfix ++ overload.
friend bool operator!=(DeepFirstIterator const &a, DeepFirstIterator const &b)
Inequality comparison operator.
DeepFirstIterator & operator++()
Prefix ++ overload.
ColumnType & operator*()
Dereference operator.
DeepFirstIterator & operator=(Column *columnPtr)
Copy assignment operator.
bool isLastColumn()
Check if the current cell belong the last column.
size_t getCurrentLayer() const
Gets the current layer (depth) of the iterator.
Class for setup the table layout.
Definition: TableLayout.hpp:36
bool isLineBreakEnabled() const
TableLayout(string_view title, std::vector< string > const &args)
Construct a new Table Layout object.
integer const & getMarginTitle() const
integer const & getMarginValue() const
std::vector< size_t > & getNbSubDataLines()
Get the Nb Rows object.
TableLayout(string_view title, TableLayoutArgs args)
Construct a new Table Layout object.
integer const & getColumnMargin() const
std::initializer_list< std::variant< string_view, TableLayout::Column > > TableLayoutArgs
Alias for an initializer list of variants that can contain either a string or a layout column.
TableLayout(string_view title, std::vector< TableLayout::Column > const &columns)
Construct a new Table Layout object.
Alignment
Type of aligment for a column.
Definition: TableLayout.hpp:41
DeepFirstIterator endDeepFirst()
TableLayout & setMargin(MarginValue marginValue)
Set the minimal margin width between cell content and borders.
std::vector< size_t > & getSublineInHeaderCounts()
Get the Nb Rows object.
integer const & getBorderMargin() const
TableLayout(TableLayoutArgs args)
Construct a new Table Layout object.
string_view getTitle() const
void addToColumns(string_view m_header)
Create and add a column to the columns vector given a string.
TableLayout & setTitle(string_view title)
std::vector< Column > const & getColumns() const
Section
Enumeration for table sections.
Definition: TableLayout.hpp:55
std::vector< Column > & getColumns()
MarginValue
Space to apply between all data and border.
Definition: TableLayout.hpp:45
size_t getMaxDepth() const
Get the max depth of a column.
TableLayout & enableLineBreak(bool value)
Remove the return line at the end & begenning of the table.
DeepFirstIterator beginDeepFirst()
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:33
std::string_view string_view
String type.
Definition: DataTypes.hpp:94
Structure grouping the cell information to display it in a table (content, type, alignment,...
Definition: TableLayout.hpp:73
size_t m_cellWidth
Maximum length of the data in the cell.
Definition: TableLayout.hpp:81
CellType m_cellType
The type of the cell (Header,Value, Merge, ...).
Definition: TableLayout.hpp:77
CellLayout(CellType cellType, string const &value, TableLayout::Alignment alignment)
Constructor to initialize a cell given celltype, value and alignment.
CellLayout()
Constructor to initialize a Cell with a default settings.
std::vector< string > m_lines
vector containing each cell content, separated by lines.
Definition: TableLayout.hpp:75
Alignment m_alignment
The alignment of the cell (left, center, right).
Definition: TableLayout.hpp:79
Structure to set up values m_alignment for each colum.
Definition: TableLayout.hpp:61
Alignment headerAlignment
Alignment for column name. By default aligned to center.
Definition: TableLayout.hpp:63
Alignment valueAlignment
Alignment for column values. By default aligned to right side.
Definition: TableLayout.hpp:65