Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Euclid::Table::AsciiWriter Class Reference

TableWriter implementation for writing ASCII tables to streams. More...

#include <AsciiWriter.h>

Inheritance diagram for Euclid::Table::AsciiWriter:
Inheritance graph
[legend]
Collaboration diagram for Euclid::Table::AsciiWriter:
Collaboration graph
[legend]

Public Member Functions

 AsciiWriter (std::ostream &stream)
 Constructs an AsciiWriter which writes to the given stream.
 
 AsciiWriter (const std::string &filename)
 
 AsciiWriter (AsciiWriter &&)=default
 
AsciiWriteroperator= (AsciiWriter &&)=default
 
 AsciiWriter (const AsciiWriter &)=delete
 
AsciiWriteroperator= (const AsciiWriter &)=delete
 
virtual ~AsciiWriter ()=default
 Destructor.
 
AsciiWritersetCommentIndicator (const std::string &indicator)
 Set the comment indicator.
 
AsciiWritershowColumnInfo (bool show)
 Sets if the column information will be written to the stream.
 
void addComment (const std::string &message) override
 Adds a comment to the stream.
 
- Public Member Functions inherited from Euclid::Table::TableWriter
 TableWriter ()=default
 
 TableWriter (TableWriter &&)=default
 
TableWriteroperator= (TableWriter &&)=default
 
 TableWriter (const TableWriter &)=delete
 
TableWriteroperator= (const TableWriter &)=delete
 
virtual ~TableWriter ()=default
 
void addData (const Table &table)
 Appends the contents of the given table to the output.
 

Static Public Member Functions

template<typename StreamType , typename... Args>
static AsciiWriter create (Args &&... args)
 Constructs an AsciiWriter which contains an object of type StreamType.
 

Protected Member Functions

void init (const Table &table) override
 
void append (const Table &table) override
 

Private Member Functions

 AsciiWriter (std::unique_ptr< InstOrRefHolder< std::ostream > > stream_holder)
 

Private Attributes

std::unique_ptr< InstOrRefHolder< std::ostream > > m_stream_holder
 
bool m_writing_started = false
 
bool m_initialized = false
 
std::string m_comment = "#"
 
bool m_show_column_info = true
 
std::vector< size_t > m_column_lengths
 

Detailed Description

TableWriter implementation for writing ASCII tables to streams.

The format of the ASCII tables produced is the following:

The table starts with all the given comments, with one comment per line. The comments are followed by one empty line. Comments can be added by using the addComment() method. The comment indicator is by default the # character and it can be modified by using the setCommentIndicator() method.

The next rows are comment lines describing the columns of the table. They follow the format: "Column: NAME TYPE (UNIT) - DESCRIPTION", where the unit and description parts are presented only when they are not empty. The strings used as the column types are:

The column descriptions are following the same order as the columns of the table and are followed by one empty line. These descriptions can be disabled by using the showColumnInfo() method.

After that, the first row written is a comment line containing the names of the columns, followed by one empty line. Then one line is written for each Row of the table, which contains the values of the row. The boolean values are represented with "1" (meaning true) and "0" meaning false. The vector values are separated by ",".

All the alignment between the columns is done with space characters. The size in characters of each column is calculated as the size of the longest column entry (including type and name) plus one. If a subsequent call of addData() would result to longer columns, this is applied from that moment on. All the values are right aligned.

Definition at line 80 of file AsciiWriter.h.

Constructor & Destructor Documentation

◆ AsciiWriter() [1/5]

Euclid::Table::AsciiWriter::AsciiWriter ( std::ostream stream)
explicit

Constructs an AsciiWriter which writes to the given stream.

Definition at line 35 of file AsciiWriter.cpp.

◆ AsciiWriter() [2/5]

Euclid::Table::AsciiWriter::AsciiWriter ( const std::string filename)
explicit

Constructs an AsciiWriter which writes to the given file (overrides if it already exists)

Definition at line 37 of file AsciiWriter.cpp.

◆ AsciiWriter() [3/5]

Euclid::Table::AsciiWriter::AsciiWriter ( AsciiWriter &&  )
default

◆ AsciiWriter() [4/5]

Euclid::Table::AsciiWriter::AsciiWriter ( const AsciiWriter )
delete

◆ ~AsciiWriter()

virtual Euclid::Table::AsciiWriter::~AsciiWriter ( )
virtualdefault

Destructor.

◆ AsciiWriter() [5/5]

Euclid::Table::AsciiWriter::AsciiWriter ( std::unique_ptr< InstOrRefHolder< std::ostream > >  stream_holder)
explicitprivate

Definition at line 39 of file AsciiWriter.cpp.

Member Function Documentation

◆ addComment()

void Euclid::Table::AsciiWriter::addComment ( const std::string message)
overridevirtual

Adds a comment to the stream.

This method can only be called before any data have been written. It directly adds a comment to the stream, following the rules explained at the documentation of the class.

