Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
TableWriter implementation for writing ASCII tables to streams. More...
#include <AsciiWriter.h>
Public Member Functions | |
AsciiWriter (std::ostream &stream) | |
Constructs an AsciiWriter which writes to the given stream. | |
AsciiWriter (const std::string &filename) | |
AsciiWriter (AsciiWriter &&)=default | |
AsciiWriter & | operator= (AsciiWriter &&)=default |
AsciiWriter (const AsciiWriter &)=delete | |
AsciiWriter & | operator= (const AsciiWriter &)=delete |
virtual | ~AsciiWriter ()=default |
Destructor. | |
AsciiWriter & | setCommentIndicator (const std::string &indicator) |
Set the comment indicator. | |
AsciiWriter & | showColumnInfo (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. | |
![]() | |
TableWriter ()=default | |
TableWriter (TableWriter &&)=default | |
TableWriter & | operator= (TableWriter &&)=default |
TableWriter (const TableWriter &)=delete | |
TableWriter & | operator= (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 |
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.
|
explicit |
Constructs an AsciiWriter which writes to the given stream.
Definition at line 35 of file AsciiWriter.cpp.
|
explicit |
Constructs an AsciiWriter which writes to the given file (overrides if it already exists)
Definition at line 37 of file AsciiWriter.cpp.
|
default |
|
delete |
|
virtualdefault |
Destructor.
|
explicitprivate |
Definition at line 39 of file AsciiWriter.cpp.
|
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.
message | The message to add |
Elements::Exception | If 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.
|
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().
|
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.
StreamType | The type of the stream to use for writing |
Args | The types of arguments to pass to the StreamType constructor |
args | The arguments to use for constructing the StreamType instance |
|
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().
|
default |
|
delete |
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.
Elements::Exception | If the given indicator is the empty string |
Elements::Exception | if writing of data has already started |
Definition at line 42 of file AsciiWriter.cpp.
References std::string::empty(), m_comment, and m_writing_started.
AsciiWriter & Euclid::Table::AsciiWriter::showColumnInfo | ( | bool | show | ) |
Sets if the column information will be written to the stream.
show | True to write the information, false to skip it |
Elements::Exception | if writing of data has already started |
Definition at line 54 of file AsciiWriter.cpp.
References m_show_column_info, and m_writing_started.
|
private |
Definition at line 185 of file AsciiWriter.h.
|
private |
Definition at line 183 of file AsciiWriter.h.
Referenced by addComment(), append(), init(), and setCommentIndicator().
|
private |
Definition at line 182 of file AsciiWriter.h.
Referenced by addComment(), and init().
|
private |
Definition at line 184 of file AsciiWriter.h.
Referenced by init(), and showColumnInfo().
|
private |
Definition at line 180 of file AsciiWriter.h.
Referenced by addComment(), append(), and init().
|
private |
Definition at line 181 of file AsciiWriter.h.
Referenced by addComment(), init(), setCommentIndicator(), and showColumnInfo().