NCBI C++ ToolKit
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CTablePrinter Class Reference

Search Toolkit Book for CTablePrinter

This can be used to lay out neat ASCII data. More...

#include <util/table_printer.hpp>

+ Collaboration diagram for CTablePrinter:

Classes

struct  SColInfo
 This structure contains info about a given column. More...
 
struct  SColInfoVec
 This holds the info about all columns for the table. More...
 
struct  SEndOfCell
 Stream an instance of this object into the CTablePrinter to have it write out the current table cell and prepare for the next one. More...
 

Public Types

enum  EDataTooLong {
  eDataTooLong_ShowErrorInColumn , eDataTooLong_TruncateWithEllipses , eDataTooLong_ShowWholeData , eDataTooLong_ThrowException ,
  eDataTooLong_Default = eDataTooLong_ShowErrorInColumn
}
 controls how table should behave when a data cell is too long to fit into its column. More...
 
enum  EJustify { eJustify_Left , eJustify_Right }
 controls how the data in each column is justified. More...
 

Public Member Functions

 CTablePrinter (const SColInfoVec &vecColInfo, ostream &ostrm, const string &sColumnSeparator=" ")
 
 ~CTablePrinter (void)
 Destructor finishes the table if it's not already finished. More...
 
template<class TValue >
CTablePrinterStreamToCurrentCell (const TValue &value)
 This is just a helper for the global "operator <<" function for writing into the table. More...
 
CTablePrinterEndOfCurrentCell (void)
 This writes the contents of the current cell and prepares for the next one. More...
 
void FinishTable (void)
 If the table is not already finished, this finishes it by writing the closing row (which is usually a series of dashes). More...
 

Private Types

enum  EState { eState_Initial , eState_PrintingRows }
 This keeps track of the state of the table writer. More...
 

Private Member Functions

 CTablePrinter (const CTablePrinter &)
 forbid copy More...
 
CTablePrinteroperator= (const CTablePrinter &)
 forbid assignment More...
 
void x_PrintDashes (void)
 This writes a row of dashes which starts the table, separates the header from the data, and ends the table. More...
 
void x_PrintColumnNames (void)
 This prints the column names, appropriately spaced and separated. More...
 
void x_AddCellValue (const string &sValue)
 This is the underlying logic to add another cell to the table data. More...
 

Private Attributes

EState m_eState
 This keeps track of the state of the table writer. More...
 
SColInfoVec m_vecColInfo
 The info about columns of the table. More...
 
ostream & m_ostrm
 The ostream to which the table is written. More...
 
Uint4 m_iNextCol
 The 0-based index of the column that the next AddCell will use. More...
 
const string m_sColumnSeparator
 The text that separates columns (both in the header as well as dat). More...
 
stringstream m_NextCellContents
 The contents of the current table cell are accumulated in here. More...
 

Detailed Description

This can be used to lay out neat ASCII data.

Example:

--------------------   --------
Name                   ZIP code
--------------------   --------
Pat Doe                22801   
Sammy Smith            20852   
Chris Doe              08361   
--------------------   --------

Here is example code that would let you lay out such a table into cout:

CTablePrinter::SColInfoVec vecColInfo;
vecColInfo.AddCol("Name", 20);
vecColInfo.AddCol("ZIP code", 5);

typedef CTablePrinter::SEndOfCell SEndOfCell;

CTablePrinter table_printer(vecColInfo, cout);

table_printer << "Pat" << ' ' << "Doe" << SEndOfCell();
table_printer << "22801" << SEndOfCell();

table_printer << "Sammy Smith" << SEndOfCell();
table_printer << "20852" << SEndOfCell();

table_printer << "Chris Doe" << SEndOfCell();
table_printer << "08361" << SEndOfCell();

table_printer.FinishTable();

Definition at line 78 of file table_printer.hpp.

Member Enumeration Documentation

◆ EDataTooLong

controls how table should behave when a data cell is too long to fit into its column.

Enumerator
eDataTooLong_ShowErrorInColumn 

The data will be replaced with some sort of error message, or at least question marks if the error message wouldn't fit.

eDataTooLong_TruncateWithEllipses 

The data is truncated with ellipses (that is, "...").

This is NOT recommended for numeric data because a truncated number can look like a different number if the viewer doesn't notice the ellipses.

eDataTooLong_ShowWholeData 

This recklessly prints the whole table data, regardless of formatting corruption and regardless of how long the data is.

eDataTooLong_ThrowException 

Throws an exception when the data won't fit.

eDataTooLong_Default 

Default behavior.

Definition at line 83 of file table_printer.hpp.

◆ EJustify

controls how the data in each column is justified.

As a rough rule of thumb, numeric data should be right-justified and non-numeric data should be left-justified.

Enumerator
eJustify_Left 
eJustify_Right 

Definition at line 107 of file table_printer.hpp.

◆ EState

enum CTablePrinter::EState
private

This keeps track of the state of the table writer.

Enumerator
eState_Initial 

This means the header row has NOT been printed yet.

eState_PrintingRows 

This means the header row HAS been printed, and data rows are being printed.

