LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::LArFormattingHelper::Table Class Reference

Table class. More...

#include "LArFormattingHelper.h"

Public Member Functions

 Table (const pandora::StringVector &columnTitles, const unsigned int precision=3)
 Table constructor. More...
 
void Print () const
 Print the table. More...
 
template<typename T >
void AddElement (const T &value, const Style style=REGULAR, const Color color=DEFAULT)
 Add an element to the table into the next (non-separator) column. More...
 

Private Member Functions

bool IsSeparatorColumn (const unsigned int column) const
 If the supplied column is a separator (vertical rule) More...
 
void PrintColumnTitles () const
 Print the column titles. More...
 
void PrintHorizontalLine () const
 Print a horizontal line. More...
 
void PrintTableElements () const
 Print the table elements. More...
 
void PrintTableCell (const std::string &value, const std::string &format, const unsigned int index) const
 Print a table cell. More...
 
void CheckAndSetSeparatorColumn ()
 Check if the next table cell is in a separator column, if so add a blank element. More...
 
void UpdateColumnWidth ()
 Update the width of the last column in which an element was added. More...
 

Private Attributes

const pandora::StringVector m_columnTitles
 The vector of columns titles in the table. More...
 
const unsigned int m_precision
 The number of significant figures to use when displaying number types. More...
 
pandora::StringVector m_elements
 The vector of flattened table elements. More...
 
pandora::StringVector m_format
 The formatting of each table element. More...
 
std::vector< unsigned int > m_widths
 The widths of each column (in units of number of characters) More...
 
std::stringstream m_stringstream
 The stringstream to print objects to. More...
 

Detailed Description

Table class.

Definition at line 128 of file LArFormattingHelper.h.

Constructor & Destructor Documentation

lar_content::LArFormattingHelper::Table::Table ( const pandora::StringVector &  columnTitles,
const unsigned int  precision = 3 
)

Table constructor.

Parameters
columnTitlesthe string columns titles use empty string for a separator column
precisionthe number of significant figures to display for number type table elements

Definition at line 88 of file LArFormattingHelper.cc.

References m_columnTitles, m_precision, m_stringstream, and m_widths.

88  :
89  m_columnTitles(columnTitles),
90  m_precision(precision)
91 {
92  m_stringstream.precision(m_precision);
93 
94  for (const std::string &title : m_columnTitles)
95  m_widths.push_back(title.length());
96 }
std::stringstream m_stringstream
The stringstream to print objects to.
const unsigned int m_precision
The number of significant figures to use when displaying number types.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)

Member Function Documentation

template<typename T >
void lar_content::LArFormattingHelper::Table::AddElement ( const T &  value,
const Style  style = REGULAR,
const Color  color = DEFAULT 
)
inline

Add an element to the table into the next (non-separator) column.

Parameters
valuethe element to add to the table
stylethe style of the element
colorthe color of the element

Definition at line 217 of file LArFormattingHelper.h.

References CheckAndSetSeparatorColumn(), lar_content::LArFormattingHelper::GetFormatCharacter(), m_elements, m_format, m_stringstream, and UpdateColumnWidth().

Referenced by lar_content::LArMonitoringHelper::PrintMatchingTable().

218 {
220 
221  if (!(m_stringstream << value))
222  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
223 
224  m_elements.push_back(static_cast<std::string>(m_stringstream.str()));
226  m_stringstream.str("");
227 
228  this->UpdateColumnWidth();
230 }
pandora::StringVector m_format
The formatting of each table element.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
std::stringstream m_stringstream
The stringstream to print objects to.
pandora::StringVector m_elements
The vector of flattened table elements.
double value
Definition: spectrum.C:18
void CheckAndSetSeparatorColumn()
Check if the next table cell is in a separator column, if so add a blank element. ...
std::size_t color(std::string const &procname)
void UpdateColumnWidth()
Update the width of the last column in which an element was added.
void lar_content::LArFormattingHelper::Table::CheckAndSetSeparatorColumn ( )
private

