fpmas 1.6
Public Member Functions | List of all members
fpmas::io::FileOutput Class Reference

#include <output.h>

Inheritance diagram for fpmas::io::FileOutput:
Inheritance graph
[legend]
Collaboration diagram for fpmas::io::FileOutput:
Collaboration graph
[legend]

Public Member Functions

 FileOutput ()
 
 FileOutput (std::string filename, std::ios::openmode mode=std::ios::out)
 
 FileOutput (std::string file_format, int rank, std::ios::openmode mode=std::ios::out)
 
 FileOutput (std::string file_format, api::scheduler::TimeStep step, std::ios::openmode mode=std::ios::out)
 
 FileOutput (std::string file_format, int rank, api::scheduler::TimeStep step, std::ios::openmode mode=std::ios::out)
 
std::ofstream & get () override
 
- Public Member Functions inherited from fpmas::api::io::OutputStream
virtual std::ostream & get ()=0
 
std::ostream & operator<< (std::ostream &(*func)(std::ostream &))
 

Detailed Description

An helper class that can be used to initialize a file output stream.

This class is notably useful to extend output classes that take an output stream as constructor parameter, such as CsvOutput.

For example, the following code is wrong and will produce a segmentation fault:

class MyOutput : public fpmas::io::CsvOutput<int> {
private:
fpmas::io::FileOutput file("file.csv");
public:
MyOutput()
: fpmas::io::CsvOutput<int>(
this->file,
{"my_field", [] () {return ...}}
) {}
};
Definition: csv_output.h:153
Definition: output.h:366
Definition: fpmas.cpp:3

Indeed, the CsvOutput constructor uses the specified output stream (to automatically write CSV headers), BUT CsvOutput, as a base class of MyOutput, is initialized before MyOutput, so before file, what produces a segmentation fault.

This FileOutput class can be used to easily solve the issue:

// FileOutput must be specified BEFORE CsvOutput
class MyOutput : public fpmas::io::FileOutput, fpmas::io::CsvOutput<int> {
public:
MyOutput() :
fpmas::io::FileOutput("file.csv"),
fpmas::io::CsvOutput<int>(
*this,
{"my_field", [] () {return ...}}
) {}
};
FileOutput()
Definition: output.h:381

In this case, FileOutput is properly initialized before CsvOutput so no error occurs.

Constructor & Destructor Documentation

◆ FileOutput() [1/5]

fpmas::io::FileOutput::FileOutput ( )
inline

Default constructor.

The underlying file is left in an undefined state.

◆ FileOutput() [2/5]

fpmas::io::FileOutput::FileOutput ( std::string  filename,
std::ios::openmode  mode = std::ios::out 
)

FileOutput constructor.

The underlying file is opened only when get() is called.

See also
https://en.cppreference.com/w/cpp/io/basic_filebuf/open
Parameters
filenamefile name
modestandard open mode

◆ FileOutput() [3/5]

fpmas::io::FileOutput::FileOutput ( std::string  file_format,
int  rank,
std::ios::openmode  mode = std::ios::out 
)

FileOutput constructor.

file_format is formatted using fpmas::utils::format(std::string, int) to produce a file name.

The underlying file is opened only when get() is called.

See also
https://en.cppreference.com/w/cpp/io/basic_filebuf/open
Parameters
file_formatfile name to format
rankrank of the current process
modestandard open mode

◆ FileOutput() [4/5]

fpmas::io::FileOutput::FileOutput ( std::string  file_format,
api::scheduler::TimeStep  step,
std::ios::openmode  mode = std::ios::out 
)

FileOutput constructor.

file_format is formatted using fpmas::utils::format(std::string, fpmas::api::scheduler::TimeStep) to produce a file name.

The underlying file is opened only when get() is called.

See also
https://en.cppreference.com/w/cpp/io/basic_filebuf/open
Parameters
file_formatfile name to format
stepcurrent time step
modestandard open mode

◆ FileOutput() [5/5]

fpmas::io::FileOutput::FileOutput ( std::string  file_format,
int  rank,
api::scheduler::TimeStep  step,
std::ios::openmode  mode = std::ios::out 
)

FileOutput constructor.

file_format is formatted using fpmas::utils::format(std::string, int, fpmas::api::scheduler::TimeStep) to produce a file name.

The underlying file is opened only when get() is called.

See also
https://en.cppreference.com/w/cpp/io/basic_filebuf/open
Parameters
file_formatfile name to format
rankrank of the current process
stepcurrent time step
modestandard open mode

Member Function Documentation

◆ get()

std::ofstream & fpmas::io::FileOutput::get ( )
overridevirtual

Returns a reference to the internal file output stream, that can be used to output data using the classical C++ << operator.

The file is open once, the first time this get() method is called.

This allows the FileOutput object to be declared on all processes, while being opened only on one process, assuming only one process is writing to the file, preventing other processes to delete the file content when it is opened if the current openmode is out.

Returns
file output stream

Implements fpmas::api::io::OutputStream.


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