Reference documentation for deal.II version Git 3ecc854 2017-02-27 14:34:24 -0500
Public Member Functions | Private Attributes | List of all members
ConditionalOStream Class Reference

#include <deal.II/base/conditional_ostream.h>

Public Member Functions

 ConditionalOStream (std::ostream &stream, const bool active=true)
void set_condition (const bool active)
bool is_active () const
std::ostream & get_stream () const
template<typename T >
const ConditionalOStreamoperator<< (const T &t) const
const ConditionalOStreamoperator<< (std::ostream &(*p)(std::ostream &)) const

Private Attributes

std::ostream & output_stream
bool active_flag

Detailed Description

A class that allows printing to an output stream, e.g. std::cout, depending on the ConditionalOStream object being active (default) or not. The condition of this object can be changed by set_condition() and in the constructor. This class is used in the step-17, step-18, step-32, step-33, and step-35 tutorial programs.

This class is mostly useful in parallel computations. Ordinarily, you would use std::cout to print messages like what the program is presently doing, or the number of degrees of freedom in each step. However, in parallel programs, this means that each of the MPI processes write to the screen, which yields many repetitions of the same text. To avoid it, one would have to have a designated process, say the one with MPI process number zero, do the output, and guard each write statement with an if- condition. This becomes cumbersome and clutters up the code. Rather than doing so, the present class can be used: objects of its type act just like a standard output stream, but they only print something based on a condition that can be set to, for example, mpi_process==0, so that only one process has a true condition and in all other processes writes to this object just disappear in nirvana.

The usual usage of this class is as follows:

ConditionalOStream pout(std::cout, this_mpi_process==0);
// all processes print following
// information to standard output
std::cout << "Reading parameter file on process "
<< this_mpi_process << std::endl;
// following is printed by
// process 0 only
pout << "Solving ..." << std::endl;
pout << "done" << std::endl;

Here, `Reading parameter file on process xy' is printed by each process separately. In contrast to that, `Solving ...' and `done' is printed to standard output only once, namely by process 0.

This class is not derived from ostream. Therefore


is not possible. Instead use the is_active() function for a work- around:

if (pout.is_active())
Ralf Hartmann, Wolfgang Bangerth, 2004

Definition at line 83 of file conditional_ostream.h.

Constructor & Destructor Documentation

ConditionalOStream::ConditionalOStream ( std::ostream &  stream,
const bool  active = true 

Constructor. Set the stream to which we want to write, and the condition based on which writes are actually forwarded. Per default the condition of an object is active.

Definition at line 21 of file

Member Function Documentation

void ConditionalOStream::set_condition ( const bool  active)

Depending on the active flag set the condition of this stream to active (true) or non-active (false). An object of this class prints to cout if and only if its condition is active.

Definition at line 29 of file

bool ConditionalOStream::is_active ( ) const

Return the condition of the object.

Definition at line 35 of file

std::ostream & ConditionalOStream::get_stream ( ) const

Return a reference to the stream currently in use.

Definition at line 172 of file conditional_ostream.h.

template<class T >
const ConditionalOStream & ConditionalOStream::operator<< ( const T &  t) const

Output a constant something through this stream. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

Definition at line 150 of file conditional_ostream.h.

const ConditionalOStream & ConditionalOStream::operator<< ( std::ostream &(*)(std::ostream &)  p) const

Treat ostream manipulators. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

Note that compilers want to see this treated differently from the general template above since functions like std::endl are actually overloaded and can't be bound directly to a template type.

Definition at line 161 of file conditional_ostream.h.

Member Data Documentation

std::ostream& ConditionalOStream::output_stream

Reference to the stream we want to write to.

Definition at line 136 of file conditional_ostream.h.

bool ConditionalOStream::active_flag

Stores the actual condition the object is in.

Definition at line 141 of file conditional_ostream.h.

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