Definition at line 207 of file table_printer.hpp.

Constructor & Destructor Documentation

◆ CTablePrinter() [1/2]

CTablePrinter::CTablePrinter ( const SColInfoVec vecColInfo,
ostream &  ostrm,
const string sColumnSeparator = "   " 
)
Parameters
vecColInfoThis holds information about all the columns that will be shown in the table.
ostrmThis is the output stream to which the table will be written
sColumnSeparatorDefault should be fine for most purposes, but the caller can separate columns with something else (for example, " | ").

Definition at line 53 of file table_printer.cpp.

References CTablePrinter::SColInfoVec::m_colInfoVec, m_vecColInfo, and NON_CONST_ITERATE.

◆ ~CTablePrinter()

CTablePrinter::~CTablePrinter ( void  )
inline

Destructor finishes the table if it's not already finished.

See FinishTable().

Definition at line 161 of file table_printer.hpp.

References FinishTable().

◆ CTablePrinter() [2/2]

CTablePrinter::CTablePrinter ( const CTablePrinter )
private

forbid copy

Member Function Documentation

◆ EndOfCurrentCell()

CTablePrinter& CTablePrinter::EndOfCurrentCell ( void  )
inline

This writes the contents of the current cell and prepares for the next one.

This is really just a helper for the "operator <<" that accepts SEndOfCell.

Definition at line 185 of file table_printer.hpp.

References kEmptyStr, m_NextCellContents, and x_AddCellValue().

Referenced by operator<<< CTablePrinter::SEndOfCell >().

◆ FinishTable()

void CTablePrinter::FinishTable ( void  )

If the table is not already finished, this finishes it by writing the closing row (which is usually a series of dashes).

If cells are added after this point, a new header will be printed and a new table will be written.

Definition at line 71 of file table_printer.cpp.

References eState_Initial, eState_PrintingRows, m_eState, NCBI_USER_THROW_FMT, and x_PrintDashes().

Referenced by ~CTablePrinter().

◆ operator=()

CTablePrinter& CTablePrinter::operator= ( const CTablePrinter )
private

forbid assignment

◆ StreamToCurrentCell()

template<class TValue >
CTablePrinter& CTablePrinter::StreamToCurrentCell ( const TValue &  value)
inline

This is just a helper for the global "operator <<" function for writing into the table.

Definition at line 175 of file table_printer.hpp.

References m_NextCellContents, and rapidjson::value.

Referenced by operator<<().

◆ x_AddCellValue()

void CTablePrinter::x_AddCellValue ( const string sValue)
private

◆ x_PrintColumnNames()

void CTablePrinter::x_PrintColumnNames ( void  )
private

This prints the column names, appropriately spaced and separated.

Definition at line 100 of file table_printer.cpp.

References ITERATE, kEmptyStr, CTablePrinter::SColInfoVec::m_colInfoVec, m_ostrm, m_sColumnSeparator, and m_vecColInfo.

Referenced by x_AddCellValue().

◆ x_PrintDashes()

void CTablePrinter::x_PrintDashes ( void  )
private

This writes a row of dashes which starts the table, separates the header from the data, and ends the table.

Definition at line 87 of file table_printer.cpp.

References ITERATE, kEmptyStr, CTablePrinter::SColInfoVec::m_colInfoVec, m_ostrm, m_sColumnSeparator, m_vecColInfo, and string.

Referenced by FinishTable(), and x_AddCellValue().

Member Data Documentation

◆ m_eState

EState CTablePrinter::m_eState
private

This keeps track of the state of the table writer.

Definition at line 215 of file table_printer.hpp.

Referenced by FinishTable(), and x_AddCellValue().

◆ m_iNextCol

Uint4 CTablePrinter::m_iNextCol
private

The 0-based index of the column that the next AddCell will use.

Definition at line 222 of file table_printer.hpp.

Referenced by x_AddCellValue().

◆ m_NextCellContents

stringstream CTablePrinter::m_NextCellContents
private

The contents of the current table cell are accumulated in here.

Definition at line 228 of file table_printer.hpp.

Referenced by EndOfCurrentCell(), and StreamToCurrentCell().

◆ m_ostrm

ostream& CTablePrinter::m_ostrm
private

The ostream to which the table is written.

Definition at line 220 of file table_printer.hpp.

Referenced by x_AddCellValue(), x_PrintColumnNames(), and x_PrintDashes().

◆ m_sColumnSeparator

const string CTablePrinter::m_sColumnSeparator
private

The text that separates columns (both in the header as well as dat).

Definition at line 224 of file table_printer.hpp.

Referenced by x_AddCellValue(), x_PrintColumnNames(), and x_PrintDashes().

◆ m_vecColInfo

SColInfoVec CTablePrinter::m_vecColInfo
private

The info about columns of the table.

Definition at line 218 of file table_printer.hpp.

Referenced by CTablePrinter(), x_AddCellValue(), x_PrintColumnNames(), and x_PrintDashes().


The documentation for this class was generated from the following files:
Modified on Mon May 20 05:05:25 2024 by modify_doxy.py rev. 669887