Psi4
Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Private Attributes | List of all members
psi::TableSpecs< T1, T2, T3, T4, T5, T6 > Class Template Reference

TableSpecs will hold all the data needed to print a table out. More...

#include <TableSpecs.h>

Public Member Functions

virtual void SetTitles (const std::vector< std::string > &Titles)
 Sets the titles. More...
 
virtual void SetWidths (const std::vector< int > &Widths)
 Sets the width of each column. More...
 
virtual void SetAlignments (const std::vector< Align > &Alignments)
 Sets the alignment of the columns. More...
 
virtual std::string Table () const
 
 TableSpecs (const int NRows, const char TitleSep='-', const char ColSep=' ', const char RowSep='\0', int Width=80)
 Default constructor, sets Data to NULL. More...
 
virtual ~TableSpecs ()
 
void Init (T1 *Data1, T2 *Data2=NULL, T3 *Data3=NULL, T4 *Data4=NULL, T5 *Data5=NULL, T6 *Data6=NULL)
 The call that actually initializes this class. More...
 
template<typename NotDouble >
std::string PrintData (const NotDouble Data) const
 Overloaded function to catch doubles. More...
 
std::string PrintData (const double Data) const
 
virtual std::string GetData (const int row) const
 Returns the Data for row "row" as a string (non-EOL terminated) More...
 

Protected Member Functions

virtual bool IsBase () const
 Tells us we are no longer the base recursion class. More...
 
virtual int NCols () const
 Returns the number of columns. More...
 
virtual std::vector< std::string > Title () const
 Returns the title vector. More...
 
virtual std::vector< AlignAlignments () const
 Returns the Alignment vector. More...
 
virtual int Width () const
 Returns the width. More...
 
virtual char RowSep () const
 Returns the RowSeparator. More...
 
virtual char ColSep () const
 Returns the Column separator. More...
 
virtual char TitleSep () const
 Returns the Title separator. More...
 
virtual int NRows () const
 Returns the number of rows. More...
 
virtual std::vector< int > ReqWidth () const
 Returns the requested width of the column. More...
 

Protected Attributes

T1 * Data_
 This class does not take ownership of your data. More...
 

Private Types

typedef TableSpecs< T2, T3, T4,
T5, T6 > 
BaseType
 Typedef of the base for compactness. More...
 

Private Attributes

std::string TokenSep_
 

Detailed Description

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
class psi::TableSpecs< T1, T2, T3, T4, T5, T6 >

TableSpecs will hold all the data needed to print a table out.

General case, class that is instantiated any time all T's are not Nothing.

Because I'm being a good boy and not using C++11, this class suffers from two problems one it can't contain an arbitrary number of parameters and two every time a specialization is needed code has to be copied and pasted. Because of the recursive nature

Say you want to have this class make a M column table, where each column has a data type of a different T and is labeled Tc where c is the column number, i.e. T1 is the type of column1, T2, is the type of column 2, etc. Furthermore assume that your table will have N rows. I mandate that the number of rows for each column must be the same, so use place holders if that isn't the case. Your object definition is (a lot longer here because I have to show you all the options etc.):

*
*
* char TSep='*';
*
*
* char ColSep='|';
*
*
* char RowSep="-";
*
*
* int Width=26;
*
*
* TableSpecs<T1,T2,T3,...,TM> MyData(N,TSep,ColSep,RowSep,Width);
*
*
* T1* Data1;
* T2* Data2;
* ...
* TM* Data3;
*
* MyData.Init(Data1,Data2,...,DataM);
*
* // This is a vector of the column titles
* //
* // Each title may span more than one line. If they do Titles should
* // be an array, such that if the longest title spans L lines it is of
* // L*M length, and Titles[i*M+j] is the i-th line of the j-th
* // title
* std::vector<std::string> Titles;
* Titles.push_back("Title1");
* Titles.push_back("Title2");
* ...
* Titles.push_back("TitleM");
*
* MyData.SetTitles(Titles);//If not specified no titles printed
*
*
*
* std::vector<Align> Alignments;
* Alignments.push_back(Align1);
* Alignments.push_back(Align2);
* ...
* Alignments.push_back(Align3);
*
* MyData.SetAlignments(Alignments);
*
*
*
* std::string MyTable=MyData.Table();
*

The above code should generate something along the lines of (blank lines between rows so hopefully doxygen doesn't destroy it):

Column #: 0123456789ABCDEFGHIJKLMNOP (assumed 26 columns)

          Title1 | Title2 | Title3

         **************************

         Data1[0]|Data2[0]|Data3[0]

         --------------------------

         Data1[1]|Data2[1]|Data3[1]

         --------------------------

         ...

         --------------------------

         Data1[N]|Data2[N]|Data3[N]

In order to minimize the amount of duplicate code this class is defined recursively, which may make it hard to understand at first. Consequentially, I have included a pretty thorough description below if you are hell-bent on understanding it or modifying it read on.

The line this comment is actually attached to is a forward declaration of a TableSpecs class with up to 6 Columns. All of these classes together are make up the TableSpecs class and how it works is defined here. If in the future more columns are needed simply add more template parameters and update the following classes so that they are consistent with the number of template parameters you changed it to. You also will need to modify StreamBase.h's MakeTable function.

The following discussion assumes that the class is still built with 6 columns; if it is not the generalization to the current number of columns should be straightforward.

Only one implementation of a TableSpecs class is actually defined. That is the case we call the general case and it corresponds to the case when 6 arguments are specified. Every case between 6 and 0 Columns is then filled in via recursion. At each level of recursion we pull of the first template parameter and define it as a data set and pass the remaining ones to the base class. This ends when we hit the class TableSpecs<Nothing,Nothing,Nothing,Nothing,Nothing,Nothing>, which we term the base case.

So how does this work more concretely, say we want three columns of integers, we then define an object TableSpecs<int,int,int>. This expands to the general case of TableSpecs<int,int,int,Nothing,Nothing,Nothing>, which has a member std::vector<int> Data and a base class: TableSpces<int,int,Nothing,Nothing,Nothing,Nothing>. This base class has a member std::vector<int> Data and a base class TableSpecs<int,Nothing...> where the ellipses continue for 4 more Nothings. This base class also has a member std::vector<int> Data, but now it's base class is the base recursion case. Ultimately we use typedefs to avoid the messy types that arise.

Let us assume in our example above are TableSpecs objects are of types: General, Generalm1 (General "minus" 1), Generalm2, and Base for the class hierarchy from TableSpecs<int,int,int> down to the base case respectively. Column 1's data is then General::Data, Column 2's data is then Generalm1::Data, and Column 3's data is then Generalm2::Data.

Also note that for the for the Table() function to work, each class needs to have access to the data. The easiest way to do this is to use setters and getters to go to and from the base respectively.

Member Typedef Documentation

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
typedef TableSpecs<T2, T3, T4, T5, T6> psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::BaseType
private

Typedef of the base for compactness.

Constructor & Destructor Documentation

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::TableSpecs ( const int  NRows,
const char  TitleSep = '-',
const char  ColSep = ' ',
const char  RowSep = '\0',
int  Width = 80 
)
inline

Default constructor, sets Data to NULL.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::~TableSpecs ( )
inlinevirtual

Member Function Documentation

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual std::vector<Align> psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Alignments ( ) const
inlineprotectedvirtual

Returns the Alignment vector.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual char psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::ColSep ( ) const
inlineprotectedvirtual

Returns the Column separator.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual std::string psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::GetData ( const int  row) const
inlinevirtual

Returns the Data for row "row" as a string (non-EOL terminated)

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
void psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Init ( T1 *  Data1,
T2 *  Data2 = NULL,
T3 *  Data3 = NULL,
T4 *  Data4 = NULL,
T5 *  Data5 = NULL,
T6 *  Data6 = NULL 
)
inline

The call that actually initializes this class.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual bool psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::IsBase ( ) const
inlineprotectedvirtual

Tells us we are no longer the base recursion class.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual int psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::NCols ( ) const
inlineprotectedvirtual

Returns the number of columns.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual int psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::NRows ( ) const
inlineprotectedvirtual

Returns the number of rows.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
template<typename NotDouble >
std::string psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::PrintData ( const NotDouble  Data) const
inline

Overloaded function to catch doubles.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
std::string psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::PrintData ( const double  Data) const
inline
template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual std::vector<int> psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::ReqWidth ( ) const
inlineprotectedvirtual

Returns the requested width of the column.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual char psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::RowSep ( ) const
inlineprotectedvirtual

Returns the RowSeparator.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual void psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::SetAlignments ( const std::vector< Align > &  Alignments)
inlinevirtual

Sets the alignment of the columns.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual void psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::SetTitles ( const std::vector< std::string > &  Titles)
inlinevirtual

Sets the titles.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual void psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::SetWidths ( const std::vector< int > &  Widths)
inlinevirtual

Sets the width of each column.

template<typename T1 , typename T2 , typename T3 , typename T4 , typename T5 , typename T6 >
std::string psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Table ( ) const
virtual

Returns the Table as a string

Start with the number of rows

Then the widths the user set up

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual std::vector<std::string> psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Title ( ) const
inlineprotectedvirtual

Returns the title vector.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual char psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::TitleSep ( ) const
inlineprotectedvirtual

Returns the Title separator.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
virtual int psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Width ( ) const
inlineprotectedvirtual

Returns the width.

Member Data Documentation

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
T1* psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::Data_
protected

This class does not take ownership of your data.

template<typename T1 = Nothing, typename T2 = Nothing, typename T3 = Nothing, typename T4 = Nothing, typename T5 = Nothing, typename T6 = Nothing>
std::string psi::TableSpecs< T1, T2, T3, T4, T5, T6 >::TokenSep_
private

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