Parameters
messageThe message to add
Exceptions
Elements::ExceptionIf data have already been written

Implements Euclid::Table::TableWriter.

Definition at line 63 of file AsciiWriter.cpp.

References std::getline(), m_comment, m_initialized, m_stream_holder, and m_writing_started.

Here is the call graph for this function:

◆ append()

void Euclid::Table::AsciiWriter::append ( const Table table)
overrideprotectedvirtual

Writes to the stream the contents of the table, following the rules explained at the class documentation

Implements Euclid::Table::TableWriter.

Definition at line 115 of file AsciiWriter.cpp.

References Euclid::Table::calculateColumnLengths(), m_comment, m_stream_holder, std::setw(), and std::string::size().

Here is the call graph for this function:

◆ create()

template<typename StreamType , typename... Args>
static AsciiWriter Euclid::Table::AsciiWriter::create ( Args &&...  args)
static

Constructs an AsciiWriter which contains an object of type StreamType.

This is the most generic construction of AsciiWriter, which can be used with any type which inherits from std::istream. It is public to provide full flexibility, but the other constructors should cover most of the use cases of this class. When this constructor is used, an object of type StreamType is constructed using the given arguments, which will be deleted when the AsciiWriter goes out of scope. If you want to not bound the lifetime of the stream with the AsciiWriter you should use the AsciiWriter(std::ostream&) constructor instead.

Note that when this method is called, only the StreamType template parameter has to be specified. The argument types will be inferred.

Template Parameters
StreamTypeThe type of the stream to use for writing
ArgsThe types of arguments to pass to the StreamType constructor
Parameters
argsThe arguments to use for constructing the StreamType instance
Returns
The newly constructed AsciiWriter

◆ init()

void Euclid::Table::AsciiWriter::init ( const Table table)
overrideprotectedvirtual

Writes to the stream the column info, following the rules explained at the class documentation

Implements Euclid::Table::TableWriter.

Definition at line 78 of file AsciiWriter.cpp.

References std::string::c_str(), Euclid::Table::calculateColumnLengths(), m_comment, m_initialized, m_show_column_info, m_stream_holder, m_writing_started, Euclid::Table::quoted(), std::setw(), and Euclid::Table::typeToKeyword().

Here is the call graph for this function:

◆ operator=() [1/2]

AsciiWriter & Euclid::Table::AsciiWriter::operator= ( AsciiWriter &&  )
default

◆ operator=() [2/2]

AsciiWriter & Euclid::Table::AsciiWriter::operator= ( const AsciiWriter )
delete

◆ setCommentIndicator()

AsciiWriter & Euclid::Table::AsciiWriter::setCommentIndicator ( const std::string indicator)

Set the comment indicator.

This method can be used to change the comment indicator to something different than the default (#). It returns a reference to the AsciiWriter so it can be chained with other calls in the same line.

Returns
A reference to the AsciiWriter instance
Exceptions
Elements::ExceptionIf the given indicator is the empty string
Elements::Exceptionif writing of data has already started

Definition at line 42 of file AsciiWriter.cpp.

References std::string::empty(), m_comment, and m_writing_started.

Here is the call graph for this function:

◆ showColumnInfo()

AsciiWriter & Euclid::Table::AsciiWriter::showColumnInfo ( bool  show)

Sets if the column information will be written to the stream.

Parameters
showTrue to write the information, false to skip it
Returns
A reference to the AsciiWriter instance
Exceptions
Elements::Exceptionif writing of data has already started

Definition at line 54 of file AsciiWriter.cpp.

References m_show_column_info, and m_writing_started.

Member Data Documentation

◆ m_column_lengths

std::vector<size_t> Euclid::Table::AsciiWriter::m_column_lengths
private

Definition at line 185 of file AsciiWriter.h.

◆ m_comment

std::string Euclid::Table::AsciiWriter::m_comment = "#"
private

Definition at line 183 of file AsciiWriter.h.

Referenced by addComment(), append(), init(), and setCommentIndicator().

◆ m_initialized

bool Euclid::Table::AsciiWriter::m_initialized = false
private

Definition at line 182 of file AsciiWriter.h.

Referenced by addComment(), and init().

◆ m_show_column_info

bool Euclid::Table::AsciiWriter::m_show_column_info = true
private

Definition at line 184 of file AsciiWriter.h.

Referenced by init(), and showColumnInfo().

◆ m_stream_holder

std::unique_ptr<InstOrRefHolder<std::ostream> > Euclid::Table::AsciiWriter::m_stream_holder
private

Definition at line 180 of file AsciiWriter.h.

Referenced by addComment(), append(), and init().

◆ m_writing_started

bool Euclid::Table::AsciiWriter::m_writing_started = false
private

Definition at line 181 of file AsciiWriter.h.

Referenced by addComment(), init(), setCommentIndicator(), and showColumnInfo().


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