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 "TableTypes.hpp"
24 #include <variant>
25 
26 
27 namespace geos
28 {
29 
34 {
35 
36 public:
37 
39  static constexpr size_t noColumnMaxWidth = std::numeric_limits< size_t >::max();
40 
42  enum Alignment { right, left, center };
43 
46  {
47  tiny = 0,
48  small = 1,
49  medium = 2,
50  large = 3
51  };
52 
56  enum Section { header, values };
57 
62  {
64  Alignment headerAlignment = Alignment::center;
66  Alignment valueAlignment = Alignment::right;
67  };
68 
73  class CellLayout
74  {
75 public:
80 
85 
90  CellLayout( CellType cellType );
91 
98  CellLayout( CellType cellType, TableLayout::Alignment alignment );
99 
103  size_t getWidth() const
104  { return m_cellWidth; }
105 
110  void setWidth( size_t cellWidth )
111  { m_cellWidth = cellWidth; }
112 
117  { return m_lines; }
118 
122  std::vector< string_view > & getLines()
123  { return m_lines; }
124 
128  size_t getHeight() const
129  { return m_lines.size(); }
130 
134  size_t isEmpty() const
135  { return m_lines.empty() || m_lines[0].empty(); }
136 
143  void prepareLayout( string_view value, size_t maxLineWidth );
144 
145 private:
147  size_t m_cellWidth;
149  stdVector< string_view > m_lines;
150  };
151 
157  class Cell
158  {
159 public:
162 
166  Cell();
167 
173  Cell( CellType cellType, TableLayout::Alignment alignment );
174 
181  Cell( CellType cellType, TableLayout::Alignment alignment, string_view value );
182 
188  Cell( Cell const & other );
189 
195  Cell( Cell && other );
196 
203  Cell & operator=( Cell const & other );
204 
211  Cell & operator=( Cell && other );
212 
217  { return m_text; }
218 
223  void setText( string_view text );
224 
229  void prepareLayout( size_t maxLineWidth );
230 
231 private:
233  string m_text;
234  };
235 
240  class Column
241  {
242 public:
245 
252 
257 
262  explicit Column( string_view name ):
263  Column( name, ColumnAlignement() )
264  {}
265 
271  Column( string_view name, ColumnAlignement alignment );
272 
278  { return m_parent; }
279 
284  Column const * getParent() const
285  { return m_parent; }
286 
291  void setParent( Column * parent )
292  { m_parent = parent; }
293 
298  { return m_next; }
299 
303  Column const * getNext() const
304  { return m_next; }
305 
309  void setNext( Column * nextCell )
310  { m_next = nextCell; }
311 
318 
325 
331  Column & setVisibility( bool visible );
332 
338  Column & addSubColumns( std::initializer_list< Column > subCol );
339 
345  Column & addSubColumns( std::initializer_list< string > subColNames );
346 
352  Column & addSubColumns( stdVector< string > const & subColNames );
353 
359  Column & addSubColumn( string_view subColName );
360 
366  Column & addSubColumn( Column const & subCol );
367 
373  Column & setHeaderAlignment( Alignment headerAlignment );
374 
380  Column & setValuesAlignment( Alignment valueAlignment );
381 
386  bool hasChild() const
387  { return !this->m_subColumns.empty(); }
388 
393  bool hasParent() const
394  { return this->m_parent != nullptr; }
395 
399  bool hasNext() const
400  { return this->m_next != nullptr; }
401 
405  bool isVisible() const
406  { return m_header.m_layout.m_cellType!=CellType::Hidden; }
407 
408 private:
410  Column * m_parent = nullptr;
412  Column * m_next = nullptr;
413  };
414 
420  {
421 public:
423  using ColumnType = Column const;
424 
430  DeepFirstIterator( ColumnType * columnPtr, size_t idxLayer ):
431  m_currentColumn( columnPtr ), m_currentLayer( idxLayer )
432  {}
433 
440  {
441  this->m_currentColumn = columnPtr;
442  return *this;
443  }
444 
450 
456 
462  { return *m_currentColumn; }
463 
467  ColumnType * getPtr() const
468  { return m_currentColumn; }
469 
475  { return m_currentColumn; }
476 
483  friend bool operator== ( DeepFirstIterator const & a, DeepFirstIterator const & b )
484  { return a.m_currentColumn == b.m_currentColumn; };
491  friend bool operator!= ( DeepFirstIterator const & a, DeepFirstIterator const & b )
492  { return a.m_currentColumn != b.m_currentColumn; };
493 
498  size_t getCurrentLayer() const
499  { return m_currentLayer; }
500 
501 private:
503  ColumnType * m_currentColumn;
505  size_t m_currentLayer;
506  };
507 
514 
521  { return DeepFirstIterator( nullptr, 0 ); }
522 
524  using TableLayoutArgs = std::initializer_list< std::variant< string_view, TableLayout::Column > >;
525 
528 
529 
530  TableLayout()
531  {
532  setMargin( MarginValue::medium );
533  }
534 
541  stdVector< TableLayout::Column > const & columns )
542  {
543  setMargin( MarginValue::medium );
544  setTitle( title );
545  addColumns( columns );
546  }
547 
554  TableLayoutArgs args )
555  {
556  setMargin( MarginValue::medium );
557  setTitle( title );
558  processArguments( args );
559  }
560 
567  {
568  setMargin( MarginValue::medium );
569  processArguments( args );
570  }
571 
578  stdVector< string > const & args )
579  {
580  setMargin( MarginValue::medium );
581  setTitle( title );
582  addColumns( args );
583  }
584 
588  ColumnsList const & getColumns() const
589  { return m_tableColumns; }
590 
595  { return m_tableColumns; }
596 
600  CellLayout const & getTitleLayout() const
601  { return m_tableTitleLayout; }
602 
607  { return m_tableTitleLayout; }
608 
613  { return m_tableTitleStr; }
614 
619  { return m_defaultHeaderAlignment; }
620 
625  { return m_defaultValueAlignment; }
626 
632 
638  TableLayout & enableLineBreak( bool value );
639 
646 
652  TableLayout & setMaxColumnWidth( size_t width );
653 
659  TableLayout & setIndentation( size_t spacesCount );
660 
667 
674 
680  { return m_maxColumnWidth != noColumnMaxWidth; }
681 
685  bool isLineBreakEnabled() const;
686 
690  integer const & getBorderMargin() const
691  { return m_borderMargin; }
692 
696  integer const & getColumnMargin() const
697  { return m_columnMargin; }
698 
702  integer const & getMarginValue() const
703  { return m_marginValue; }
704 
708  size_t const & getMaxColumnWidth() const
709  { return m_maxColumnWidth; }
710 
714  size_t const & getIndentation() const
715  { return m_indentation; }
716 
722  TableLayout & addColumns( stdVector< Column > const & columnNames );
723 
730 
737 
744 
750  TableLayout & addColumn( Column const & column );
751 
752 protected:
753 
759  {
760  for( auto const & arg : args )
761  {
762  std::visit( [this]( auto const & value ) {
763  addColumn( value );
764  }, arg );
765  }
766  }
767 
772  template< typename ... Ts >
773  void processArguments( Ts &... args )
774  {
775  addColumns( args ... );
776  }
777 
780 
782  bool m_lineBreakAtBegin = true;
783 
786 
788  CellLayout m_tableTitleLayout = CellLayout( CellType::Header, Alignment::center );
789 
792 
793 
796 
799 
802 
804  size_t m_indentation = 0;
805 
807  Alignment m_defaultHeaderAlignment = Alignment::center;
808 
810  Alignment m_defaultValueAlignment = Alignment::right;
811 
812 };
813 
818 {
819 public:
820 
826 
838 
843 
849 
853  size_t getColumnLayersCount() const
854  { return m_columnLayersCount; }
855 
861  { return m_visibleLowermostColumnCount; }
862 
868  { return m_totalLowermostColumnCount; }
869 
874  { return m_indentationStr; }
875 
876 private:
877 
878  // Number of column layers that a table layout has, default is 1;
879  size_t m_columnLayersCount;
880  // Numbers of lower most column
881  size_t m_totalLowermostColumnCount;
882 // Numbers of lower most column that are visible
883  size_t m_visibleLowermostColumnCount;
884 
885  string m_indentationStr;
886 
891  void prepareLayoutRecusive( stdVector< TableLayout::Column > & columns, size_t level );
892 
893 };
894 }
895 
896 #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
string_view getIndentationStr() 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:74
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:77
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:79
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:34
size_t const & getIndentation() const
bool isLineBreakEnabled() const
string m_tableTitleStr
Table title text.
DeepFirstIterator endDeepFirst() const
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
Alignment m_defaultValueAlignment
default value for data cells alignement.
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
size_t m_indentation
The number of spaces at the left of the table.
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.
TableLayout & addColumns(stdVector< Column > const &columnNames)
Create and add columns to the columns vector given a string vector.
TableLayout & addColumns(TableLayoutArgs columns)
Create and add columns to the columns vector given a string and/or columns.
void processArguments(Ts &... args)
Alignment
Type of aligment for a column.
Definition: TableLayout.hpp:42
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.
TableLayout & setDefaultValueAlignment(Alignment alignment)
Sets the default value for data cells alignement. Used with column-free layout.
static constexpr size_t noColumnMaxWidth
default value for m_maxColumnWidth when it is not set
Definition: TableLayout.hpp:39
ColumnsList & getColumns()
TableLayout & addColumn(string_view columnName)
Create and add a column to the columns vector given a string.
TableLayout & setDefaultHeaderAlignment(Alignment alignment)
Sets the default value for columns header cells alignement. Used with column-free layout.
TableLayout(TableLayoutArgs args)
Construct a new Table Layout object.
Alignment getDefaultHeaderAlignment() const
CellLayout m_tableTitleLayout
Table title cell layout settings.
TableLayout & setIndentation(size_t spacesCount)
Set the indentation of the whole table.
Alignment m_defaultHeaderAlignment
default value for columns header cells alignement.
TableLayout & setTitle(string_view title)
integer m_borderMargin
The number of spaces at each table sides.
size_t const & getMaxColumnWidth() const
Section
Enumeration for table sections.
Definition: TableLayout.hpp:56
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:46
TableLayout(string_view title, stdVector< TableLayout::Column > const &columns)
Construct a new Table Layout object.
TableLayout & addColumns(stdVector< string > const &columns)
Create and add columns to the columns vector given a string vector.
TableLayout & enableLineBreak(bool value)
Remove the return line at the end & begenning of the table.
CellLayout & getTitleLayout()
Alignment getDefaultValueAlignment() const
TableLayout & addColumn(Column const &column)
Create and add a column to the columns vector given a Column.
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:35
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:62
Alignment headerAlignment
Alignment for column name. By default aligned to center.
Definition: TableLayout.hpp:64
Alignment valueAlignment
Alignment for column values. By default aligned to right side.
Definition: TableLayout.hpp:66