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  static constexpr size_t noColumnMaxWidth = std::numeric_limits< size_t >::max();
42 
44  enum Alignment { right, left, center };
45 
47  static constexpr Alignment defaultHeaderAlignment = Alignment::center;
48 
50  static constexpr Alignment defaultValueAlignment = Alignment::right;
51 
54  {
55  tiny = 0,
56  small = 1,
57  medium = 2,
58  large = 3
59  };
60 
64  enum Section { header, values };
65 
70  {
75  };
76 
81  class CellLayout
82  {
83 public:
88 
93 
98  CellLayout( CellType cellType );
99 
107 
111  size_t getWidth() const
112  { return m_cellWidth; }
113 
118  void setWidth( size_t cellWidth )
119  { m_cellWidth = cellWidth; }
120 
125  { return m_lines; }
126 
130  std::vector< string_view > & getLines()
131  { return m_lines; }
132 
136  size_t getHeight() const
137  { return m_lines.size(); }
138 
142  size_t isEmpty() const
143  { return m_lines.empty() || m_lines[0].empty(); }
144 
151  void prepareLayout( string_view value, size_t maxLineWidth );
152 
153 private:
155  size_t m_cellWidth;
157  stdVector< string_view > m_lines;
158  };
159 
165  class Cell
166  {
167 public:
170 
174  Cell();
175 
181  Cell( CellType cellType, TableLayout::Alignment alignment );
182 
189  Cell( CellType cellType, TableLayout::Alignment alignment, string_view value );
190 
196  Cell( Cell const & other );
197 
203  Cell( Cell && other );
204 
211  Cell & operator=( Cell const & other );
212 
219  Cell & operator=( Cell && other );
220 
225  { return m_text; }
226 
231  void setText( string_view text );
232 
237  void prepareLayout( size_t maxLineWidth );
238 
239 private:
241  string m_text;
242  };
243 
248  class Column
249  {
250 public:
253 
260 
265 
270  explicit Column( string_view name ):
271  Column( name, ColumnAlignement() )
272  {}
273 
279  Column( string_view name, ColumnAlignement alignment );
280 
286  { return m_parent; }
287 
292  Column const * getParent() const
293  { return m_parent; }
294 
299  void setParent( Column * parent )
300  { m_parent = parent; }
301 
306  { return m_next; }
307 
311  Column const * getNext() const
312  { return m_next; }
313 
317  void setNext( Column * nextCell )
318  { m_next = nextCell; }
319 
326 
333 
339  Column & setVisibility( bool visible );
340 
346  Column & addSubColumns( std::initializer_list< Column > subCol );
347 
353  Column & addSubColumns( std::initializer_list< string > subColNames );
354 
360  Column & addSubColumns( stdVector< string > const & subColNames );
361 
367  Column & addSubColumn( string_view subColName );
368 
374  Column & addSubColumn( Column const & subCol );
375 
381  Column & setHeaderAlignment( Alignment headerAlignment );
382 
388  Column & setValuesAlignment( Alignment valueAlignment );
389 
394  bool hasChild() const
395  { return !this->m_subColumns.empty(); }
396 
401  bool hasParent() const
402  { return this->m_parent != nullptr; }
403 
407  bool hasNext() const
408  { return this->m_next != nullptr; }
409 
413  bool isVisible() const
414  { return m_header.m_layout.m_cellType!=CellType::Hidden; }
415 
416 private:
418  Column * m_parent = nullptr;
420  Column * m_next = nullptr;
421  };
422 
428  {
429 public:
431  using ColumnType = Column const;
432 
438  DeepFirstIterator( ColumnType * columnPtr, size_t idxLayer ):
439  m_currentColumn( columnPtr ), m_currentLayer( idxLayer )
440  {}
441 
448  {
449  this->m_currentColumn = columnPtr;
450  return *this;
451  }
452 
458 
464 
470  { return *m_currentColumn; }
471 
475  ColumnType * getPtr() const
476  { return m_currentColumn; }
477 
483  { return m_currentColumn; }
484 
491  friend bool operator== ( DeepFirstIterator const & a, DeepFirstIterator const & b )
492  { return a.m_currentColumn == b.m_currentColumn; };
499  friend bool operator!= ( DeepFirstIterator const & a, DeepFirstIterator const & b )
500  { return a.m_currentColumn != b.m_currentColumn; };
501 
506  size_t getCurrentLayer() const
507  { return m_currentLayer; }
508 
509 private:
511  ColumnType * m_currentColumn;
513  size_t m_currentLayer;
514  };
515 
522 
529  { return DeepFirstIterator( nullptr, 0 ); }
530 
532  using TableLayoutArgs = std::initializer_list< std::variant< string_view, TableLayout::Column > >;
533 
536 
537 
538  TableLayout()
539  {
540  setMargin( MarginValue::medium );
541  }
542 
549  stdVector< TableLayout::Column > const & columns )
550  {
551  setMargin( MarginValue::medium );
552  setTitle( title );
553  addColumns( columns );
554  }
555 
562  TableLayoutArgs args )
563  {
564  setMargin( MarginValue::medium );
565  setTitle( title );
566  processArguments( args );
567  }
568 
575  {
576  setMargin( MarginValue::medium );
577  processArguments( args );
578  }
579 
586  stdVector< string > const & args )
587  {
588  setMargin( MarginValue::medium );
589  setTitle( title );
590  addColumns( args );
591  }
592 
596  ColumnsList const & getColumns() const
597  { return m_tableColumns; }
598 
603  { return m_tableColumns; }
604 
608  CellLayout const & getTitleLayout() const
609  { return m_tableTitleLayout; }
610 
615  { return m_tableTitleLayout; }
616 
621  { return m_tableTitleStr; }
622 
628 
634  TableLayout & enableLineBreak( bool value );
635 
642 
648  TableLayout & setMaxColumnWidth( size_t width );
649 
655  { return m_maxColumnWidth != noColumnMaxWidth; }
656 
660  bool isLineBreakEnabled() const;
661 
665  integer const & getBorderMargin() const
666  { return m_borderMargin; }
667 
671  integer const & getColumnMargin() const
672  { return m_columnMargin; }
673 
677  integer const & getMarginValue() const
678  { return m_marginValue; }
679 
683  size_t const & getMaxColumnWidth() const
684  { return m_maxColumnWidth; }
685 
690  void addColumns( stdVector< TableLayout::Column > const & columnNames );
691 
696  void addColumns( stdVector< string > const & columns );
697 
702  void addColumn( string_view columnName );
703 
708  void addColumn( TableLayout::Column const & column );
709 
710 protected:
711 
717  {
718  for( auto const & arg : args )
719  {
720  std::visit( [this]( auto const & value ) {
721  addColumn( value );
722  }, arg );
723  }
724  }
725 
730  template< typename ... Ts >
731  void processArguments( Ts &... args )
732  {
733  addColumns( args ... );
734  }
735 
738 
740  bool m_lineBreakAtBegin = true;
741 
744 
746  CellLayout m_tableTitleLayout = CellLayout( CellType::Header, Alignment::center );
747 
750 
751 
754 
757 
760 
761 };
762 
767 {
768 public:
769 
775 
787 
792 
798 
802  size_t getColumnLayersCount() const
803  { return m_columnLayersCount; }
804 
810  { return m_visibleLowermostColumnCount; }
811 
817  { return m_totalLowermostColumnCount; }
818 
819 private:
820 
821  // Number of column layers that a table layout has, default is 1;
822  size_t m_columnLayersCount;
823  // Numbers of lower most column
824  size_t m_totalLowermostColumnCount;
825 // Numbers of lower most column that are visible
826  size_t m_visibleLowermostColumnCount;
827 
832  void prepareLayoutRecusive( stdVector< TableLayout::Column > & columns, size_t level );
833 
834 };
835 }
836 
837 #endif /* GEOS_COMMON_FORMAT_TABLE_TABLELAYOUT_HPP */
Variation of the TableLayout to store precomputed layout information, ready to be formatted.
size_t getVisibleLowermostColumnCount() const
PreparedTableLayout()
Construct a default Table Formatter without layout specification (to only insert data in it,...
PreparedTableLayout(PreparedTableLayout const &)=delete
As prepared CellLayout & Column types have internal pointers, we cannot copy this class.
size_t getTotalLowermostColumnCount() const
PreparedTableLayout(TableLayout const &other)
Precompute various information for formatting from a configurated TableLayout:
PreparedTableLayout(PreparedTableLayout &&)=delete
as prepared CellLayout & Column types have internal pointers, we cannot move this class (SSO breaks s...
size_t getColumnLayersCount() const
Represents a cell in a table with ownership of its text data.
void setText(string_view text)
Set the full cell text.
string_view getText() const
CellLayout m_layout
The view & display setting on m_text.
Cell & operator=(Cell const &other)
Copy data, or throw an error if the layout has already been prepared (which means instance will refer...
Cell()
Constructor to initialize a Cell with a default settings. Use prepareLayout() after setup.
Cell & operator=(Cell &&other)
Move data, or throw an error if the layout has already been prepared (which means instance will refer...
Cell(CellType cellType, TableLayout::Alignment alignment)
Constructor to partially initialize a cell with display settings. Use prepareLayout() after setup.
void prepareLayout(size_t maxLineWidth)
Precompute m_layout display settings and link it with m_text.
Cell(Cell const &other)
Copy data, or throw an error if the layout has already been prepared (which means instance will refer...
Cell(CellType cellType, TableLayout::Alignment alignment, string_view value)
Constructor to partially initialize a cell with all settings. Use prepareLayout() after setup.
Cell(Cell &&other)
Move data, or throw an error if the layout has already been prepared (which means instance will refer...
View on cell data with information to display it in a table (content, type, alignment,...
Definition: TableLayout.hpp:82
std::vector< string_view > & getLines()
CellLayout(CellType cellType, TableLayout::Alignment alignment)
Constructor to fully initialize a cell with given celltype, text and alignment. m_cellWidth will be i...
void setWidth(size_t cellWidth)
Set the width of the cell, which must be constrained by the content lines length.
CellType m_cellType
The type of the cell (Header,Value, Merge, ...).
Definition: TableLayout.hpp:85
CellLayout()
Constructor to initialize a Cell with a default settings. Use prepareLayout() when setup.
void prepareLayout(string_view value, size_t maxLineWidth)
Set the data view to the given string_view & precompute display settings.
stdVector< string_view > const & getLines() const
Alignment m_alignment
The alignment of the cell (left, center, right).
Definition: TableLayout.hpp:87
CellLayout(CellType cellType)
Constructor to initialize an empty Cell of a given type.
Class representing a column in a table layout.
Column & addSubColumns(std::initializer_list< Column > subCol)
Adds multiple sub-columns to the column.
stdVector< Column > ColumnsList
Alias for the list of columns.
Column * getParent()
Get the parent column.
Column()
Construct a default column with no parameter (must be configurated).
Column & addSubColumn(string_view subColName)
Adds a single sub-column to the column.
ColumnAlignement m_alignment
struct containing m_alignment for the column (header and values)
string_view getName() const
Get the name of the column.
Column & setValuesAlignment(Alignment valueAlignment)
Sets the values alignment for the column.
Cell m_header
The header cell.
ColumnsList m_subColumns
A vector containing all sub-columns in the column.
Column const * getNext() const
void setNext(Column *nextCell)
Column & setVisibility(bool visible)
Set the column and its children visibility.
Column & addSubColumns(std::initializer_list< string > subColNames)
Adds multiple sub-columns to the column.
void setParent(Column *parent)
Set the parent column.
Column & addSubColumns(stdVector< string > const &subColNames)
Adds multiple sub-columns to the column.
Column & setName(string_view name)
Sets the name of the column.
Column const * getParent() const
Get the parent column.
bool hasChild() const
Checks if the column has any child columns.
Column & addSubColumn(Column const &subCol)
Adds a single sub-column to the column.
bool hasParent() const
Checks if the column has a parent column.
Column & setHeaderAlignment(Alignment headerAlignment)
Sets the header alignment for the column.
Column(string_view name, ColumnAlignement alignment)
Construct a default column with minimal parameters.
Column(string_view name)
Construct a default column with minimal parameters.
Iterator to loop over all columns, starting by the deepest sub columns, then to their parents,...
Column const ColumnType
alias for column
DeepFirstIterator(ColumnType *columnPtr, size_t idxLayer)
Construct a new Leaf Iterator object.
friend bool operator==(DeepFirstIterator const &a, DeepFirstIterator const &b)
Equality comparison operator.
DeepFirstIterator operator++(int)
Postfix ++ overload.
friend bool operator!=(DeepFirstIterator const &a, DeepFirstIterator const &b)
Inequality comparison operator.
DeepFirstIterator & operator++()
Prefix ++ overload.
ColumnType & operator*() const
Dereference operator.
DeepFirstIterator & operator=(ColumnType *columnPtr)
Copy assignment operator.
ColumnType * operator->() const
Arrow operator.
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
string m_tableTitleStr
Table title text.
DeepFirstIterator endDeepFirst() const
static constexpr Alignment defaultValueAlignment
default value for data cells alignement
Definition: TableLayout.hpp:50
void addColumns(stdVector< TableLayout::Column > const &columnNames)
Create and add columns to the columns vector given a string vector.
void addColumns(stdVector< string > const &columns)
Create and add columns to the columns vector given a string vector.
integer m_marginValue
The number of margin spaces around contents.
void processArguments(TableLayoutArgs args)
Add a column to the table given an initializer_list of string & Column.
integer const & getMarginValue() const
TableLayout & setMaxColumnWidth(size_t width)
Set the maximal width for each column.
TableLayout(string_view title, TableLayoutArgs args)
Construct a new Table Layout object.
CellLayout const & getTitleLayout() const
bool isMaxColumnWidthSet()
check if a column max width has been set
size_t m_maxColumnWidth
Max width for each column.
integer const & getColumnMargin() const
bool m_lineBreakAtBegin
Indicate if we have a line break a the beginning of the table.
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.
void processArguments(Ts &... args)
Alignment
Type of aligment for a column.
Definition: TableLayout.hpp:44
DeepFirstIterator beginDeepFirst() const
TableLayout & setMargin(MarginValue marginValue)
Set the minimal margin width between cell content and borders.
integer const & getBorderMargin() const
TableLayout(string_view title, stdVector< string > const &args)
Construct a new Table Layout object.
static constexpr size_t noColumnMaxWidth
default value for m_maxColumnWidth when it is not set
Definition: TableLayout.hpp:41
ColumnsList & getColumns()
TableLayout(TableLayoutArgs args)
Construct a new Table Layout object.
void addColumn(string_view columnName)
Create and add a column to the columns vector given a string.
CellLayout m_tableTitleLayout
Table title cell layout settings.
TableLayout & setTitle(string_view title)
integer m_borderMargin
The number of spaces at each table sides.
size_t const & getMaxColumnWidth() const
void addColumn(TableLayout::Column const &column)
Create and add a column to the columns vector given a Column.
static constexpr Alignment defaultHeaderAlignment
default value for columns header cells alignement
Definition: TableLayout.hpp:47
Section
Enumeration for table sections.
Definition: TableLayout.hpp:64
Column::ColumnsList ColumnsList
Alias for the list of columns.
ColumnsList m_tableColumns
Columns settings hierarchy.
ColumnsList const & getColumns() const
string_view getTitleStr() const
MarginValue
Space to apply between all data and border.
Definition: TableLayout.hpp:54
TableLayout(string_view title, stdVector< TableLayout::Column > const &columns)
Construct a new Table Layout object.
TableLayout & enableLineBreak(bool value)
Remove the return line at the end & begenning of the table.
CellLayout & getTitleLayout()
integer m_columnMargin
The number of character between two columns (spaces + the separacting character).
int integer
Signed integer type.
Definition: DataTypes.hpp:81
CellType
The different type a cell can handle.
Definition: TableTypes.hpp:33
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector
Structure to set up values m_alignment for each colum.
Definition: TableLayout.hpp:70
Alignment headerAlignment
Alignment for column name. By default aligned to center.
Definition: TableLayout.hpp:72
Alignment valueAlignment
Alignment for column values. By default aligned to right side.
Definition: TableLayout.hpp:74