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 
45  static constexpr Alignment defaultHeaderAlignment = Alignment::center;
46 
48  static constexpr Alignment defaultValueAlignment = Alignment::right;
49 
52  {
53  tiny = 0,
54  small = 1,
55  medium = 2,
56  large = 3
57  };
58 
62  enum Section { header, values };
63 
68  {
73  };
74 
79  class CellLayout
80  {
81 public:
86 
91 
96  CellLayout( CellType cellType );
97 
105 
109  size_t getWidth() const
110  { return m_cellWidth; }
111 
116  void setWidth( size_t cellWidth )
117  { m_cellWidth = cellWidth; }
118 
123  { return m_lines; }
124 
128  std::vector< string_view > & getLines()
129  { return m_lines; }
130 
134  size_t getHeight() const
135  { return m_lines.size(); }
136 
140  size_t isEmpty() const
141  { return m_lines.empty() || m_lines[0].empty(); }
142 
149  void prepareLayout( string_view value, size_t maxLineWidth );
150 
151 private:
153  size_t m_cellWidth;
155  stdVector< string_view > m_lines;
156  };
157 
163  class Cell
164  {
165 public:
168 
172  Cell();
173 
179  Cell( CellType cellType, TableLayout::Alignment alignment );
180 
187  Cell( CellType cellType, TableLayout::Alignment alignment, string_view value );
188 
194  Cell( Cell const & other );
195 
201  Cell( Cell && other );
202 
209  Cell & operator=( Cell const & other );
210 
217  Cell & operator=( Cell && other );
218 
223  { return m_text; }
224 
229  void setText( string_view text );
230 
235  void prepareLayout( size_t maxLineWidth );
236 
237 private:
239  string m_text;
240  };
241 
246  class Column
247  {
248 public:
251 
258 
263 
268  explicit Column( string_view name ):
269  Column( name, ColumnAlignement() )
270  {}
271 
277  Column( string_view name, ColumnAlignement alignment );
278 
284  { return m_parent; }
285 
290  Column const * getParent() const
291  { return m_parent; }
292 
297  void setParent( Column * parent )
298  { m_parent = parent; }
299 
304  { return m_next; }
305 
309  Column const * getNext() const
310  { return m_next; }
311 
315  void setNext( Column * nextCell )
316  { m_next = nextCell; }
317 
324 
331 
337  Column & setVisibility( bool visible );
338 
344  Column & addSubColumns( std::initializer_list< Column > subCol );
345 
351  Column & addSubColumns( std::initializer_list< string > subColNames );
352 
358  Column & addSubColumns( stdVector< string > const & subColNames );
359 
365  Column & addSubColumn( string_view subColName );
366 
372  Column & addSubColumn( Column const & subCol );
373 
379  Column & setHeaderAlignment( Alignment headerAlignment );
380 
386  Column & setValuesAlignment( Alignment valueAlignment );
387 
392  bool hasChild() const
393  { return !this->m_subColumns.empty(); }
394 
399  bool hasParent() const
400  { return this->m_parent != nullptr; }
401 
405  bool hasNext() const
406  { return this->m_next != nullptr; }
407 
411  bool isVisible() const
412  { return m_header.m_layout.m_cellType!=CellType::Hidden; }
413 
414 private:
416  Column * m_parent = nullptr;
418  Column * m_next = nullptr;
419  };
420 
426  {
427 public:
429  using ColumnType = Column const;
430 
436  DeepFirstIterator( ColumnType * columnPtr, size_t idxLayer ):
437  m_currentColumn( columnPtr ), m_currentLayer( idxLayer )
438  {}
439 
446  {
447  this->m_currentColumn = columnPtr;
448  return *this;
449  }
450 
456 
462 
468  { return *m_currentColumn; }
469 
473  ColumnType * getPtr() const
474  { return m_currentColumn; }
475 
481  { return m_currentColumn; }
482 
489  friend bool operator== ( DeepFirstIterator const & a, DeepFirstIterator const & b )
490  { return a.m_currentColumn == b.m_currentColumn; };
497  friend bool operator!= ( DeepFirstIterator const & a, DeepFirstIterator const & b )
498  { return a.m_currentColumn != b.m_currentColumn; };
499 
504  size_t getCurrentLayer() const
505  { return m_currentLayer; }
506 
507 private:
509  ColumnType * m_currentColumn;
511  size_t m_currentLayer;
512  };
513 
520 
527  { return DeepFirstIterator( nullptr, 0 ); }
528 
530  using TableLayoutArgs = std::initializer_list< std::variant< string_view, TableLayout::Column > >;
531 
534 
535 
536  TableLayout()
537  {
538  setMargin( MarginValue::medium );
539  }
540 
547  stdVector< TableLayout::Column > const & columns )
548  {
549  setMargin( MarginValue::medium );
550  setTitle( title );
551  addColumns( columns );
552  }
553 
560  TableLayoutArgs args )
561  {
562  setMargin( MarginValue::medium );
563  setTitle( title );
564  processArguments( args );
565  }
566 
573  {
574  setMargin( MarginValue::medium );
575  processArguments( args );
576  }
577 
584  stdVector< string > const & args )
585  {
586  setMargin( MarginValue::medium );
587  setTitle( title );
588  addColumns( args );
589  }
590 
594  ColumnsList const & getColumns() const
595  { return m_tableColumns; }
596 
601  { return m_tableColumns; }
602 
606  CellLayout const & getTitleLayout() const
607  { return m_tableTitleLayout; }
608 
613  { return m_tableTitleLayout; }
614 
619  { return m_tableTitleStr; }
620 
626 
632  TableLayout & enableLineBreak( bool value );
633 
640 
646  TableLayout & setMaxColumnWidth( size_t width );
647 
653  { return m_maxColumnWidth != noColumnMaxWidth; }
654 
658  bool isLineBreakEnabled() const;
659 
663  integer const & getBorderMargin() const
664  { return m_borderMargin; }
665 
669  integer const & getColumnMargin() const
670  { return m_columnMargin; }
671 
675  integer const & getMarginValue() const
676  { return m_marginValue; }
677 
681  size_t const & getMaxColumnWidth() const
682  { return m_maxColumnWidth; }
683 
688  void addColumns( stdVector< TableLayout::Column > const & columnNames );
689 
694  void addColumns( stdVector< string > const & columns );
695 
700  void addColumn( string_view columnName );
701 
706  void addColumn( TableLayout::Column const & column );
707 
708 protected:
709 
715  {
716  for( auto const & arg : args )
717  {
718  std::visit( [this]( auto const & value ) {
719  addColumn( value );
720  }, arg );
721  }
722  }
723 
728  template< typename ... Ts >
729  void processArguments( Ts &... args )
730  {
731  addColumns( args ... );
732  }
733 
736 
738  bool m_lineBreakAtBegin = true;
739 
742 
744  CellLayout m_tableTitleLayout = CellLayout( CellType::Header, Alignment::center );
745 
748 
749 
752 
755 
758 
759 };
760 
765 {
766 public:
767 
773 
785 
790 
796 
800  size_t getColumnLayersCount() const
801  { return m_columnLayersCount; }
802 
808  { return m_visibleLowermostColumnCount; }
809 
815  { return m_totalLowermostColumnCount; }
816 
817 private:
818 
819  // Number of column layers that a table layout has, default is 1;
820  size_t m_columnLayersCount;
821  // Numbers of lower most column
822  size_t m_totalLowermostColumnCount;
823 // Numbers of lower most column that are visible
824  size_t m_visibleLowermostColumnCount;
825 
830  void prepareLayoutRecusive( stdVector< TableLayout::Column > & columns, size_t level );
831 
832 };
833 }
834 
835 #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:80
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:83
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:85
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
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:48
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: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.
static constexpr size_t noColumnMaxWidth
default value for m_maxColumnWidth when it is not set
Definition: TableLayout.hpp:39
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:45
Section
Enumeration for table sections.
Definition: TableLayout.hpp:62
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:52
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: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:68
Alignment headerAlignment
Alignment for column name. By default aligned to center.
Definition: TableLayout.hpp:70
Alignment valueAlignment
Alignment for column values. By default aligned to right side.
Definition: TableLayout.hpp:72