Check if the next table cell is in a separator column, if so add a blank element.

Definition at line 100 of file LArFormattingHelper.cc.

References m_columnTitles, m_elements, and m_format.

Referenced by AddElement().

101 {
102  if (m_columnTitles[m_elements.size() % m_columnTitles.size()] == "")
103  {
104  m_format.push_back("");
105  m_elements.push_back("");
106  }
107 }
pandora::StringVector m_format
The formatting of each table element.
pandora::StringVector m_elements
The vector of flattened table elements.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
bool lar_content::LArFormattingHelper::Table::IsSeparatorColumn ( const unsigned int  column) const
private

If the supplied column is a separator (vertical rule)

Parameters
columnthe column index to check
Returns
true/false

Definition at line 111 of file LArFormattingHelper.cc.

References m_columnTitles.

Referenced by PrintTableCell().

112 {
113  if (column >= m_columnTitles.size())
114  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
115 
116  return (m_columnTitles[column] == "");
117 }
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
void lar_content::LArFormattingHelper::Table::Print ( ) const

Print the table.

Definition at line 133 of file LArFormattingHelper.cc.

References m_columnTitles, PrintColumnTitles(), PrintHorizontalLine(), and PrintTableElements().

Referenced by lar_content::LArMonitoringHelper::PrintMatchingTable().

134 {
135  if (m_columnTitles.empty())
136  return;
137 
138  this->PrintColumnTitles();
139  this->PrintHorizontalLine();
140  this->PrintTableElements();
141 }
void PrintTableElements() const
Print the table elements.
void PrintHorizontalLine() const
Print a horizontal line.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
void PrintColumnTitles() const
Print the column titles.
void lar_content::LArFormattingHelper::Table::PrintColumnTitles ( ) const
private

Print the column titles.

Parameters
widthsa vector of the number of characters to include in each column

Definition at line 145 of file LArFormattingHelper.cc.

References lar_content::LArFormattingHelper::BOLD, lar_content::LArFormattingHelper::GetFormatCharacter(), m_columnTitles, and PrintTableCell().

Referenced by Print().

146 {
147  for (unsigned int i = 0, nColumns = m_columnTitles.size(); i < nColumns; ++i)
149 }
void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
Print a table cell.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
void lar_content::LArFormattingHelper::Table::PrintHorizontalLine ( ) const
private

Print a horizontal line.

Parameters
widthsa vector of the number of characters to include in each column

Definition at line 153 of file LArFormattingHelper.cc.

References lar_content::LArFormattingHelper::GetFormatCharacter(), m_columnTitles, m_widths, PrintTableCell(), and lar_content::LArFormattingHelper::REGULAR.

Referenced by Print().

154 {
155  for (unsigned int i = 0, nColumns = m_columnTitles.size(); i < nColumns; ++i)
157 }
void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
Print a table cell.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
void lar_content::LArFormattingHelper::Table::PrintTableCell ( const std::string &  value,
const std::string &  format,
const unsigned int  index 
) const
private

Print a table cell.

Parameters
valuethe string to print in the cell
formata formatting string (see GetFormatCharacter(...))
indexthe index of the table cell (0 –> number of elements) used to find the column
widthsa vector of the number of characters to include in each column

Definition at line 180 of file LArFormattingHelper.cc.

References IsSeparatorColumn(), m_columnTitles, m_widths, and lar_content::LArFormattingHelper::ResetStyle().

Referenced by PrintColumnTitles(), PrintHorizontalLine(), and PrintTableElements().

