C++ Samples
Demonstrates how to create a C++ application using the VideoXpert SDK
CppSamples::Common::TabularPrinter Class Reference

This class helps to print a table data with alignment. More...

#include <TabularPrinter.h>

Public Member Functions

 TabularPrinter ()
 Create an instance of TabularPrinter class. More...
 
 ~TabularPrinter ()
 
void AddColumn (std::string heading, int width, int alignment=-1)
 Add a column to the table. More...
 
void AddRow ()
 Add a row to the table. More...
 
void AddCellValue (std::string value)
 Add a std::string value to the current cell in the table and forward the pointer to next cell. More...
 
void AddCellValue (int value)
 Add a std::string value to the current cell in the table and forward the pointer to next cell. More...
 
std::string ToString ()
 Convert the table to std::string and return it. More...
 

Private Member Functions

std::string PrintRow (Row *row)
 
std::string GetHeader ()
 

Static Private Member Functions

static std::string GetAlignedCellValue (std::string actualValue, ColumnHeading *columnHeading)
 

Private Attributes

int _columnCount
 
std::vector< ColumnHeading * > _headings
 
std::vector< Row * > _rows
 

Detailed Description

This class helps to print a table data with alignment.

Definition at line 29 of file TabularPrinter.h.

Constructor & Destructor Documentation

TabularPrinter::TabularPrinter ( )

Create an instance of TabularPrinter class.

Create an instance of Paging class.

Definition at line 10 of file TabularPrinter.cpp.

10  {
11  _columnCount = 0;
12 
13  vector<string> vv;
14 }
CppSamples::Common::TabularPrinter::~TabularPrinter ( )
inline

Definition at line 37 of file TabularPrinter.h.

37 { }

Member Function Documentation

void CppSamples::Common::TabularPrinter::AddCellValue ( std::string  value)

Add a std::string value to the current cell in the table and forward the pointer to next cell.

Parameters
valueNew std::string value to be added..
void TabularPrinter::AddCellValue ( int  value)

Add a std::string value to the current cell in the table and forward the pointer to next cell.

Add a string value to the current cell in the table and forward the pointer to next cell.

Parameters
valueNew int value to be added..

Definition at line 53 of file TabularPrinter.cpp.

53  {
54  AddCellValue(to_string(value));
55 }
void AddCellValue(std::string value)
Add a std::string value to the current cell in the table and forward the pointer to next cell...
void TabularPrinter::AddColumn ( std::string  heading,
int  width,
int  alignment = -1 
)

Add a column to the table.

Parameters
headingHeading of the column.
widthWidth of the column.
isLeftAligned-1 for Left alignment. 0 for center. 1 for right alignment. [Default is Left Alignment].

Definition at line 22 of file TabularPrinter.cpp.

22  {
23  _columnCount++;
24  ColumnHeading* columnHeading = new ColumnHeading();
25  columnHeading->Value = heading;
26  columnHeading->Width = width;
27  columnHeading->Alignment = alignment;
28  _headings.push_back(columnHeading);
29 }
std::vector< ColumnHeading * > _headings
This class represents a column heading.
void TabularPrinter::AddRow ( )

Add a row to the table.

Definition at line 34 of file TabularPrinter.cpp.

34  {
35  _rows.push_back(new Row());
36 }
This class represents a row of data in the table.
string TabularPrinter::GetAlignedCellValue ( std::string  actualValue,
ColumnHeading columnHeading 
)
staticprivate

Definition at line 83 of file TabularPrinter.cpp.

83  {
84 
85  string alignedCellValue = "";
86  int colWidth = columnHeading->Width;
87  int currentWidth = actualValue.size();
88  if (currentWidth > colWidth)
89  alignedCellValue = actualValue.substr(0, colWidth);
90  else {
91  int spacing = colWidth - currentWidth;
92  int alignment = columnHeading->Alignment;
93  if (alignment == -1)
94  alignedCellValue = actualValue + string(spacing, ' ');
95  else if (alignment == 1)
96  alignedCellValue = string(spacing, ' ') + actualValue;
97  else {
98  int left = spacing / 2;
99  int right = spacing - left;
100  alignedCellValue = string(left, ' ') + actualValue + string(right, ' ');
101  }
102  }
103  return alignedCellValue;
104 }
string TabularPrinter::GetHeader ( )
private

Definition at line 106 of file TabularPrinter.cpp.

106  {
107  string rowValue = "";
108  int colCount = _headings.size();
109  for (int i = 0; i < colCount; i++)
110  rowValue += GetAlignedCellValue(_headings[i]->Value, _headings[i]);
111  return rowValue;
112 }
static std::string GetAlignedCellValue(std::string actualValue, ColumnHeading *columnHeading)
std::vector< ColumnHeading * > _headings
string TabularPrinter::PrintRow ( Row row)
private

Definition at line 74 of file TabularPrinter.cpp.

74  {
75 
76  string rowValue = "";
77  int colCount = row->Values.size();
78  for (int i = 0; i < colCount; i++)
79  rowValue += GetAlignedCellValue(row->Values[i], _headings[i]);
80  return rowValue;
81 }
static std::string GetAlignedCellValue(std::string actualValue, ColumnHeading *columnHeading)
std::vector< std::string > Values
std::vector< ColumnHeading * > _headings
string TabularPrinter::ToString ( )

Convert the table to std::string and return it.

Convert the table to string and return it.

Returns
String value representing the table.

Definition at line 61 of file TabularPrinter.cpp.

61  {
62 
63  string tableValue = GetHeader();
64  string line = string(tableValue.size(), '-');
65  tableValue = line + "\n" + tableValue + "\n" + line + "\n";
66 
67  int rowCount = _rows.size();
68  for (int i = 0; i < rowCount; i++)
69  tableValue += PrintRow(_rows[i]) + "\n";
70  tableValue += line + "\n";
71  return tableValue;
72 }

Member Data Documentation

int CppSamples::Common::TabularPrinter::_columnCount
private

Definition at line 71 of file TabularPrinter.h.

std::vector<ColumnHeading*> CppSamples::Common::TabularPrinter::_headings
private

Definition at line 72 of file TabularPrinter.h.

std::vector<Row*> CppSamples::Common::TabularPrinter::_rows
private

Definition at line 73 of file TabularPrinter.h.


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