181 {
182  if (m_columnTitles.empty())
183  return;
184 
185  const unsigned int column(index % m_widths.size());
186 
187  const std::string separator(this->IsSeparatorColumn(column) ? "" : " ");
188  std::cout << "|" << format << separator << std::setw(m_widths[column]) << std::internal << value << separator
189  << std::resetiosflags(std::ios::adjustfield);
191 
192  if (column == m_columnTitles.size() - 1)
193  std::cout << "|" << std::endl;
194 }
bool IsSeparatorColumn(const unsigned int column) const
If the supplied column is a separator (vertical rule)
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
double value
Definition: spectrum.C:18
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
static void ResetStyle(std::ostream &stream=std::cout)
Reset the style of the standard output stream.
void lar_content::LArFormattingHelper::Table::PrintTableElements ( ) const
private

Print the table elements.

Parameters
widthsa vector of the number of characters to include in each column

Definition at line 161 of file LArFormattingHelper.cc.

References m_columnTitles, m_elements, m_format, and PrintTableCell().

Referenced by Print().

162 {
163  if (m_columnTitles.empty())
164  return;
165 
166  const unsigned int nRows((m_elements.size() + m_columnTitles.size() - 1) / m_columnTitles.size());
167  const unsigned int nColumns(m_columnTitles.size());
168  if (nRows * nColumns != m_elements.size())
169  {
170  std::cout << "LArFormattingHelper::Table::PrintTableElements - Error: Number of table elements added doesn't fill a whole row" << std::endl;
171  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
172  }
173 
174  for (unsigned int i = 0; i < nRows * nColumns; i++)
175  this->PrintTableCell(m_elements[i], m_format[i], i);
176 }
void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
Print a table cell.
pandora::StringVector m_format
The formatting of each table element.
pandora::StringVector m_elements
The vector of flattened table elements.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
void lar_content::LArFormattingHelper::Table::UpdateColumnWidth ( )
private

Update the width of the last column in which an element was added.

Definition at line 121 of file LArFormattingHelper.cc.

References m_elements, and m_widths.

Referenced by AddElement().

122 {
123  if (m_widths.empty() || m_elements.empty())
124  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
125 
126  const unsigned int currentElementIndex(m_elements.size() - 1);
127  const unsigned int column(currentElementIndex % m_widths.size());
128  m_widths[column] = std::max(static_cast<unsigned int>(m_elements[currentElementIndex].length()), m_widths[column]);
129 }
pandora::StringVector m_elements
The vector of flattened table elements.
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)

Member Data Documentation

const pandora::StringVector lar_content::LArFormattingHelper::Table::m_columnTitles
private

The vector of columns titles in the table.

Definition at line 205 of file LArFormattingHelper.h.

Referenced by CheckAndSetSeparatorColumn(), IsSeparatorColumn(), Print(), PrintColumnTitles(), PrintHorizontalLine(), PrintTableCell(), PrintTableElements(), and Table().

pandora::StringVector lar_content::LArFormattingHelper::Table::m_elements
private

The vector of flattened table elements.

Definition at line 207 of file LArFormattingHelper.h.

Referenced by AddElement(), CheckAndSetSeparatorColumn(), PrintTableElements(), and UpdateColumnWidth().

pandora::StringVector lar_content::LArFormattingHelper::Table::m_format
private

The formatting of each table element.

Definition at line 208 of file LArFormattingHelper.h.

Referenced by AddElement(), CheckAndSetSeparatorColumn(), and PrintTableElements().

const unsigned int lar_content::LArFormattingHelper::Table::m_precision
private

The number of significant figures to use when displaying number types.

Definition at line 206 of file LArFormattingHelper.h.

Referenced by Table().

std::stringstream lar_content::LArFormattingHelper::Table::m_stringstream
private

The stringstream to print objects to.

Definition at line 210 of file LArFormattingHelper.h.

Referenced by AddElement(), and Table().

std::vector<unsigned int> lar_content::LArFormattingHelper::Table::m_widths
private

The widths of each column (in units of number of characters)

Definition at line 209 of file LArFormattingHelper.h.

Referenced by PrintHorizontalLine(), PrintTableCell(), Table(), and UpdateColumnWidth().


The documentation for this class was generated from the following files: